#include <iostream> #include <utility> #include <vector> #include <algorithm> using namespace std; int n, k, q; long long tab[300001], wyn[300001], t[1048577], mxtab[300001], base = 1, tree[1048577], tabwyn[300001]; vector<pair<pair<int, int>, int>> v; void insert(int i, long long wart){ i += base-1; t[i] += wart; i >>= 1; while(i >= 1){ t[i] = max(t[i<<1], t[(i<<1)+1]); i >>= 1; } } long long query(int i, int pocz, int kon, int x, int y){ if(x <= pocz && kon <= y) return t[i]; int sr = (pocz+kon)>>1; long long wynik = 0; if(x <= sr) wynik = max(wynik, query(i<<1, pocz, sr, x, y)); if(sr < y) wynik = max(wynik, query((i<<1)+1, sr+1, kon, x, y)); return wynik; } int main() { scanf("%d%d%d", &n, &k, &q); while(base < n-k+1) base <<= 1; long long sum = 0; for(int i = 1; i <= k; ++i){ scanf("%lld", &tab[i]); sum += tab[i]; } wyn[1] = sum; t[base] = tree[base] = sum; for(int i = k+1; i <= n; ++i){ scanf("%lld", &tab[i]); sum -= tab[i-k]; sum += tab[i]; wyn[i-k+1] = sum; tree[base+(i-k+1)-1] = t[base+(i-k+1)-1] = sum; } for(int i = base-1; i >= 1; --i) tree[i] = t[i] = max(tree[i<<1], tree[(i<<1)+1]); for(int i = 1; i <= q; ++i){ int l, p; scanf("%d%d", &l, &p); v.push_back(make_pair(make_pair(p, l), i)); } sort(v.begin(), v.end()); //for(int i = 0; i < (int) v.size(); ++i) printf("%d %d\n", v[i].first.second, v[i].first.first); //printf("\n\n"); while(!v.empty()){ // p - first, l - second int l, p, currentP = v.back().first.first; long long wynik = 0; bool pain = 0; l = v.back().first.second; p = v.back().first.first; //printf("p: %d %d\n", currentP, (int) v.size()); p -= k - 1; while(l > p && v.back().first.first == currentP){ tabwyn[v.back().second] = 0; v.pop_back(); if(v.empty()){ pain = 1; break; } l = v.back().first.second; } if(pain) break; if(v.back().first.first != currentP) continue; while(p-k+1 <= l && v.back().first.first == currentP){ // jedna mozliwosc wynik = max((long long) 0, query(1, 1, base, l, p)); tabwyn[v.back().second] = wynik; wynik = 0; v.pop_back(); if(v.empty()){ pain = 1; break; } l = v.back().first.second; } if(pain) break; if(v.back().first.first != currentP) continue; //laczenie wynik = max((long long) 0, query(1, 1, base, p-k+1, p)); int j = p-k; while(v.back().first.first == currentP){ int tmp = query(1, 1, base, j+k, p); if(j+k <= p) insert(j, tmp); wynik = max(wynik, t[j+base-1]); while(l == j){ tabwyn[v.back().second] = wynik; v.pop_back(); if(v.empty()){ pain = 1; break; } if(v.back().first.first == currentP) l = v.back().first.second; else break; } --j; } //p -= k; l += base-1; p += base-1; while(l != 1 && p != 1){ for(int j = l; j <= p; ++j) t[j] = tree[j]; l >>= 1; p >>= 1; } t[1] = tree[1]; } for(int i = 1; i <= q; ++i) printf("%lld\n", tabwyn[i]); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <iostream> #include <utility> #include <vector> #include <algorithm> using namespace std; int n, k, q; long long tab[300001], wyn[300001], t[1048577], mxtab[300001], base = 1, tree[1048577], tabwyn[300001]; vector<pair<pair<int, int>, int>> v; void insert(int i, long long wart){ i += base-1; t[i] += wart; i >>= 1; while(i >= 1){ t[i] = max(t[i<<1], t[(i<<1)+1]); i >>= 1; } } long long query(int i, int pocz, int kon, int x, int y){ if(x <= pocz && kon <= y) return t[i]; int sr = (pocz+kon)>>1; long long wynik = 0; if(x <= sr) wynik = max(wynik, query(i<<1, pocz, sr, x, y)); if(sr < y) wynik = max(wynik, query((i<<1)+1, sr+1, kon, x, y)); return wynik; } int main() { scanf("%d%d%d", &n, &k, &q); while(base < n-k+1) base <<= 1; long long sum = 0; for(int i = 1; i <= k; ++i){ scanf("%lld", &tab[i]); sum += tab[i]; } wyn[1] = sum; t[base] = tree[base] = sum; for(int i = k+1; i <= n; ++i){ scanf("%lld", &tab[i]); sum -= tab[i-k]; sum += tab[i]; wyn[i-k+1] = sum; tree[base+(i-k+1)-1] = t[base+(i-k+1)-1] = sum; } for(int i = base-1; i >= 1; --i) tree[i] = t[i] = max(tree[i<<1], tree[(i<<1)+1]); for(int i = 1; i <= q; ++i){ int l, p; scanf("%d%d", &l, &p); v.push_back(make_pair(make_pair(p, l), i)); } sort(v.begin(), v.end()); //for(int i = 0; i < (int) v.size(); ++i) printf("%d %d\n", v[i].first.second, v[i].first.first); //printf("\n\n"); while(!v.empty()){ // p - first, l - second int l, p, currentP = v.back().first.first; long long wynik = 0; bool pain = 0; l = v.back().first.second; p = v.back().first.first; //printf("p: %d %d\n", currentP, (int) v.size()); p -= k - 1; while(l > p && v.back().first.first == currentP){ tabwyn[v.back().second] = 0; v.pop_back(); if(v.empty()){ pain = 1; break; } l = v.back().first.second; } if(pain) break; if(v.back().first.first != currentP) continue; while(p-k+1 <= l && v.back().first.first == currentP){ // jedna mozliwosc wynik = max((long long) 0, query(1, 1, base, l, p)); tabwyn[v.back().second] = wynik; wynik = 0; v.pop_back(); if(v.empty()){ pain = 1; break; } l = v.back().first.second; } if(pain) break; if(v.back().first.first != currentP) continue; //laczenie wynik = max((long long) 0, query(1, 1, base, p-k+1, p)); int j = p-k; while(v.back().first.first == currentP){ int tmp = query(1, 1, base, j+k, p); if(j+k <= p) insert(j, tmp); wynik = max(wynik, t[j+base-1]); while(l == j){ tabwyn[v.back().second] = wynik; v.pop_back(); if(v.empty()){ pain = 1; break; } if(v.back().first.first == currentP) l = v.back().first.second; else break; } --j; } //p -= k; l += base-1; p += base-1; while(l != 1 && p != 1){ for(int j = l; j <= p; ++j) t[j] = tree[j]; l >>= 1; p >>= 1; } t[1] = tree[1]; } for(int i = 1; i <= q; ++i) printf("%lld\n", tabwyn[i]); return 0; } |