#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
int N,A[100002];
LLI bit[100003],bit2[100003];
int update(int i,int num) {
for (i++; i <= N; i += i & (-i)) bit[i] += num;
return 0;
}
LLI query(int s,int e) {
LLI sum = 0;
for (e++; e > 0; e -= e & (-e)) sum += bit[e];
for (; s > 0; s -= s & (-s)) sum -= bit[s];
return sum;
}
set<int> layer[31];
int tree[1 << 18],pre[1 << 18],occ[1 << 18];
int build(int s,int e,int i) {
if (s == e) {
occ[i] = 1,tree[i] = pre[i] = 0;
return 0;
}
int mid = (s+e) / 2;
build(s,mid,2*i),build(mid+1,e,2*i+1);
tree[i] = tree[2*i]+tree[2*i+1];
pre[i] = min(pre[2*i],tree[2*i]+pre[2*i+1]);
occ[i] = 0;
if (pre[i] == pre[2*i]) occ[i] += occ[2*i];
if (pre[i] == tree[2*i]+pre[2*i+1]) occ[i] += occ[2*i+1];
return 0;
}
int update3(int i,int num) {
int u = i+(1 << 17);
tree[u] += num,pre[u] += num;
while (u >= (1 << 12)) {
u >>= 1;
tree[u] = tree[u << 1]+tree[(u << 1)|1];
pre[u] = min(pre[u << 1],tree[u << 1]+pre[(u << 1)|1]);
occ[u] = ((pre[u] == pre[u << 1]) ? occ[u << 1]:0)+((pre[u] == tree[u << 1]+pre[(u << 1)|1]) ? occ[(u << 1)|1]:0);
}
return 0;
}
int arr[1000];
int f = 0;
int toadd[1 << 17],done[1 << 17];
vi all;
int update(int s,int e,int as,int ae,int i,int num) {
if (f == 1) toadd[as] += num,toadd[ae+1] -= num,all.pb(as),all.pb(ae+1);
else update3(as,num),update3(ae+1,-num);
return 0;
}
pair<pii,int> query(int s,int e,int qs,int qe,int i) {
if ((s >= qs) && (e <= qe) && (i >= (1 << 11))) return mp(mp(tree[i],pre[i]),occ[i]);
int mid = (s+e) / 2;
if (qe <= mid) return query(s,mid,qs,qe,2*i);
else if (qs >= mid+1) return query(mid+1,e,qs,qe,2*i+1);
else {
auto l = query(s,mid,qs,qe,2*i),r = query(mid+1,e,qs,qe,2*i+1);
pair<pii,int> p;
p.first.first = l.first.first+r.first.first;
p.first.second = min(l.first.second,l.first.first+r.first.second);
p.second = 0;
if (p.first.second == l.first.second) p.second += l.second;
if (p.first.second == l.first.first+r.first.second) p.second += r.second;
return p;
}
}
int check(int l,int r,int m = -1) {
if ((m != -1) && ((l > m) || (r < m))) return 0;
else return (l < r-1) && (min(A[l],A[r]) > query(l+1,r-1));
}
int update2(int p,int a,int t) {
int i;
for (i = 0; i < 31; i++) {
auto it = layer[i].lower_bound(p);
if (it == layer[i].end()) continue;
if (t == 1) {
if ((it != layer[i].begin()) && check(*prev(it),*it,p)) update(0,N-1,*prev(it)+1,*it-1,0,-1);
if ((next(it) != layer[i].end()) && check(*it,*next(it),p)) update(0,N-1,*it+1,*next(it)-1,0,-1);
if ((it != layer[i].begin()) && (next(it) != layer[i].end()) && check(*prev(it),*next(it),p))
update(0,N-1,*prev(it)+1,*next(it)-1,0,-1);
if ((it != layer[i].begin()) && (prev(it) != layer[i].begin()) && check(*prev(prev(it)),*it,p))
update(0,N-1,*prev(prev(it))+1,*it-1,0,-1);
if ((next(it) != layer[i].end()) && (next(next(it)) != layer[i].end()) && check(*it,*next(next(it)),p))
update(0,N-1,*it+1,*next(next(it))-1,0,-1);
}
if (*it == p) layer[i].erase(it);
}
update(p,a-A[p]),A[p] = a;
for (i = 0; i < 31; i++) {
if (i <= __lg(A[p])) layer[i].insert(p);
auto it = layer[i].lower_bound(p);
if (it == layer[i].end()) continue;
if (t == 1) {
if ((it != layer[i].begin()) && check(*prev(it),*it,p)) update(0,N-1,*prev(it)+1,*it-1,0,1);
if ((next(it) != layer[i].end()) && check(*it,*next(it),p)) update(0,N-1,*it+1,*next(it)-1,0,1);
if ((it != layer[i].begin()) && (next(it) != layer[i].end()) && check(*prev(it),*next(it),p))
update(0,N-1,*prev(it)+1,*next(it)-1,0,1);
if ((it != layer[i].begin()) && (prev(it) != layer[i].begin()) && check(*prev(prev(it)),*it,p))
update(0,N-1,*prev(prev(it))+1,*it-1,0,1);
if ((next(it) != layer[i].end()) && (next(next(it)) != layer[i].end()) && check(*it,*next(next(it)),p))
update(0,N-1,*it+1,*next(next(it))-1,0,1);
}
}
return 0;
}
int main() {
int i;
int Q;
scanf("%d",&N);
mt19937 gen;
clock_t init = clock();
for (i = 1; i <= N; i++) scanf("%d",&A[i]);
A[0] = A[N+1] = 1.1e9,N += 2;
int j;
for (i = 0; i < N; i++) {
for (j = 0; j <= __lg(A[i]); j++) layer[j].insert(i);
}
for (i = 0; i < N; i++) update(i,A[i]);
build(0,(1 << 17)-1,1);
for (i = 0; i < 31; i++) {
for (auto it = layer[i].begin(); it != layer[i].end(); it++) {
if (it != layer[i].begin()) {
if (check(*prev(it),*it)) update(0,N-1,*prev(it)+1,*it-1,0,1);
if (next(it) != layer[i].end()) {
if (check(*prev(it),*next(it))) update(0,N-1,*prev(it)+1,*next(it)-1,0,1);
}
}
}
}
int T,X,Y,L,R;
scanf("%d",&Q);
for (i = 0; i < Q; i++) {
scanf("%d",&T);
//T = 2;
if (T == 1) {
//X = uniform_int_distribution<int>(1,N-2)(gen),Y = uniform_int_distribution<int>(1,1e9)(gen);
scanf("%d %d",&X,&Y);
f = 1;
update2(X,Y,1);
f = 0;
for (int x: all) {
if (toadd[x] != 0) update3(x,toadd[x]),toadd[x] = 0;
}
all.clear();
}
else {
scanf("%d %d",&L,&R);
if ((L == 1) && (R == N-2)) {
auto p = query(0,(1 << 17)-1,L,R,1);
printf("%d\n",p.second);
}
else {
//L = 1+(rand() % N),R = 1+(rand() % N); if (L > R) swap(L,R);
int l = A[L-1],r = A[R+1];
f = 1;
update2(L-1,1.1e9,1),update2(R+1,1.1e9,1);
f = 0;
for (int x: all) {
if (!done[x] && (toadd[x] != 0)) update3(x,toadd[x]),done[x] = 1;
}
auto p = query(0,(1 << 17)-1,L,R,1);
printf("%d\n",p.second);
update2(L-1,l,0),update2(R+1,r,0);
for (int x: all) {
if (done[x] && (toadd[x] != 0)) update3(x,-toadd[x]),done[x] = 0,toadd[x] = 0;
}
all.clear();
}
}
}
//cerr<<(double)(clock()-init)/CLOCKS_PER_SEC<<endl;
return 0;
}
Compilation message
fish2.cpp: In function 'int main()':
fish2.cpp:171:13: warning: unused variable 'init' [-Wunused-variable]
171 | clock_t init = clock();
| ^~~~
fish2.cpp:169:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
169 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
fish2.cpp:172:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
172 | for (i = 1; i <= N; i++) scanf("%d",&A[i]);
| ~~~~~^~~~~~~~~~~~
fish2.cpp:193:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
193 | scanf("%d",&Q);
| ~~~~~^~~~~~~~~
fish2.cpp:195:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
195 | scanf("%d",&T);
| ~~~~~^~~~~~~~~
fish2.cpp:199:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
199 | scanf("%d %d",&X,&Y);
| ~~~~~^~~~~~~~~~~~~~~
fish2.cpp:209:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
209 | scanf("%d %d",&L,&R);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
4 ms |
3364 KB |
Output is correct |
3 |
Correct |
3 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3284 KB |
Output is correct |
5 |
Correct |
16 ms |
3760 KB |
Output is correct |
6 |
Correct |
22 ms |
3632 KB |
Output is correct |
7 |
Correct |
14 ms |
3624 KB |
Output is correct |
8 |
Correct |
17 ms |
3476 KB |
Output is correct |
9 |
Correct |
19 ms |
3580 KB |
Output is correct |
10 |
Correct |
15 ms |
4052 KB |
Output is correct |
11 |
Correct |
22 ms |
4096 KB |
Output is correct |
12 |
Correct |
16 ms |
3812 KB |
Output is correct |
13 |
Correct |
22 ms |
3540 KB |
Output is correct |
14 |
Correct |
12 ms |
3668 KB |
Output is correct |
15 |
Correct |
19 ms |
3772 KB |
Output is correct |
16 |
Correct |
17 ms |
3632 KB |
Output is correct |
17 |
Correct |
13 ms |
3596 KB |
Output is correct |
18 |
Correct |
18 ms |
3556 KB |
Output is correct |
19 |
Correct |
17 ms |
3864 KB |
Output is correct |
20 |
Correct |
21 ms |
3836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3284 KB |
Output is correct |
2 |
Correct |
796 ms |
74828 KB |
Output is correct |
3 |
Correct |
301 ms |
34712 KB |
Output is correct |
4 |
Correct |
793 ms |
74796 KB |
Output is correct |
5 |
Correct |
360 ms |
36428 KB |
Output is correct |
6 |
Correct |
1523 ms |
140424 KB |
Output is correct |
7 |
Correct |
84 ms |
13832 KB |
Output is correct |
8 |
Correct |
1564 ms |
140352 KB |
Output is correct |
9 |
Correct |
84 ms |
13936 KB |
Output is correct |
10 |
Correct |
821 ms |
76584 KB |
Output is correct |
11 |
Correct |
254 ms |
29004 KB |
Output is correct |
12 |
Correct |
143 ms |
21996 KB |
Output is correct |
13 |
Correct |
141 ms |
21904 KB |
Output is correct |
14 |
Correct |
917 ms |
77336 KB |
Output is correct |
15 |
Correct |
969 ms |
77388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
4 ms |
3364 KB |
Output is correct |
3 |
Correct |
3 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3284 KB |
Output is correct |
5 |
Correct |
16 ms |
3760 KB |
Output is correct |
6 |
Correct |
22 ms |
3632 KB |
Output is correct |
7 |
Correct |
14 ms |
3624 KB |
Output is correct |
8 |
Correct |
17 ms |
3476 KB |
Output is correct |
9 |
Correct |
19 ms |
3580 KB |
Output is correct |
10 |
Correct |
15 ms |
4052 KB |
Output is correct |
11 |
Correct |
22 ms |
4096 KB |
Output is correct |
12 |
Correct |
16 ms |
3812 KB |
Output is correct |
13 |
Correct |
22 ms |
3540 KB |
Output is correct |
14 |
Correct |
12 ms |
3668 KB |
Output is correct |
15 |
Correct |
19 ms |
3772 KB |
Output is correct |
16 |
Correct |
17 ms |
3632 KB |
Output is correct |
17 |
Correct |
13 ms |
3596 KB |
Output is correct |
18 |
Correct |
18 ms |
3556 KB |
Output is correct |
19 |
Correct |
17 ms |
3864 KB |
Output is correct |
20 |
Correct |
21 ms |
3836 KB |
Output is correct |
21 |
Correct |
3 ms |
3284 KB |
Output is correct |
22 |
Correct |
796 ms |
74828 KB |
Output is correct |
23 |
Correct |
301 ms |
34712 KB |
Output is correct |
24 |
Correct |
793 ms |
74796 KB |
Output is correct |
25 |
Correct |
360 ms |
36428 KB |
Output is correct |
26 |
Correct |
1523 ms |
140424 KB |
Output is correct |
27 |
Correct |
84 ms |
13832 KB |
Output is correct |
28 |
Correct |
1564 ms |
140352 KB |
Output is correct |
29 |
Correct |
84 ms |
13936 KB |
Output is correct |
30 |
Correct |
821 ms |
76584 KB |
Output is correct |
31 |
Correct |
254 ms |
29004 KB |
Output is correct |
32 |
Correct |
143 ms |
21996 KB |
Output is correct |
33 |
Correct |
141 ms |
21904 KB |
Output is correct |
34 |
Correct |
917 ms |
77336 KB |
Output is correct |
35 |
Correct |
969 ms |
77388 KB |
Output is correct |
36 |
Correct |
944 ms |
78340 KB |
Output is correct |
37 |
Correct |
424 ms |
36392 KB |
Output is correct |
38 |
Correct |
418 ms |
36284 KB |
Output is correct |
39 |
Correct |
932 ms |
78524 KB |
Output is correct |
40 |
Correct |
418 ms |
36012 KB |
Output is correct |
41 |
Correct |
1644 ms |
141224 KB |
Output is correct |
42 |
Correct |
1630 ms |
141296 KB |
Output is correct |
43 |
Correct |
143 ms |
15164 KB |
Output is correct |
44 |
Correct |
141 ms |
14812 KB |
Output is correct |
45 |
Correct |
888 ms |
73772 KB |
Output is correct |
46 |
Correct |
885 ms |
75556 KB |
Output is correct |
47 |
Correct |
315 ms |
26656 KB |
Output is correct |
48 |
Correct |
176 ms |
23144 KB |
Output is correct |
49 |
Correct |
194 ms |
22748 KB |
Output is correct |
50 |
Correct |
951 ms |
78440 KB |
Output is correct |
51 |
Correct |
994 ms |
77916 KB |
Output is correct |
52 |
Correct |
988 ms |
77668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3284 KB |
Output is correct |
2 |
Correct |
796 ms |
74828 KB |
Output is correct |
3 |
Correct |
301 ms |
34712 KB |
Output is correct |
4 |
Correct |
793 ms |
74796 KB |
Output is correct |
5 |
Correct |
360 ms |
36428 KB |
Output is correct |
6 |
Correct |
1523 ms |
140424 KB |
Output is correct |
7 |
Correct |
84 ms |
13832 KB |
Output is correct |
8 |
Correct |
1564 ms |
140352 KB |
Output is correct |
9 |
Correct |
84 ms |
13936 KB |
Output is correct |
10 |
Correct |
821 ms |
76584 KB |
Output is correct |
11 |
Correct |
254 ms |
29004 KB |
Output is correct |
12 |
Correct |
143 ms |
21996 KB |
Output is correct |
13 |
Correct |
141 ms |
21904 KB |
Output is correct |
14 |
Correct |
917 ms |
77336 KB |
Output is correct |
15 |
Correct |
969 ms |
77388 KB |
Output is correct |
16 |
Correct |
4 ms |
3412 KB |
Output is correct |
17 |
Execution timed out |
4061 ms |
36780 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3284 KB |
Output is correct |
2 |
Correct |
796 ms |
74828 KB |
Output is correct |
3 |
Correct |
301 ms |
34712 KB |
Output is correct |
4 |
Correct |
793 ms |
74796 KB |
Output is correct |
5 |
Correct |
360 ms |
36428 KB |
Output is correct |
6 |
Correct |
1523 ms |
140424 KB |
Output is correct |
7 |
Correct |
84 ms |
13832 KB |
Output is correct |
8 |
Correct |
1564 ms |
140352 KB |
Output is correct |
9 |
Correct |
84 ms |
13936 KB |
Output is correct |
10 |
Correct |
821 ms |
76584 KB |
Output is correct |
11 |
Correct |
254 ms |
29004 KB |
Output is correct |
12 |
Correct |
143 ms |
21996 KB |
Output is correct |
13 |
Correct |
141 ms |
21904 KB |
Output is correct |
14 |
Correct |
917 ms |
77336 KB |
Output is correct |
15 |
Correct |
969 ms |
77388 KB |
Output is correct |
16 |
Correct |
5 ms |
3412 KB |
Output is correct |
17 |
Execution timed out |
4083 ms |
75088 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
4 ms |
3364 KB |
Output is correct |
3 |
Correct |
3 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3284 KB |
Output is correct |
5 |
Correct |
16 ms |
3760 KB |
Output is correct |
6 |
Correct |
22 ms |
3632 KB |
Output is correct |
7 |
Correct |
14 ms |
3624 KB |
Output is correct |
8 |
Correct |
17 ms |
3476 KB |
Output is correct |
9 |
Correct |
19 ms |
3580 KB |
Output is correct |
10 |
Correct |
15 ms |
4052 KB |
Output is correct |
11 |
Correct |
22 ms |
4096 KB |
Output is correct |
12 |
Correct |
16 ms |
3812 KB |
Output is correct |
13 |
Correct |
22 ms |
3540 KB |
Output is correct |
14 |
Correct |
12 ms |
3668 KB |
Output is correct |
15 |
Correct |
19 ms |
3772 KB |
Output is correct |
16 |
Correct |
17 ms |
3632 KB |
Output is correct |
17 |
Correct |
13 ms |
3596 KB |
Output is correct |
18 |
Correct |
18 ms |
3556 KB |
Output is correct |
19 |
Correct |
17 ms |
3864 KB |
Output is correct |
20 |
Correct |
21 ms |
3836 KB |
Output is correct |
21 |
Correct |
3 ms |
3284 KB |
Output is correct |
22 |
Correct |
796 ms |
74828 KB |
Output is correct |
23 |
Correct |
301 ms |
34712 KB |
Output is correct |
24 |
Correct |
793 ms |
74796 KB |
Output is correct |
25 |
Correct |
360 ms |
36428 KB |
Output is correct |
26 |
Correct |
1523 ms |
140424 KB |
Output is correct |
27 |
Correct |
84 ms |
13832 KB |
Output is correct |
28 |
Correct |
1564 ms |
140352 KB |
Output is correct |
29 |
Correct |
84 ms |
13936 KB |
Output is correct |
30 |
Correct |
821 ms |
76584 KB |
Output is correct |
31 |
Correct |
254 ms |
29004 KB |
Output is correct |
32 |
Correct |
143 ms |
21996 KB |
Output is correct |
33 |
Correct |
141 ms |
21904 KB |
Output is correct |
34 |
Correct |
917 ms |
77336 KB |
Output is correct |
35 |
Correct |
969 ms |
77388 KB |
Output is correct |
36 |
Correct |
944 ms |
78340 KB |
Output is correct |
37 |
Correct |
424 ms |
36392 KB |
Output is correct |
38 |
Correct |
418 ms |
36284 KB |
Output is correct |
39 |
Correct |
932 ms |
78524 KB |
Output is correct |
40 |
Correct |
418 ms |
36012 KB |
Output is correct |
41 |
Correct |
1644 ms |
141224 KB |
Output is correct |
42 |
Correct |
1630 ms |
141296 KB |
Output is correct |
43 |
Correct |
143 ms |
15164 KB |
Output is correct |
44 |
Correct |
141 ms |
14812 KB |
Output is correct |
45 |
Correct |
888 ms |
73772 KB |
Output is correct |
46 |
Correct |
885 ms |
75556 KB |
Output is correct |
47 |
Correct |
315 ms |
26656 KB |
Output is correct |
48 |
Correct |
176 ms |
23144 KB |
Output is correct |
49 |
Correct |
194 ms |
22748 KB |
Output is correct |
50 |
Correct |
951 ms |
78440 KB |
Output is correct |
51 |
Correct |
994 ms |
77916 KB |
Output is correct |
52 |
Correct |
988 ms |
77668 KB |
Output is correct |
53 |
Correct |
4 ms |
3412 KB |
Output is correct |
54 |
Execution timed out |
4061 ms |
36780 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |