Segment Cost
Practice
3 (10 votes)
Binary search
Algorithms
C++
Problem
69% Success 2272 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given an array of integers \(A\) of size \(N\). Cost of a segment \((L,R)\) is defined as \((A[L] + A[L+1] + .... +A[R])\) - \((A[L] \oplus A[L+1] \oplus .... \oplus A[R])\) (where \(\newcommand*\xor{\oplus}\) denotes the Bitwise XOR operator).
Given a range \((x, y)\), find the smallest subsegment \((x', y')\) such that \(x \le x' \le y' \le y\) and its cost is maximum among all the subsegments of \((x, y)\).
If there exists more than answer, print the subsegment with smallest \(x'\).
Input format
- First line contains an integer \(N\)
- Second line contains \(N\) space-separated integers denoting the array \(A\)
- Next line contains two space separated integer \(x\ y\)
Output format
Print two space separated integers \(x' \ y'\) denoting the subsegment.
Constraints
\(1 \le N \le 10^5 \\ 0 \le A[i] \le 10^9 \\ 1 \le x \le y \le N\)
Submissions
Please login to view your submissions
Similar Problems
Points:20
446 votes
Tags:
MathematicsEasy
Points:30
6 votes
Tags:
ApprovedBinary SearchMathMediumOpenSorting
Points:30
11 votes
Tags:
AlgorithmsBinary SearchGreedy AlgorithmsMediumReadySearching
Editorial