Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int nmax = 100006;
- int block[nmax], blockSize, a[nmax], N, M;
- long long sumByBlock[nmax];
- long long getSum(int L, int R) {
- long long ans = 0;
- while(L < R && block[L] == block[L + 1]) {
- ans += a[L];
- L++;
- }
- while(R > L && block[R] == block[R - 1]) {
- ans += a[R];
- R--;
- }
- if(L == R) {
- ans += a[L];
- L++;
- } else {
- ans += a[L];
- ans += a[R];
- L++;
- R--;
- }
- if(L <= R) {
- for(int j = block[L]; j <= block[R]; ++j) {
- ans += sumByBlock[j];
- }
- }
- return ans;
- }
- void updatePos(int pos, int x) {
- int whatBlock = block[pos];
- sumByBlock[whatBlock] -= a[pos];
- a[pos] = x;
- sumByBlock[whatBlock] += a[pos];
- }
- int main() {
- cin >> N;
- for(int i = 0; i < N; ++i) {
- cin >> a[i];
- }
- blockSize = (int) sqrt(N);
- for(int i = 0; i < N; ++i) {
- block[i] = i / blockSize;
- }
- for(int i = 0; i < N; ++i) {
- int whatBlock = block[i];
- sumByBlock[whatBlock] += a[i];
- }
- cin >> M;
- long long ans = -2e18;
- for(int i = 0; i < M; ++i) {
- int L, R;
- cin >> L >> R;
- if(L > R) {
- swap(L, R);
- }
- ans = max(ans, getSum(L - 1, R - 1));
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment