This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#define N 100001
#define NN 262144
#define SQ 300
#define max2(x,y) (x>y?x:y)
using namespace std;
typedef pair<pair<int, int>, int> ppi;
int n, q_len, M_len, nn, in[N], Md[N];
long long tree[NN], out[N];
ppi Q[N];
set<int> S;
map<int, int> M;
bool cmp(ppi x, ppi y) {
pair<int,int> tx = { x.first.first / SQ,x.first.second / SQ };
pair<int,int> ty = { y.first.first / SQ,y.first.second / SQ };
return tx < ty;
}
void Update(int lev) {
tree[lev] = max2(tree[lev * 2], tree[lev * 2 + 1]);
if (lev > 1) Update(lev / 2);
}
void Push(int x, long long num) {
tree[nn + x - 1] += num;
Update((nn + x - 1) / 2);
}
int main() {
scanf("%d %d", &n, &q_len);
for (int i = 1; i <= n; i++) scanf("%d", &in[i]), S.insert(in[i]);
for (auto i : S) M[i] = ++M_len;
for (int i = 1; i <= n; i++) Md[i] = M[in[i]];
for (nn = 1; nn < M_len; nn *= 2);
for (int i = 1; i <= q_len; i++) {
scanf("%d %d", &Q[i].first.first, &Q[i].first.second);
Q[i].second = i;
}
sort(Q + 1, Q + q_len + 1, cmp);
int l = 1, r = 0;
for (int i = 1; i <= q_len; i++) {
int x = Q[i].first.first, y = Q[i].first.second;
while (r < y) Push(Md[r + 1], in[r + 1]), r++;
while (l > x) Push(Md[l - 1], in[l - 1]), l--;
while (r > y) Push(Md[r], -in[r]), r--;
while (l < x) Push(Md[l], -in[l]), l++;
out[Q[i].second] = tree[1];
}
for (int i = 1; i <= q_len; i++) printf("%lld\n", out[i]);
}
Compilation message (stderr)
historic.cpp: In function 'int main()':
historic.cpp:32:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &q_len);
^
historic.cpp:33:67: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; i++) scanf("%d", &in[i]), S.insert(in[i]);
^
historic.cpp:39:56: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &Q[i].first.first, &Q[i].first.second);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |