Submission #405361

# Submission time Handle Problem Language Result Execution time Memory
405361 2021-05-16T09:47:12 Z oolimry Progression (NOI20_progression) C++17
100 / 100
1572 ms 88232 KB
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) x.size())
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x, y) cerr << #x << " is " << x << ", " << #y << " is " << y << endl;
#define show3(x, y, z) cerr << #x << " is " << x << ", " << #y << " is " << y << ", " << #z << " is " << z << endl;
typedef long long lint;
typedef pair<lint, lint> ii;

struct thing{
	lint leftval, rightval, leftcnt, rightcnt, best, full, sum;
};

inline thing single(int u){
	return {u,u,1,1,1,1,u};
}

inline thing merge(thing L, thing R){
	thing res;
	if(L.full and R.full and L.rightval == R.leftval){
		return {L.leftval, R.rightval, L.leftcnt+R.leftcnt, L.rightcnt+R.rightcnt, L.best+R.best, 1, L.sum+R.sum};
	}
	
	res.leftval = L.leftval, res.rightval = R.rightval;
	res.leftcnt = L.leftcnt, res.rightcnt = R.rightcnt;
	res.best = max(L.best, R.best);
	res.full = 0;
	res.sum = L.sum + R.sum;
	
	if(L.rightval == R.leftval){
		res.best = max(res.best, L.rightcnt + R.leftcnt);
		if(L.full) res.leftcnt += R.leftcnt;
		else if(R.full) res.rightcnt += L.rightcnt;
		res.best = max(res.best, res.leftcnt);
		res.best = max(res.best, res.rightcnt);
	}
	
	return res;
}

lint inf = 1234567890123456;
lint arr[300005];
struct node{
	lint s, e, m;
	thing val;
	lint add = 0;
	lint rewrite = inf;
	node *l, *r;
	
	node(int S, int E){
		s = S, e = E, m = (s+e)/2;
		if(s == e) val = single(arr[s]);
		else{
			l = new node(s,m);
			r = new node(m+1,e);
			val = merge(l->val, r->val);
			add = 0; rewrite = inf;
		}	
	}
	
	void applyadd(lint A){
		val.leftval += A;
		val.rightval += A;
		val.sum += (e-s+1)*A;
		add += A;
	}
	
	void applyrewrite(lint W){
		if(W < inf / 100){
			val.leftval = W;
			val.rightval = W;
			val.leftcnt = (e-s+1);
			val.rightcnt = (e-s+1);
			val.best = (e-s+1);
			val.full = 1;
			val.sum = (e-s+1)*W;
			add = 0;
			rewrite = W;
		}
	}
	
	void push(){
		if(rewrite != inf and s != e){
			l->applyrewrite(rewrite);
			r->applyrewrite(rewrite);
			rewrite = inf;
		}
		if(add != 0 and s != e){
			l->applyadd(add);
			r->applyadd(add);
			add = 0;
		}
	}
	
	thing query(int S, int E){
		push();
		if(s == S and E == e) return val;
		else if(E <= m) return l->query(S,E);
		else if(S >= m+1) return r->query(S,E);
		else return merge(l->query(S,m), r->query(m+1,E));
	} 
	
	void updateadd(int S, int E, lint A){
		push();
		if(s == S and E == e){
			applyadd(A);
			push();
		}
		else{
			if(E <= m) l->updateadd(S,E,A);
			else if(S >= m+1) r->updateadd(S,E,A);
			else l->updateadd(S,m,A), r->updateadd(m+1,E,A);
			val = merge(l->val, r->val);
		}
	}
	
	void updaterewrite(int S, int E, lint W){
		push();
		if(s == S and E == e){
			applyrewrite(W);
			push();
		}
		else{
			if(E <= m) l->updaterewrite(S,E,W);
			else if(S >= m+1) r->updaterewrite(S,E,W);
			else l->updaterewrite(S,m,W), r->updaterewrite(m+1,E,W);
			val = merge(l->val, r->val);
		}
	}
} *root;


int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	int n, Q; cin >> n >> Q;
	lint x; cin >> x;
	lint firstval = x;
	
	for(int i = 2;i <= n;i++){
		lint y; cin >> y;
		arr[i-1] = y-x;
		x = y;
	}
	
	if(n != 1) root = new node(1,n-1);
	
	while(Q--){
		lint t, l, r; cin >> t >> l >> r;
		if(t == 3){
			if(l == r) cout << "1\n";
			else cout << root->query(l,r-1).best+1 << '\n';
		}
		else{
			lint a, b; cin >> a >> b;
			if(n == 1) continue;
			if(t == 1){
				if(l != r) root->updateadd(l,r-1,b);
				if(l != 1) root->updateadd(l-1,l-1,a);
				if(r != n) root->updateadd(r,r,-((r-l)*b+a));
				
				if(l == 1) firstval += a;
			}
			else{
				if(r != n){
					lint nxtval = firstval;
					nxtval += root->query(1,r).sum;
					//show2(firstval,nxtval);
					root->updaterewrite(r,r,nxtval-((r-l)*b+a));
				}
				if(l != 1){
					lint preval = firstval;
					if(l != 2) preval += root->query(1,l-2).sum;
					//show2(preval, firstval);
					root->updaterewrite(l-1,l-1,a-preval);
				}
				if(l != r) root->updaterewrite(l,r-1,b);
				
				if(l == 1) firstval = a;
			}
		}
		
		//for(int i = 1;i <= n-1;i++) cout << root->query(i,i).leftval << " "; cout << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 212 ms 78452 KB Output is correct
2 Correct 112 ms 496 KB Output is correct
3 Correct 109 ms 580 KB Output is correct
4 Correct 120 ms 504 KB Output is correct
5 Correct 108 ms 664 KB Output is correct
6 Correct 109 ms 600 KB Output is correct
7 Correct 110 ms 484 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 245 ms 78328 KB Output is correct
12 Correct 215 ms 78260 KB Output is correct
13 Correct 216 ms 78556 KB Output is correct
14 Correct 213 ms 78660 KB Output is correct
15 Correct 209 ms 78668 KB Output is correct
16 Correct 247 ms 78328 KB Output is correct
17 Correct 238 ms 78284 KB Output is correct
18 Correct 218 ms 78340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 2 ms 328 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 308 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 3 ms 588 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 2 ms 588 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 3 ms 588 KB Output is correct
17 Correct 3 ms 588 KB Output is correct
18 Correct 3 ms 588 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 369 ms 78772 KB Output is correct
2 Correct 99 ms 852 KB Output is correct
3 Correct 94 ms 900 KB Output is correct
4 Correct 84 ms 856 KB Output is correct
5 Correct 102 ms 936 KB Output is correct
6 Correct 114 ms 972 KB Output is correct
7 Correct 104 ms 888 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 444 ms 78472 KB Output is correct
12 Correct 377 ms 78788 KB Output is correct
13 Correct 440 ms 78680 KB Output is correct
14 Correct 439 ms 78532 KB Output is correct
15 Correct 379 ms 78744 KB Output is correct
16 Correct 444 ms 79120 KB Output is correct
17 Correct 449 ms 79232 KB Output is correct
18 Correct 440 ms 79344 KB Output is correct
19 Correct 368 ms 78404 KB Output is correct
20 Correct 394 ms 78496 KB Output is correct
21 Correct 369 ms 78520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 775 ms 78112 KB Output is correct
2 Correct 148 ms 460 KB Output is correct
3 Correct 150 ms 472 KB Output is correct
4 Correct 145 ms 488 KB Output is correct
5 Correct 152 ms 452 KB Output is correct
6 Correct 150 ms 492 KB Output is correct
7 Correct 149 ms 472 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 754 ms 78092 KB Output is correct
12 Correct 779 ms 78048 KB Output is correct
13 Correct 775 ms 78084 KB Output is correct
14 Correct 768 ms 77992 KB Output is correct
15 Correct 708 ms 78116 KB Output is correct
16 Correct 782 ms 78236 KB Output is correct
17 Correct 819 ms 78228 KB Output is correct
18 Correct 800 ms 78128 KB Output is correct
19 Correct 751 ms 78244 KB Output is correct
20 Correct 746 ms 78020 KB Output is correct
21 Correct 766 ms 78028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 369 ms 78772 KB Output is correct
2 Correct 99 ms 852 KB Output is correct
3 Correct 94 ms 900 KB Output is correct
4 Correct 84 ms 856 KB Output is correct
5 Correct 102 ms 936 KB Output is correct
6 Correct 114 ms 972 KB Output is correct
7 Correct 104 ms 888 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 444 ms 78472 KB Output is correct
12 Correct 377 ms 78788 KB Output is correct
13 Correct 440 ms 78680 KB Output is correct
14 Correct 439 ms 78532 KB Output is correct
15 Correct 379 ms 78744 KB Output is correct
16 Correct 444 ms 79120 KB Output is correct
17 Correct 449 ms 79232 KB Output is correct
18 Correct 440 ms 79344 KB Output is correct
19 Correct 368 ms 78404 KB Output is correct
20 Correct 394 ms 78496 KB Output is correct
21 Correct 369 ms 78520 KB Output is correct
22 Correct 1200 ms 78224 KB Output is correct
23 Correct 158 ms 588 KB Output is correct
24 Correct 147 ms 492 KB Output is correct
25 Correct 160 ms 580 KB Output is correct
26 Correct 149 ms 576 KB Output is correct
27 Correct 167 ms 580 KB Output is correct
28 Correct 150 ms 584 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1184 ms 78384 KB Output is correct
33 Correct 1078 ms 78296 KB Output is correct
34 Correct 1127 ms 78256 KB Output is correct
35 Correct 1137 ms 78304 KB Output is correct
36 Correct 980 ms 78444 KB Output is correct
37 Correct 982 ms 78528 KB Output is correct
38 Correct 961 ms 78596 KB Output is correct
39 Correct 1141 ms 78144 KB Output is correct
40 Correct 1148 ms 78372 KB Output is correct
41 Correct 1192 ms 78328 KB Output is correct
42 Correct 1177 ms 78352 KB Output is correct
43 Correct 1105 ms 78148 KB Output is correct
44 Correct 1088 ms 78440 KB Output is correct
45 Correct 1101 ms 78236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 78452 KB Output is correct
2 Correct 112 ms 496 KB Output is correct
3 Correct 109 ms 580 KB Output is correct
4 Correct 120 ms 504 KB Output is correct
5 Correct 108 ms 664 KB Output is correct
6 Correct 109 ms 600 KB Output is correct
7 Correct 110 ms 484 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 245 ms 78328 KB Output is correct
12 Correct 215 ms 78260 KB Output is correct
13 Correct 216 ms 78556 KB Output is correct
14 Correct 213 ms 78660 KB Output is correct
15 Correct 209 ms 78668 KB Output is correct
16 Correct 247 ms 78328 KB Output is correct
17 Correct 238 ms 78284 KB Output is correct
18 Correct 218 ms 78340 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 328 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 2 ms 308 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 3 ms 588 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 3 ms 588 KB Output is correct
31 Correct 2 ms 588 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 3 ms 588 KB Output is correct
34 Correct 3 ms 588 KB Output is correct
35 Correct 3 ms 588 KB Output is correct
36 Correct 3 ms 588 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 369 ms 78772 KB Output is correct
41 Correct 99 ms 852 KB Output is correct
42 Correct 94 ms 900 KB Output is correct
43 Correct 84 ms 856 KB Output is correct
44 Correct 102 ms 936 KB Output is correct
45 Correct 114 ms 972 KB Output is correct
46 Correct 104 ms 888 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 444 ms 78472 KB Output is correct
51 Correct 377 ms 78788 KB Output is correct
52 Correct 440 ms 78680 KB Output is correct
53 Correct 439 ms 78532 KB Output is correct
54 Correct 379 ms 78744 KB Output is correct
55 Correct 444 ms 79120 KB Output is correct
56 Correct 449 ms 79232 KB Output is correct
57 Correct 440 ms 79344 KB Output is correct
58 Correct 368 ms 78404 KB Output is correct
59 Correct 394 ms 78496 KB Output is correct
60 Correct 369 ms 78520 KB Output is correct
61 Correct 775 ms 78112 KB Output is correct
62 Correct 148 ms 460 KB Output is correct
63 Correct 150 ms 472 KB Output is correct
64 Correct 145 ms 488 KB Output is correct
65 Correct 152 ms 452 KB Output is correct
66 Correct 150 ms 492 KB Output is correct
67 Correct 149 ms 472 KB Output is correct
68 Correct 1 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 754 ms 78092 KB Output is correct
72 Correct 779 ms 78048 KB Output is correct
73 Correct 775 ms 78084 KB Output is correct
74 Correct 768 ms 77992 KB Output is correct
75 Correct 708 ms 78116 KB Output is correct
76 Correct 782 ms 78236 KB Output is correct
77 Correct 819 ms 78228 KB Output is correct
78 Correct 800 ms 78128 KB Output is correct
79 Correct 751 ms 78244 KB Output is correct
80 Correct 746 ms 78020 KB Output is correct
81 Correct 766 ms 78028 KB Output is correct
82 Correct 1200 ms 78224 KB Output is correct
83 Correct 158 ms 588 KB Output is correct
84 Correct 147 ms 492 KB Output is correct
85 Correct 160 ms 580 KB Output is correct
86 Correct 149 ms 576 KB Output is correct
87 Correct 167 ms 580 KB Output is correct
88 Correct 150 ms 584 KB Output is correct
89 Correct 1 ms 204 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 1184 ms 78384 KB Output is correct
93 Correct 1078 ms 78296 KB Output is correct
94 Correct 1127 ms 78256 KB Output is correct
95 Correct 1137 ms 78304 KB Output is correct
96 Correct 980 ms 78444 KB Output is correct
97 Correct 982 ms 78528 KB Output is correct
98 Correct 961 ms 78596 KB Output is correct
99 Correct 1141 ms 78144 KB Output is correct
100 Correct 1148 ms 78372 KB Output is correct
101 Correct 1192 ms 78328 KB Output is correct
102 Correct 1177 ms 78352 KB Output is correct
103 Correct 1105 ms 78148 KB Output is correct
104 Correct 1088 ms 78440 KB Output is correct
105 Correct 1101 ms 78236 KB Output is correct
106 Correct 1552 ms 88052 KB Output is correct
107 Correct 195 ms 3636 KB Output is correct
108 Correct 194 ms 3640 KB Output is correct
109 Correct 193 ms 3652 KB Output is correct
110 Correct 1 ms 332 KB Output is correct
111 Correct 1 ms 340 KB Output is correct
112 Correct 1 ms 332 KB Output is correct
113 Correct 1159 ms 87196 KB Output is correct
114 Correct 1200 ms 87196 KB Output is correct
115 Correct 1183 ms 87100 KB Output is correct
116 Correct 1055 ms 87028 KB Output is correct
117 Correct 1545 ms 88104 KB Output is correct
118 Correct 1065 ms 87236 KB Output is correct
119 Correct 1050 ms 87172 KB Output is correct
120 Correct 430 ms 85712 KB Output is correct
121 Correct 438 ms 85608 KB Output is correct
122 Correct 448 ms 85656 KB Output is correct
123 Correct 363 ms 84708 KB Output is correct
124 Correct 384 ms 84804 KB Output is correct
125 Correct 372 ms 84804 KB Output is correct
126 Correct 1505 ms 84628 KB Output is correct
127 Correct 1562 ms 84736 KB Output is correct
128 Correct 1530 ms 88232 KB Output is correct
129 Correct 1510 ms 84920 KB Output is correct
130 Correct 1130 ms 84804 KB Output is correct
131 Correct 1145 ms 84768 KB Output is correct
132 Correct 1137 ms 84800 KB Output is correct
133 Correct 1494 ms 88088 KB Output is correct
134 Correct 1572 ms 88100 KB Output is correct
135 Correct 1537 ms 88132 KB Output is correct
136 Correct 196 ms 3732 KB Output is correct
137 Correct 195 ms 3648 KB Output is correct
138 Correct 194 ms 3628 KB Output is correct