#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define Nmax 500001
long long N,Q,A[Nmax],S[Nmax] ={};
bool ss(tuple<long long,long long,long long> a,tuple<long long,long long,long long> b){
return get<0>(a) < get<0>(b);
}
bool operator<(pair<long long,long long> a , pair<long long,long long> b){
if(a.first == b.first) return a.second<b.second;
return a.first<b.first;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
set<pair<long long,long long>> then;
set<pair<long long,long long>> now;
set<pair<long long,long long>> past;
long long then_val = -1;
long long now_val = 0;
now.insert(make_pair(0,0));
cin>>N;
for (long long i = 1;i<=N;i++){
cin>>A[i];
S[i] = S[i-1]+A[i];
//first element to be greater than value;
long long bound = S[i];
auto pointer = now.upper_bound(make_pair(S[i],1000000000000000001));
if(pointer==now.begin()){
auto new_pointer = then.upper_bound(make_pair(S[i],1000000000000000001));
new_pointer--;
long long delta = S[i]-new_pointer->second;
auto it = now.insert(make_pair(S[i]+delta,S[i])).first;
if(it!=now.begin()){
auto comp = prev(it);
if(comp->second > it->second) {now.erase(it); continue;}
}
while(it!=prev(now.end())){
auto checker = next(it);
if (it->second > checker->second){
now.erase(checker);
} else continue;
}
/*if(!past.empty()){
new_pointer = past.upper_bound(make_pair(S[i],1000000000000000001));
new_pointer--;
delta = S[i]-new_pointer->second;
then.insert(make_pair(S[i]+delta,S[i]));}*/
} else{
then_val++;
now_val++;
pointer--;
long long delta = S[i]-pointer->second;
swap(past,then);
swap(then,now);
now.clear();
//cout<<S[i]+delta<<" "<<S[i]<<" "<<pointer->second<<" "<<endl;
now.insert(make_pair(S[i]+delta,S[i]));
/*auto new_pointer = then.upper_bound(make_pair(S[i],1000000000000000001));
new_pointer--;
delta = S[i]-new_pointer->second;
now.insert(make_pair(S[i]+delta,S[i]));
if(!past.empty()){
new_pointer = past.upper_bound(make_pair(S[i],1000000000000000001));
new_pointer--;
delta = S[i]-new_pointer->second;
then.insert(make_pair(S[i]+delta,S[i]));}*/
}
}
cout<<now_val;
}
/*
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
long long l[Nmax];
long long r[Nmax];
long long fi[Nmax] ={};
long long specialloc[Nmax] ={};
long long startspecial[3][Nmax] = {};
long long endspecial[3][Nmax] = {};
long long comlen = 1;
cin>>N>>Q;
cin>>s;
string com;
com += s[0];
fi[1] = 0;
for(long long i = 1;i<=N-1;i++){
if(s[i]!=s[i-1]) {comlen++; com += s[i];}
fi[i+1] = comlen - 1;
}
for(long long i = 1;i<=Q;i++){
cin>>l[i]>>r[i];
l[i] = fi[l[i]];
r[i] = fi[r[i]];
long long ans = 0;
long long delta = r[i]- l[i]+1;
if(com[l[i]]==com[r[i]]) delta--;
while(delta>=4){
delta -=2;
ans++;
}
ans += delta - 1;
cout<<max(ans,(long long)0)<<endl;
}
}
//com += '0';
/*vector<tuple<long long,long long,long long>> special;
for(char i = 'A';i<='C';i++){
long long lastfound = -1;
for (long long j = 0;j<=comlen;j++){
if (j==comlen) com[j]=i;
if(com[j]==i){
if((j-lastfound)>3){
//cout<<j<<" "<<lastfound<<endl;
special.push_back(make_tuple((long long)lastfound+1,(long long)j-1,(long long)(i-'A')));
specialloc[lastfound+1]++;
specialloc[j-1]--;
}
lastfound = j;
}
}
}
sort(special.begin(),special.end(),ss);
for (auto i: special){
long long a,b,c;
tie(a,b,c) = i;
long long need_to_end_at_or_after = a + 2;
long long need_to_start_at_or_before = b - 2;
if(specialloc[a]==0){
endspecial[(long long)(i-'A')][a-1];
} else{
}
if(specialloc[b]==0){
} else{
}
}
//cout<<com<<endl;
*/
Compilation message
segments.cpp:129:5: warning: "/*" within comment [-Wcomment]
129 | /*vector<tuple<long long,long long,long long>> special;
|
segments.cpp: In function 'int main()':
segments.cpp:33:19: warning: unused variable 'bound' [-Wunused-variable]
33 | long long bound = S[i];
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
232 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
232 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
232 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
388 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
232 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
388 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
35 ms |
8508 KB |
Output is correct |
47 |
Correct |
25 ms |
3256 KB |
Output is correct |
48 |
Correct |
18 ms |
2744 KB |
Output is correct |
49 |
Correct |
17 ms |
3328 KB |
Output is correct |
50 |
Correct |
15 ms |
2796 KB |
Output is correct |
51 |
Correct |
24 ms |
2840 KB |
Output is correct |
52 |
Correct |
12 ms |
1612 KB |
Output is correct |
53 |
Correct |
10 ms |
1108 KB |
Output is correct |
54 |
Correct |
19 ms |
2072 KB |
Output is correct |
55 |
Correct |
24 ms |
2928 KB |
Output is correct |
56 |
Correct |
12 ms |
2324 KB |
Output is correct |
57 |
Correct |
19 ms |
4948 KB |
Output is correct |
58 |
Correct |
19 ms |
4336 KB |
Output is correct |
59 |
Correct |
13 ms |
2388 KB |
Output is correct |
60 |
Correct |
16 ms |
2520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
232 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
388 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
35 ms |
8508 KB |
Output is correct |
47 |
Correct |
25 ms |
3256 KB |
Output is correct |
48 |
Correct |
18 ms |
2744 KB |
Output is correct |
49 |
Correct |
17 ms |
3328 KB |
Output is correct |
50 |
Correct |
15 ms |
2796 KB |
Output is correct |
51 |
Correct |
24 ms |
2840 KB |
Output is correct |
52 |
Correct |
12 ms |
1612 KB |
Output is correct |
53 |
Correct |
10 ms |
1108 KB |
Output is correct |
54 |
Correct |
19 ms |
2072 KB |
Output is correct |
55 |
Correct |
24 ms |
2928 KB |
Output is correct |
56 |
Correct |
12 ms |
2324 KB |
Output is correct |
57 |
Correct |
19 ms |
4948 KB |
Output is correct |
58 |
Correct |
19 ms |
4336 KB |
Output is correct |
59 |
Correct |
13 ms |
2388 KB |
Output is correct |
60 |
Correct |
16 ms |
2520 KB |
Output is correct |
61 |
Correct |
203 ms |
41428 KB |
Output is correct |
62 |
Correct |
207 ms |
41288 KB |
Output is correct |
63 |
Correct |
164 ms |
19784 KB |
Output is correct |
64 |
Correct |
82 ms |
11032 KB |
Output is correct |
65 |
Correct |
74 ms |
10956 KB |
Output is correct |
66 |
Correct |
128 ms |
11188 KB |
Output is correct |
67 |
Correct |
97 ms |
9548 KB |
Output is correct |
68 |
Correct |
49 ms |
5364 KB |
Output is correct |
69 |
Correct |
100 ms |
9240 KB |
Output is correct |
70 |
Correct |
126 ms |
11300 KB |
Output is correct |
71 |
Correct |
57 ms |
10188 KB |
Output is correct |
72 |
Correct |
109 ms |
23100 KB |
Output is correct |
73 |
Correct |
81 ms |
15676 KB |
Output is correct |
74 |
Correct |
63 ms |
10956 KB |
Output is correct |
75 |
Correct |
73 ms |
11084 KB |
Output is correct |