#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const int SQ = 400;
int N, Q, A[MAXN+10], B[MAXN+10], S;
vector<int> comp;
struct Query
{
int l, r, p;
bool operator < (const Query &p)
{
if(l/SQ==p.l/SQ) return r<p.r;
return l/SQ<p.l/SQ;
}
};
Query query[MAXN+10];
ll tree[MAXN*4+10], ans[MAXN+10];
void update(int node, int tl, int tr, int pos, ll val)
{
if(tl==tr) { tree[node]+=val; return; }
int mid=tl+tr>>1;
if(pos<=mid) update(node*2, tl, mid, pos, val);
else update(node*2+1, mid+1, tr, pos, val);
tree[node]=max(tree[node*2], tree[node*2+1]);
}
ll qquery() { return tree[1]; }
void push(int x) { update(1, 1, S, B[x], A[x]); }
void pop(int x) { update(1, 1, S, B[x], -A[x]); }
int main()
{
int i, j;
scanf("%d%d", &N, &Q);
for(i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end()); S=comp.size();
for(i=1; i<=N; i++) B[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1;
for(i=1; i<=Q; i++) scanf("%d%d", &query[i].l, &query[i].r), query[i].p=i;
sort(query+1, query+Q+1);
int l=1, r=1; push(1);
for(i=1; i<=Q; i++)
{
while(r<query[i].r) r++, push(r);
while(l>query[i].l) l--, push(l);
while(r>query[i].r) pop(r), r--;
while(l<query[i].l) pop(l), l++;
ans[query[i].p]=qquery();
}
for(i=1; i<=Q; i++) printf("%lld\n", ans[i]);
}
Compilation message
historic.cpp: In function 'void update(int, int, int, int, ll)':
historic.cpp:30:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
historic.cpp: In function 'int main()':
historic.cpp:42:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
historic.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~
historic.cpp:45:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
historic.cpp:49:61: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=Q; i++) scanf("%d%d", &query[i].l, &query[i].r), query[i].p=i;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
372 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
6 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
7 ms |
376 KB |
Output is correct |
3 |
Correct |
13 ms |
504 KB |
Output is correct |
4 |
Correct |
19 ms |
504 KB |
Output is correct |
5 |
Correct |
35 ms |
632 KB |
Output is correct |
6 |
Correct |
50 ms |
632 KB |
Output is correct |
7 |
Correct |
56 ms |
632 KB |
Output is correct |
8 |
Correct |
39 ms |
760 KB |
Output is correct |
9 |
Correct |
40 ms |
680 KB |
Output is correct |
10 |
Correct |
74 ms |
632 KB |
Output is correct |
11 |
Correct |
72 ms |
760 KB |
Output is correct |
12 |
Correct |
71 ms |
760 KB |
Output is correct |
13 |
Correct |
72 ms |
760 KB |
Output is correct |
14 |
Correct |
65 ms |
760 KB |
Output is correct |
15 |
Correct |
68 ms |
760 KB |
Output is correct |
16 |
Correct |
45 ms |
636 KB |
Output is correct |
17 |
Correct |
20 ms |
760 KB |
Output is correct |
18 |
Correct |
68 ms |
760 KB |
Output is correct |
19 |
Correct |
75 ms |
760 KB |
Output is correct |
20 |
Correct |
78 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
6 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
9 ms |
504 KB |
Output is correct |
8 |
Correct |
13 ms |
760 KB |
Output is correct |
9 |
Correct |
24 ms |
1144 KB |
Output is correct |
10 |
Correct |
15 ms |
1396 KB |
Output is correct |
11 |
Correct |
77 ms |
5616 KB |
Output is correct |
12 |
Correct |
51 ms |
2412 KB |
Output is correct |
13 |
Correct |
83 ms |
3440 KB |
Output is correct |
14 |
Correct |
114 ms |
5744 KB |
Output is correct |
15 |
Correct |
144 ms |
7668 KB |
Output is correct |
16 |
Correct |
109 ms |
6896 KB |
Output is correct |
17 |
Correct |
46 ms |
3056 KB |
Output is correct |
18 |
Correct |
76 ms |
5488 KB |
Output is correct |
19 |
Correct |
92 ms |
8176 KB |
Output is correct |
20 |
Correct |
56 ms |
5360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
1016 KB |
Output is correct |
2 |
Correct |
334 ms |
1656 KB |
Output is correct |
3 |
Correct |
736 ms |
2412 KB |
Output is correct |
4 |
Correct |
1259 ms |
3188 KB |
Output is correct |
5 |
Correct |
1819 ms |
3700 KB |
Output is correct |
6 |
Correct |
2045 ms |
4524 KB |
Output is correct |
7 |
Correct |
1930 ms |
4928 KB |
Output is correct |
8 |
Correct |
1379 ms |
5872 KB |
Output is correct |
9 |
Correct |
943 ms |
6476 KB |
Output is correct |
10 |
Correct |
310 ms |
6384 KB |
Output is correct |
11 |
Correct |
1293 ms |
6512 KB |
Output is correct |
12 |
Correct |
3261 ms |
6640 KB |
Output is correct |
13 |
Execution timed out |
4021 ms |
5876 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |