답안 #847682

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
847682 2023-09-10T06:52:28 Z qwerasdfzxcl Mizuyokan 2 (JOI23_mizuyokan2) C++17
100 / 100
803 ms 228064 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr int INF1 = 1e9 + 100;

int n;
int a[250250];

int go[250250], sp[250250][20];
ll S[250250];

struct Node{
	array<int, 2> f[51];
	int pos, sz;

	Node(){}
	Node(int _pos, int nxt){
		f[0] = {nxt, 1};
		pos = _pos;
		sz = 1;
	}
};

array<int, 2> operator + (const array<int, 2> &x, int y){
	return {x[0], x[1]+y};
}

void merge(Node &P, const Node &L, const Node &R){
	P.sz = 0;
	for (int i=0;i<L.sz;i++){
		if (L.f[i][0] >= R.pos + R.sz) P.f[P.sz++] = L.f[i];
		else P.f[P.sz++] = R.f[L.f[i][0]-R.pos] + L.f[i][1];
	}

	for (int i=0;i<R.sz;i++){
		if (P.sz==51) return;
		P.f[P.sz++] = R.f[i];
	}
}

void push(Node &L, const Node &R){
	for (int i=0;i<L.sz;i++) if (L.f[i][0] < R.pos + R.sz) L.f[i] = R.f[L.f[i][0]-R.pos] + L.f[i][1];
	for (int i=0;i<R.sz;i++){
		if (L.sz==51) return;
		L.f[L.sz++] = R.f[i];
	}
}

struct Seg{
	Node tree[1101000], ret;

	void init(int i, int l, int r){
		if (l==r) {tree[i] = Node(l, go[l]); return;}
		
		int m = (l+r)>>1;
		init(i<<1, l, m); init(i<<1|1, m+1, r);

		tree[i].pos = l;
		merge(tree[i], tree[i<<1], tree[i<<1|1]);
	}

	void update(int i, int l, int r, int s, int e){
		if (r<s || e<l) return;
		if (l==r){
			tree[i] = Node(l, go[l]);
			return;
		}

		int m = (l+r)>>1;
		update(i<<1, l, m, s, e); update(i<<1|1, m+1, r, s, e);
		merge(tree[i], tree[i<<1], tree[i<<1|1]);
	}

	void query(int i, int l, int r, int s, int e){
		if (r<s || e<l) return;
		if (s<=l && r<=e){
			push(ret, tree[i]);
			return;
		}

		int m = (l+r)>>1;
		query(i<<1, l, m, s, e); query(i<<1|1, m+1, r, s, e);
	}
}tree;

struct Seg2{
	ll tree[500500];
	int sz;

	void init(int n){
		sz = n;
		for (int i=sz;i<sz*2;i++) tree[i] = a[i-sz];
		for (int i=sz-1;i;i--) tree[i] = tree[i<<1] + tree[i<<1|1];
	}

	void update(int p, int x){
		p += sz;
		tree[p] = x;
		for (p>>=1;p;p>>=1) tree[p] = tree[p<<1] + tree[p<<1|1];
	}

	ll query(int l, int r){
		r++;
		ll ret = 0;
		for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
			if (l&1) ret += tree[l++];
			if (r&1) ret += tree[--r];
		}

		return ret;
	}
}tree2;

void init(int n){
	for (int i=1;i<=n;i++) S[i] = S[i-1] + a[i];

	go[n+1] = n+1;
	for (int i=1;i<=n;i++){
		go[i] = n+1;
		for (int j=i+2;j<=min(i+50, n);j++) if (S[j-1] - S[i] > max(a[i], a[j])){
			go[i] = j;
			break;
		}
	}

	for (int i=n;i;i--) go[i] = min(go[i], go[i+1]);
	for (int i=1;i<=n;i++) assert(go[i] - i <= 50);

	tree.init(1, 1, n);
	tree2.init(n+1);
}

int solve(int l, int r){
	if (r-l+1 < 3) return -INF1;
	
	int cnt = 0;
	tree.ret.sz = 0;
	tree.ret.pos = l;
	tree.query(1, 1, n, l, r);

	for (int i=0;i<tree.ret.sz;i++) cnt = max(cnt, tree.ret.f[i][1]-1);

	return cnt * 2 + 1;
}

int query(int l, int r){
	int ret = 1;
	if (l==r) return ret;
	if (tree2.query(l, r-1) > a[r]) ret = 2;
	if (tree2.query(l+1, r) > a[l]) ret = 2;

	int nl = r+1, nr = l-1;
	ll sum = a[l];
	for (int i=l+1;i<=min(l+30, r);sum += a[i++]) if (sum > a[i]) {nl = i; break;}
	sum = a[r];
	for (int i=r-1;i>=max(l, r-30);sum += a[i--]) if (sum > a[i]) {nr = i; break;}
	if (nl <= nr) ret = 3;

	return max({ret, solve(l, r), solve(nl, r) + 1, solve(l, nr) + 1, solve(nl, nr) + 2});
}

void update(int x, int y){
	a[x] = y;
	tree2.update(x, y);

	for (int i=x;i>=max(1, x-50);i--){
		go[i] = go[i+1];
		ll sum = a[i+1];
		for (int j=i+2;j<=min(i+50, go[i+1]-1);sum += a[j++]) if (sum > max(a[i], a[j])){
			go[i] = j;
			break;
		}
	}

	tree.update(1, 1, n, max(1, x-50), x);
}

int main(){
	scanf("%d", &n);
	for (int i=1;i<=n;i++) scanf("%d", a+i);

	init(n);

	int q;
	scanf("%d", &q);
	while(q--){
		int x, y, l, r;
		scanf("%d %d %d %d", &x, &y, &l, &r);
		
		if (a[x]!=y) update(x, y);

		l++;
		printf("%d\n", query(l, r));
	}
}

Compilation message

mizuyokan2.cpp: In function 'int main()':
mizuyokan2.cpp:181:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  181 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
mizuyokan2.cpp:182:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |  for (int i=1;i<=n;i++) scanf("%d", a+i);
      |                         ~~~~~^~~~~~~~~~~
mizuyokan2.cpp:187:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
mizuyokan2.cpp:190:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  190 |   scanf("%d %d %d %d", &x, &y, &l, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 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 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 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 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 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 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 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 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 2 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 2 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6488 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6664 KB Output is correct
36 Correct 1 ms 6556 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 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 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 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 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 2 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 2 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6488 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6664 KB Output is correct
36 Correct 1 ms 6556 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 59 ms 224852 KB Output is correct
42 Correct 55 ms 225104 KB Output is correct
43 Correct 53 ms 224980 KB Output is correct
44 Correct 48 ms 222288 KB Output is correct
45 Correct 56 ms 225192 KB Output is correct
46 Correct 58 ms 225072 KB Output is correct
47 Correct 48 ms 222288 KB Output is correct
48 Correct 56 ms 225108 KB Output is correct
49 Correct 55 ms 225108 KB Output is correct
50 Correct 56 ms 225012 KB Output is correct
51 Correct 59 ms 225264 KB Output is correct
52 Correct 48 ms 222292 KB Output is correct
53 Correct 58 ms 225188 KB Output is correct
54 Correct 56 ms 225104 KB Output is correct
55 Correct 52 ms 225108 KB Output is correct
56 Correct 60 ms 225104 KB Output is correct
57 Correct 51 ms 225108 KB Output is correct
58 Correct 57 ms 225108 KB Output is correct
59 Correct 55 ms 224968 KB Output is correct
60 Correct 54 ms 224952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4540 KB Output is correct
2 Correct 184 ms 224428 KB Output is correct
3 Correct 247 ms 225364 KB Output is correct
4 Correct 275 ms 225368 KB Output is correct
5 Correct 221 ms 225364 KB Output is correct
6 Correct 375 ms 225540 KB Output is correct
7 Correct 391 ms 225652 KB Output is correct
8 Correct 380 ms 225448 KB Output is correct
9 Correct 371 ms 225360 KB Output is correct
10 Correct 352 ms 222804 KB Output is correct
11 Correct 396 ms 225368 KB Output is correct
12 Correct 406 ms 225468 KB Output is correct
13 Correct 396 ms 225264 KB Output is correct
14 Correct 401 ms 225872 KB Output is correct
15 Correct 400 ms 225488 KB Output is correct
16 Correct 422 ms 225380 KB Output is correct
17 Correct 142 ms 225108 KB Output is correct
18 Correct 385 ms 225820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 506 ms 226820 KB Output is correct
4 Correct 558 ms 226672 KB Output is correct
5 Correct 456 ms 227164 KB Output is correct
6 Correct 776 ms 226932 KB Output is correct
7 Correct 751 ms 226896 KB Output is correct
8 Correct 696 ms 226648 KB Output is correct
9 Correct 696 ms 226840 KB Output is correct
10 Correct 778 ms 227268 KB Output is correct
11 Correct 766 ms 226908 KB Output is correct
12 Correct 776 ms 227124 KB Output is correct
13 Correct 776 ms 226612 KB Output is correct
14 Correct 783 ms 226644 KB Output is correct
15 Correct 757 ms 226900 KB Output is correct
16 Correct 320 ms 226716 KB Output is correct
17 Correct 569 ms 227152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 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 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 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 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 2 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 2 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6488 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6664 KB Output is correct
36 Correct 1 ms 6556 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 59 ms 224852 KB Output is correct
42 Correct 55 ms 225104 KB Output is correct
43 Correct 53 ms 224980 KB Output is correct
44 Correct 48 ms 222288 KB Output is correct
45 Correct 56 ms 225192 KB Output is correct
46 Correct 58 ms 225072 KB Output is correct
47 Correct 48 ms 222288 KB Output is correct
48 Correct 56 ms 225108 KB Output is correct
49 Correct 55 ms 225108 KB Output is correct
50 Correct 56 ms 225012 KB Output is correct
51 Correct 59 ms 225264 KB Output is correct
52 Correct 48 ms 222292 KB Output is correct
53 Correct 58 ms 225188 KB Output is correct
54 Correct 56 ms 225104 KB Output is correct
55 Correct 52 ms 225108 KB Output is correct
56 Correct 60 ms 225104 KB Output is correct
57 Correct 51 ms 225108 KB Output is correct
58 Correct 57 ms 225108 KB Output is correct
59 Correct 55 ms 224968 KB Output is correct
60 Correct 54 ms 224952 KB Output is correct
61 Correct 1 ms 4540 KB Output is correct
62 Correct 184 ms 224428 KB Output is correct
63 Correct 247 ms 225364 KB Output is correct
64 Correct 275 ms 225368 KB Output is correct
65 Correct 221 ms 225364 KB Output is correct
66 Correct 375 ms 225540 KB Output is correct
67 Correct 391 ms 225652 KB Output is correct
68 Correct 380 ms 225448 KB Output is correct
69 Correct 371 ms 225360 KB Output is correct
70 Correct 352 ms 222804 KB Output is correct
71 Correct 396 ms 225368 KB Output is correct
72 Correct 406 ms 225468 KB Output is correct
73 Correct 396 ms 225264 KB Output is correct
74 Correct 401 ms 225872 KB Output is correct
75 Correct 400 ms 225488 KB Output is correct
76 Correct 422 ms 225380 KB Output is correct
77 Correct 142 ms 225108 KB Output is correct
78 Correct 385 ms 225820 KB Output is correct
79 Correct 1 ms 4440 KB Output is correct
80 Correct 1 ms 4444 KB Output is correct
81 Correct 506 ms 226820 KB Output is correct
82 Correct 558 ms 226672 KB Output is correct
83 Correct 456 ms 227164 KB Output is correct
84 Correct 776 ms 226932 KB Output is correct
85 Correct 751 ms 226896 KB Output is correct
86 Correct 696 ms 226648 KB Output is correct
87 Correct 696 ms 226840 KB Output is correct
88 Correct 778 ms 227268 KB Output is correct
89 Correct 766 ms 226908 KB Output is correct
90 Correct 776 ms 227124 KB Output is correct
91 Correct 776 ms 226612 KB Output is correct
92 Correct 783 ms 226644 KB Output is correct
93 Correct 757 ms 226900 KB Output is correct
94 Correct 320 ms 226716 KB Output is correct
95 Correct 569 ms 227152 KB Output is correct
96 Correct 420 ms 224116 KB Output is correct
97 Correct 540 ms 227432 KB Output is correct
98 Correct 465 ms 227724 KB Output is correct
99 Correct 785 ms 227408 KB Output is correct
100 Correct 774 ms 227412 KB Output is correct
101 Correct 736 ms 227064 KB Output is correct
102 Correct 705 ms 226988 KB Output is correct
103 Correct 761 ms 227664 KB Output is correct
104 Correct 803 ms 226900 KB Output is correct
105 Correct 774 ms 227132 KB Output is correct
106 Correct 785 ms 227120 KB Output is correct
107 Correct 793 ms 227416 KB Output is correct
108 Correct 802 ms 227028 KB Output is correct
109 Correct 330 ms 228064 KB Output is correct
110 Correct 590 ms 227688 KB Output is correct