답안 #538186

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538186 2022-03-16T07:42:46 Z blue Progression (NOI20_progression) C++17
100 / 100
1569 ms 71944 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
using ll = long long;
using vll = vector<ll>;
 
 
int N, Q;
vll diff;
 
 
struct Node
{
	int l, r;
 
	ll lp = 0; //lp (everything below already includes lp)
	int ns = 1; //node size
 
	bool ih = 0; //is homogenous (if homogenous, adjust everything below!)
 
	ll fv = 0; //first value
	int fs = 1; //first size
 
	ll lv = 0; //last value
	int ls = 1; //last size
 
	int ts = 1; //total size
 
	ll sm = 0; //post lp
};
 
Node singlenode(int i)
{
	return {i, i, 0, 1, 1, diff[i], 1, diff[i], 1, 1, diff[i]};
}
 
Node combine(Node& A, Node& B)
{
	Node res;
 
	res.l = A.l;
	res.r = B.r;
 
	res.lp = 0;
	res.ns = A.ns + B.ns;
	res.ih = 0;
 
	res.fv = A.fv;
	if(A.fs == A.ns && B.fv == A.fv)
		res.fs = A.fs + B.fs;
	else
		res.fs = A.fs;
 
	res.lv = B.lv;
	if(B.ls == B.ns && A.lv == B.lv)
		res.ls = A.ls + B.ls;
	else
		res.ls = B.ls;
 
	res.ts = max(A.ts, B.ts);
	if(A.lv == B.fv)
		res.ts = max(res.ts, A.ls + B.fs);
 
	res.sm = A.sm + B.sm;
 
	// cerr << "combine " << A.l << ' ' << A.r << ' ' << A.lv << ' ' << A.ls << " with " << B.l << ' ' << B.r << ' ' << B.fv << ' ' << B.fs << "\n";
	return res;
}
 
Node combine2(Node A, Node B)
{
	return combine(A, B);
}
 
void add(Node& A, ll V)
{
	A.lp += V;
	A.fv += V;
	A.lv += V;
	A.sm += V * A.ns;
}
 
void set(Node& A, ll V)
{
	A.lp = 0;
	A.ih = 1;
 
	A.fv = A.lv = V;
	A.fs = A.ls = A.ts = A.ns;
	A.sm = A.ns * V;
}
 
 
 
struct segtree
{
	Node v;
	segtree* left = NULL;
	segtree* right = NULL;
 
	segtree()
	{
		;
	}
 
	segtree(int L, int R)
	{
		if(L == R) v = singlenode(L);
		else
		{
			left = new segtree(L, (L+R)/2);
			right = new segtree((L+R)/2+1, R);
			v = combine(left->v, right->v);
		}
 
		// cerr << "Node = " << L << ' ' << R << '\n';
		// cerr << v.fv << ' ' << v.fs << "  " << v.lv << ' ' << v.ls << '\n';
		// cerr << v.ts << '\n';
	}
 
	void pushdown()
	{
		if(v.ih)
		{
			v.ih = 0;
			set(left->v, v.fv);
			set(right->v, v.fv);
		}
		else
		{
			add(left->v, v.lp);
			add(right->v, v.lp);
			v.lp = 0;
		}
		v = combine(left->v, right->v);
	}
 
	ll rangesum(int L, int R)
	{
		if(R < v.l || v.r < L) return 0;
		else if(L <= v.l && v.r <= R)
		{
			return v.sm;
		}
		else
		{
			pushdown();
			return left->rangesum(L, R) + right->rangesum(L, R);
		}
	}
 
	Node getRange(int L, int R)
	{
		if(L <= v.l && v.r <= R) return v;
		else 
		{
			pushdown();
			if(L <= left->v.r && right->v.l <= R) return combine2(left->getRange(L, R), right->getRange(L, R));
			else if(L <= left->v.r) return left->getRange(L, R);
			else return right->getRange(L, R);
		}
	}
 
	void rangeadd(int L, int R, ll V)
	{
		if(L <= v.l && v.r <= R) add(v, V);
		else
		{
			pushdown();
			if(L <= left->v.r) left->rangeadd(L, R, V);
			if(right->v.l <= R) right->rangeadd(L, R, V);
			v = combine(left->v, right->v);
		}
	}
 
	void rangeset(int L, int R, ll V)
	{
		// cerr << "range set " << L << ' ' << R << ' ' << V << " at " << 
		if(L <= v.l && v.r <= R) set(v, V);
		else
		{
			pushdown();
			if(L <= left->v.r) left->rangeset(L, R, V);
			if(right->v.l <= R) right->rangeset(L, R, V);
			v = combine(left->v, right->v);
		}
 
		// cerr << "new range " << v.l << ' ' << v.r << " = " << v.fv << ' ' << v.lv << '\n';
 	}
};
 
 
 
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
 
	cin >> N >> Q;
 
	vll D(1+N);
	for(int i = 1; i <= N; i++) cin >> D[i];
 
	// ll adj = D[1];
	// for(int i = 1; i <= N; i++) D[i] -= adj;
 
	if(N == 1)
	{
		for(int j = 1; j <= Q; j++)
		{
			int Z;
			cin >> Z;
 
			int u;
			if(Z <= 2) cin >> u >> u >> u >> u;
			else
			{
				cin >> u >> u;
				cout << 1 << '\n';
			}
		}
		return 0;
	}
 
	diff = vll(1+N);
	for(int i = 1; i <= N-1; i++) diff[i] = D[i+1] - D[i];
 
	segtree ST(1, N-1);
 
	// for(int i = 1; i <= N-1; i++) cerr << diff[i] << ' ';
	// 	cerr << '\n';
 
	// cerr << "done \n";
 
	ll A0 = D[1];
 
	for(int j = 1; j <= Q; j++)
	{
		// cerr << "\n\n\n\n\n\n";
		ll o;
		cin >> o;
 
		if(o == 1)
		{
			ll L, R, S, C;
			cin >> L >> R >> S >> C;
 
			if(L > 1)
				ST.rangeadd(L-1, L-1, S);
 
			if(R < N)
				ST.rangeadd(R, R, -(S + (R-L)*C));
 
			if(L < R)
				ST.rangeadd(L, R-1, C);
 
			if(L == 1) A0 += S;
		}
		else if(o == 2)
		{
			ll L, R, S, C;
			cin >> L >> R >> S >> C;
			// S -= adj;
 
			ll SL = ST.rangesum(1, L-1);
			// ll SR = ST.rangesum(1, R);
			// ll SPL = ST.rangesum(1, L-2);
 
			ll elementR = ST.rangesum(1, R-1) + A0;
 
			if(L > 1)
			{
				// cerr << "A rangeset " << L-1 << ' ' << L-1 << ' ' << S - ST.rangesum(1, L-2) << '\n';
				// ST.rangeset(L-1, L-1, S - (A0 + ST.rangesum(1, L-2)));
				// cerr << "rangesum = " << ST.rangesum(1, L-2) << '\n';
				// cerr << "set " << L-1 << " : " << S - ST.rangesum(1, L-2) << '\n';
 
				ST.rangeadd(L-1, L-1, S - (A0 + SL));
			}
 
			if(R < N)
			{
				// cerr << "right rangesum = " << ST.rangesum(1, R-1) << '\n';
				// cerr << "right diff = " << ST.rangesum(1, R-1) - (S + (R-L)*C) << '\n';
				// cerr << "rangesum = " << ST.rangesum(1, R-1) << '\n';
				// cerr << "right element = " << S+(R-L)*C << '\n';
				// ST.rangeadd(R, R, ST.rangesum(1, R-1) - (S + (R-L)*C));
 
				// cerr << "B rangeset " << R << ' ' << R << " : " << ST.rangesum(1, R) - (S + (R-L)*C) << '\n';
				// cerr << ST.rangesum(1, R) << ' ' << S+(R-L)*C << '\n';
				// ST.rangeset(R, R, A0 + ST.rangesum(1, R) - (S + (R-L)*C));
 
				// ST.rangeset(R, R, (SR - SPL));
				ST.rangeadd(R, R, -(S + (R-L)*C - elementR));
			}
 
			if(L < R)
			{
				// cerr << "ST range set " << L << ' ' << R-1 << ' ' << C << '\n';
				ST.rangeset(L, R-1, C);

				// if(N > 1000 || Q > 1000)
				// {
				// 	ST.rangeset(L, R-1, C);
				// }
				// else
				// {
				// 	for(int f = L; f <= R-1; f++)
				// 		ST.rangeadd(f, f, C - ST.rangesum(f, f));	
				// }

				
			}
 
			if(L == 1) A0 = S;
		}
		else if(o == 3)
		{
			int L, R;
			cin >> L >> R;
 
			if(L == R) cout << 1 << '\n';
			else
			{
				cout << ST.getRange(L, R-1).ts + 1 << '\n';
			}
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 62052 KB Output is correct
2 Correct 81 ms 752 KB Output is correct
3 Correct 77 ms 776 KB Output is correct
4 Correct 76 ms 696 KB Output is correct
5 Correct 97 ms 820 KB Output is correct
6 Correct 75 ms 832 KB Output is correct
7 Correct 90 ms 744 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 163 ms 62060 KB Output is correct
12 Correct 167 ms 61988 KB Output is correct
13 Correct 173 ms 62200 KB Output is correct
14 Correct 155 ms 62260 KB Output is correct
15 Correct 168 ms 62236 KB Output is correct
16 Correct 184 ms 62000 KB Output is correct
17 Correct 156 ms 62012 KB Output is correct
18 Correct 174 ms 61924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 532 KB Output is correct
2 Correct 2 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 532 KB Output is correct
13 Correct 2 ms 524 KB Output is correct
14 Correct 2 ms 524 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 3 ms 468 KB Output is correct
19 Correct 2 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 427 ms 62496 KB Output is correct
2 Correct 95 ms 1016 KB Output is correct
3 Correct 91 ms 1060 KB Output is correct
4 Correct 73 ms 972 KB Output is correct
5 Correct 94 ms 1160 KB Output is correct
6 Correct 92 ms 1120 KB Output is correct
7 Correct 91 ms 1168 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 443 ms 62556 KB Output is correct
12 Correct 439 ms 62600 KB Output is correct
13 Correct 461 ms 62228 KB Output is correct
14 Correct 527 ms 62156 KB Output is correct
15 Correct 371 ms 62476 KB Output is correct
16 Correct 445 ms 62820 KB Output is correct
17 Correct 477 ms 62752 KB Output is correct
18 Correct 500 ms 63052 KB Output is correct
19 Correct 379 ms 62084 KB Output is correct
20 Correct 355 ms 62216 KB Output is correct
21 Correct 397 ms 62060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 825 ms 61972 KB Output is correct
2 Correct 170 ms 612 KB Output is correct
3 Correct 172 ms 620 KB Output is correct
4 Correct 163 ms 612 KB Output is correct
5 Correct 173 ms 696 KB Output is correct
6 Correct 173 ms 612 KB Output is correct
7 Correct 165 ms 660 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 893 ms 61844 KB Output is correct
12 Correct 804 ms 61772 KB Output is correct
13 Correct 963 ms 61804 KB Output is correct
14 Correct 961 ms 61788 KB Output is correct
15 Correct 842 ms 61756 KB Output is correct
16 Correct 913 ms 61968 KB Output is correct
17 Correct 972 ms 61796 KB Output is correct
18 Correct 1050 ms 61936 KB Output is correct
19 Correct 953 ms 61900 KB Output is correct
20 Correct 890 ms 61804 KB Output is correct
21 Correct 955 ms 61692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 427 ms 62496 KB Output is correct
2 Correct 95 ms 1016 KB Output is correct
3 Correct 91 ms 1060 KB Output is correct
4 Correct 73 ms 972 KB Output is correct
5 Correct 94 ms 1160 KB Output is correct
6 Correct 92 ms 1120 KB Output is correct
7 Correct 91 ms 1168 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 443 ms 62556 KB Output is correct
12 Correct 439 ms 62600 KB Output is correct
13 Correct 461 ms 62228 KB Output is correct
14 Correct 527 ms 62156 KB Output is correct
15 Correct 371 ms 62476 KB Output is correct
16 Correct 445 ms 62820 KB Output is correct
17 Correct 477 ms 62752 KB Output is correct
18 Correct 500 ms 63052 KB Output is correct
19 Correct 379 ms 62084 KB Output is correct
20 Correct 355 ms 62216 KB Output is correct
21 Correct 397 ms 62060 KB Output is correct
22 Correct 1276 ms 68520 KB Output is correct
23 Correct 177 ms 1392 KB Output is correct
24 Correct 168 ms 1424 KB Output is correct
25 Correct 153 ms 1356 KB Output is correct
26 Correct 176 ms 1492 KB Output is correct
27 Correct 158 ms 1364 KB Output is correct
28 Correct 166 ms 1400 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1051 ms 65668 KB Output is correct
33 Correct 988 ms 68608 KB Output is correct
34 Correct 1036 ms 65776 KB Output is correct
35 Correct 1152 ms 65784 KB Output is correct
36 Correct 858 ms 65804 KB Output is correct
37 Correct 849 ms 65832 KB Output is correct
38 Correct 877 ms 65784 KB Output is correct
39 Correct 1126 ms 68724 KB Output is correct
40 Correct 1157 ms 68816 KB Output is correct
41 Correct 1063 ms 69012 KB Output is correct
42 Correct 1025 ms 69108 KB Output is correct
43 Correct 979 ms 69224 KB Output is correct
44 Correct 1008 ms 69004 KB Output is correct
45 Correct 982 ms 69304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 62052 KB Output is correct
2 Correct 81 ms 752 KB Output is correct
3 Correct 77 ms 776 KB Output is correct
4 Correct 76 ms 696 KB Output is correct
5 Correct 97 ms 820 KB Output is correct
6 Correct 75 ms 832 KB Output is correct
7 Correct 90 ms 744 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 163 ms 62060 KB Output is correct
12 Correct 167 ms 61988 KB Output is correct
13 Correct 173 ms 62200 KB Output is correct
14 Correct 155 ms 62260 KB Output is correct
15 Correct 168 ms 62236 KB Output is correct
16 Correct 184 ms 62000 KB Output is correct
17 Correct 156 ms 62012 KB Output is correct
18 Correct 174 ms 61924 KB Output is correct
19 Correct 3 ms 532 KB Output is correct
20 Correct 2 ms 328 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 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 3 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 2 ms 532 KB Output is correct
31 Correct 2 ms 524 KB Output is correct
32 Correct 2 ms 524 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 3 ms 468 KB Output is correct
37 Correct 2 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 2 ms 212 KB Output is correct
40 Correct 427 ms 62496 KB Output is correct
41 Correct 95 ms 1016 KB Output is correct
42 Correct 91 ms 1060 KB Output is correct
43 Correct 73 ms 972 KB Output is correct
44 Correct 94 ms 1160 KB Output is correct
45 Correct 92 ms 1120 KB Output is correct
46 Correct 91 ms 1168 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 443 ms 62556 KB Output is correct
51 Correct 439 ms 62600 KB Output is correct
52 Correct 461 ms 62228 KB Output is correct
53 Correct 527 ms 62156 KB Output is correct
54 Correct 371 ms 62476 KB Output is correct
55 Correct 445 ms 62820 KB Output is correct
56 Correct 477 ms 62752 KB Output is correct
57 Correct 500 ms 63052 KB Output is correct
58 Correct 379 ms 62084 KB Output is correct
59 Correct 355 ms 62216 KB Output is correct
60 Correct 397 ms 62060 KB Output is correct
61 Correct 825 ms 61972 KB Output is correct
62 Correct 170 ms 612 KB Output is correct
63 Correct 172 ms 620 KB Output is correct
64 Correct 163 ms 612 KB Output is correct
65 Correct 173 ms 696 KB Output is correct
66 Correct 173 ms 612 KB Output is correct
67 Correct 165 ms 660 KB Output is correct
68 Correct 1 ms 212 KB Output is correct
69 Correct 1 ms 212 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 893 ms 61844 KB Output is correct
72 Correct 804 ms 61772 KB Output is correct
73 Correct 963 ms 61804 KB Output is correct
74 Correct 961 ms 61788 KB Output is correct
75 Correct 842 ms 61756 KB Output is correct
76 Correct 913 ms 61968 KB Output is correct
77 Correct 972 ms 61796 KB Output is correct
78 Correct 1050 ms 61936 KB Output is correct
79 Correct 953 ms 61900 KB Output is correct
80 Correct 890 ms 61804 KB Output is correct
81 Correct 955 ms 61692 KB Output is correct
82 Correct 1276 ms 68520 KB Output is correct
83 Correct 177 ms 1392 KB Output is correct
84 Correct 168 ms 1424 KB Output is correct
85 Correct 153 ms 1356 KB Output is correct
86 Correct 176 ms 1492 KB Output is correct
87 Correct 158 ms 1364 KB Output is correct
88 Correct 166 ms 1400 KB Output is correct
89 Correct 1 ms 340 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 1051 ms 65668 KB Output is correct
93 Correct 988 ms 68608 KB Output is correct
94 Correct 1036 ms 65776 KB Output is correct
95 Correct 1152 ms 65784 KB Output is correct
96 Correct 858 ms 65804 KB Output is correct
97 Correct 849 ms 65832 KB Output is correct
98 Correct 877 ms 65784 KB Output is correct
99 Correct 1126 ms 68724 KB Output is correct
100 Correct 1157 ms 68816 KB Output is correct
101 Correct 1063 ms 69012 KB Output is correct
102 Correct 1025 ms 69108 KB Output is correct
103 Correct 979 ms 69224 KB Output is correct
104 Correct 1008 ms 69004 KB Output is correct
105 Correct 982 ms 69304 KB Output is correct
106 Correct 1379 ms 71592 KB Output is correct
107 Correct 215 ms 3668 KB Output is correct
108 Correct 239 ms 3668 KB Output is correct
109 Correct 212 ms 3644 KB Output is correct
110 Correct 1 ms 332 KB Output is correct
111 Correct 1 ms 212 KB Output is correct
112 Correct 1 ms 212 KB Output is correct
113 Correct 1031 ms 70696 KB Output is correct
114 Correct 1038 ms 70732 KB Output is correct
115 Correct 1159 ms 70700 KB Output is correct
116 Correct 1036 ms 70588 KB Output is correct
117 Correct 1569 ms 71640 KB Output is correct
118 Correct 1291 ms 70664 KB Output is correct
119 Correct 1209 ms 70648 KB Output is correct
120 Correct 602 ms 69296 KB Output is correct
121 Correct 639 ms 69120 KB Output is correct
122 Correct 725 ms 69108 KB Output is correct
123 Correct 510 ms 68320 KB Output is correct
124 Correct 509 ms 68304 KB Output is correct
125 Correct 487 ms 68364 KB Output is correct
126 Correct 1500 ms 68244 KB Output is correct
127 Correct 1452 ms 68368 KB Output is correct
128 Correct 1382 ms 71664 KB Output is correct
129 Correct 1193 ms 68252 KB Output is correct
130 Correct 910 ms 68424 KB Output is correct
131 Correct 915 ms 68288 KB Output is correct
132 Correct 843 ms 68356 KB Output is correct
133 Correct 1219 ms 71696 KB Output is correct
134 Correct 1188 ms 71632 KB Output is correct
135 Correct 1313 ms 71944 KB Output is correct
136 Correct 215 ms 3728 KB Output is correct
137 Correct 279 ms 3628 KB Output is correct
138 Correct 219 ms 3660 KB Output is correct