/**
* author: AgentPengin ( Độc cô cầu bại )
* created: 23.12.2022 10:08:02
* too lazy to update time
**/
#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC target("avx","sse2")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define EL '\n'
#define fi first
#define se second
#define NAME "TASK"
#define ll long long
#define lcm(a,b) (a/gcd(a,b))*b
#define db(val) "["#val" = " << (val) << "] "
#define bend(v) (v).begin(),(v).end()
#define sz(v) (int)(v).size()
#define ex exit(0)
using namespace std;
const ll mod = 1e9 + 7;
const int inf = 0x1FFFFFFF;
const int MAXN = 1e5 + 5;
const int BLOCK_SIZE = 400;
struct Queries {
int l,r,id;
} query[MAXN];
int n,a[MAXN],b[MAXN],c[MAXN],q,L = 1,R = 0;
vector<int> values;
ll st[MAXN << 2];
ll ans[MAXN];
void update(int pos,int val,int id = 1,int l = 0,int r = sz(values) - 1) {
if (pos < l || pos > r) return;
if (l == r) {
st[id] += (ll)val;
return;
}
int mid = l + r >> 1;
if (pos <= mid) update(pos,val,id << 1,l,mid);
else update(pos,val,id << 1 | 1,mid + 1,r);
st[id] = max(st[id << 1],st[id << 1 | 1]);
}
bool cmp(Queries A, Queries B){
if (A.l / BLOCK_SIZE != B.l / BLOCK_SIZE) return A.l < B.l;
return A.l / BLOCK_SIZE % 2 ? A.r > B.r : A.r < B.r;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if (ifstream(NAME".inp")) {
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
cin >> n >> q;
for (int i = 1;i <= n;i++) {
cin >> a[i];
values.push_back(a[i]);
}
sort(bend(values));
for (int i = 1;i <= n;i++) {
b[i] = lower_bound(bend(values),a[i]) - values.begin();
c[b[i]] = a[i];
}
for (int i = 1;i <=q ;i++) {
cin >> query[i].l >> query[i].r;
query[i].id = i;
}
sort(query + 1,query + q + 1,cmp);
// sort(query + 1,query + q + 1,[&](const Queries& A,const Queries& B){
// if (A.l / BLOCK_SIZE != B.l / BLOCK_SIZE) return A.l / BLOCK_SIZE < B.l / BLOCK_SIZE;
// else return A.r < B.r;
// });
for (int i = 1;i <= q;i++) {
int l = query[i].l;
int r = query[i].r;
int id = query[i].id;
while(L < l) {
update(b[L],-values[b[L]]);
++L;
}
while(L > l) {
--L;
update(b[L],+values[b[L]]);
}
while(R < r) {
++R;
update(b[R],+values[b[R]]);
}
while(R > r) {
update(b[R],-values[b[R]]);
--R;
}
ans[id] = st[1];
}
for (int i = 1;i <= q;i++) cout << ans[i] << EL;
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
// agent pengin wants to take apio (with anya-san)
Compilation message
historic.cpp: In function 'void update(int, int, int, int, int)':
historic.cpp:53:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = l + r >> 1;
| ~~^~~
historic.cpp: In function 'int main()':
historic.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen(NAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
historic.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen(NAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4532 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4532 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4604 KB |
Output is correct |
18 |
Correct |
4 ms |
4440 KB |
Output is correct |
19 |
Correct |
10 ms |
4648 KB |
Output is correct |
20 |
Correct |
24 ms |
4704 KB |
Output is correct |
21 |
Correct |
39 ms |
4780 KB |
Output is correct |
22 |
Correct |
39 ms |
4700 KB |
Output is correct |
23 |
Correct |
35 ms |
4700 KB |
Output is correct |
24 |
Correct |
41 ms |
4948 KB |
Output is correct |
25 |
Correct |
42 ms |
4700 KB |
Output is correct |
26 |
Correct |
43 ms |
4752 KB |
Output is correct |
27 |
Correct |
42 ms |
4896 KB |
Output is correct |
28 |
Correct |
43 ms |
4752 KB |
Output is correct |
29 |
Correct |
44 ms |
4700 KB |
Output is correct |
30 |
Correct |
47 ms |
4768 KB |
Output is correct |
31 |
Correct |
37 ms |
4760 KB |
Output is correct |
32 |
Correct |
34 ms |
4700 KB |
Output is correct |
33 |
Correct |
43 ms |
4700 KB |
Output is correct |
34 |
Correct |
43 ms |
4760 KB |
Output is correct |
35 |
Correct |
42 ms |
4772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
4 ms |
4700 KB |
Output is correct |
8 |
Correct |
6 ms |
4700 KB |
Output is correct |
9 |
Correct |
14 ms |
4956 KB |
Output is correct |
10 |
Correct |
15 ms |
5088 KB |
Output is correct |
11 |
Correct |
69 ms |
7588 KB |
Output is correct |
12 |
Correct |
45 ms |
5840 KB |
Output is correct |
13 |
Correct |
82 ms |
6612 KB |
Output is correct |
14 |
Correct |
92 ms |
7040 KB |
Output is correct |
15 |
Correct |
110 ms |
7996 KB |
Output is correct |
16 |
Correct |
69 ms |
7260 KB |
Output is correct |
17 |
Correct |
46 ms |
6608 KB |
Output is correct |
18 |
Correct |
71 ms |
7624 KB |
Output is correct |
19 |
Correct |
62 ms |
7636 KB |
Output is correct |
20 |
Correct |
40 ms |
6352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4532 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4604 KB |
Output is correct |
18 |
Correct |
4 ms |
4440 KB |
Output is correct |
19 |
Correct |
10 ms |
4648 KB |
Output is correct |
20 |
Correct |
24 ms |
4704 KB |
Output is correct |
21 |
Correct |
39 ms |
4780 KB |
Output is correct |
22 |
Correct |
39 ms |
4700 KB |
Output is correct |
23 |
Correct |
35 ms |
4700 KB |
Output is correct |
24 |
Correct |
41 ms |
4948 KB |
Output is correct |
25 |
Correct |
42 ms |
4700 KB |
Output is correct |
26 |
Correct |
43 ms |
4752 KB |
Output is correct |
27 |
Correct |
42 ms |
4896 KB |
Output is correct |
28 |
Correct |
43 ms |
4752 KB |
Output is correct |
29 |
Correct |
44 ms |
4700 KB |
Output is correct |
30 |
Correct |
47 ms |
4768 KB |
Output is correct |
31 |
Correct |
37 ms |
4760 KB |
Output is correct |
32 |
Correct |
34 ms |
4700 KB |
Output is correct |
33 |
Correct |
43 ms |
4700 KB |
Output is correct |
34 |
Correct |
43 ms |
4760 KB |
Output is correct |
35 |
Correct |
42 ms |
4772 KB |
Output is correct |
36 |
Correct |
1 ms |
4440 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
2 ms |
4444 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
4 ms |
4700 KB |
Output is correct |
43 |
Correct |
6 ms |
4700 KB |
Output is correct |
44 |
Correct |
14 ms |
4956 KB |
Output is correct |
45 |
Correct |
15 ms |
5088 KB |
Output is correct |
46 |
Correct |
69 ms |
7588 KB |
Output is correct |
47 |
Correct |
45 ms |
5840 KB |
Output is correct |
48 |
Correct |
82 ms |
6612 KB |
Output is correct |
49 |
Correct |
92 ms |
7040 KB |
Output is correct |
50 |
Correct |
110 ms |
7996 KB |
Output is correct |
51 |
Correct |
69 ms |
7260 KB |
Output is correct |
52 |
Correct |
46 ms |
6608 KB |
Output is correct |
53 |
Correct |
71 ms |
7624 KB |
Output is correct |
54 |
Correct |
62 ms |
7636 KB |
Output is correct |
55 |
Correct |
40 ms |
6352 KB |
Output is correct |
56 |
Correct |
86 ms |
4956 KB |
Output is correct |
57 |
Correct |
260 ms |
5320 KB |
Output is correct |
58 |
Correct |
474 ms |
5716 KB |
Output is correct |
59 |
Correct |
719 ms |
6068 KB |
Output is correct |
60 |
Correct |
1022 ms |
6296 KB |
Output is correct |
61 |
Correct |
1336 ms |
6788 KB |
Output is correct |
62 |
Correct |
1694 ms |
7292 KB |
Output is correct |
63 |
Correct |
1484 ms |
7772 KB |
Output is correct |
64 |
Correct |
1559 ms |
8004 KB |
Output is correct |
65 |
Correct |
1714 ms |
8224 KB |
Output is correct |
66 |
Correct |
1825 ms |
8440 KB |
Output is correct |
67 |
Correct |
2749 ms |
8260 KB |
Output is correct |
68 |
Correct |
3075 ms |
8260 KB |
Output is correct |
69 |
Correct |
3185 ms |
10452 KB |
Output is correct |
70 |
Correct |
3127 ms |
10380 KB |
Output is correct |
71 |
Correct |
3187 ms |
10376 KB |
Output is correct |
72 |
Correct |
3163 ms |
10388 KB |
Output is correct |
73 |
Correct |
3112 ms |
10380 KB |
Output is correct |
74 |
Correct |
3133 ms |
10380 KB |
Output is correct |
75 |
Correct |
3153 ms |
10332 KB |
Output is correct |
76 |
Correct |
3184 ms |
10380 KB |
Output is correct |
77 |
Correct |
3160 ms |
10388 KB |
Output is correct |
78 |
Correct |
3180 ms |
10376 KB |
Output is correct |
79 |
Correct |
3265 ms |
10384 KB |
Output is correct |
80 |
Correct |
1701 ms |
10616 KB |
Output is correct |