답안 #572066

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
572066 2022-06-03T13:21:01 Z gs14004 Fish 2 (JOI22_fish2) C++17
100 / 100
3566 ms 34020 KB
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long lint;
typedef pair<lint, lint> pi;
const int MAXN = 100005;
const int MAXT = 270000;

lint a[MAXN];

struct node{
	int s, e, cost;
	bool operator<(const node &nd)const{
		return pi(s, -e) < pi(nd.s, -nd.e);
	}
	bool operator==(const node &nd)const{
		return pi(s, -e) == pi(nd.s, -nd.e);
	}
};

struct bit{
	lint tree[MAXN];
	void add(int x, lint v){
		for(int i = x; i < MAXN; i += i & -i) tree[i] += v;
	}
	lint query(int x){
		lint ret = 0;
		for(int i = x; i; i -= i & -i) ret += tree[i];
		return ret;
	}
	lint query(int s, int e){
		if(s > e) return 0;
		return query(e) - query(s - 1);
	}
}bit, subs;

struct seg{
	lint tree[MAXT];
	void init(int s, int e, int p, lint *a){
		if(s == e){
			tree[p] = a[s];
			return;
		}
		int m = (s+e)/2;
		init(s, m, 2*p, a);
		init(m+1, e, 2*p+1, a);
		tree[p] = max(tree[2*p], tree[2*p+1]);
	}
	void upd(int pos, int s, int e, int p, lint v){
		if(s == e){
			tree[p] = v;
			return;
		}
		int m = (s + e) / 2;
		if(pos <= m) upd(pos, s, m, 2*p, v);
		else upd(pos, m+1, e, 2*p+1, v);
		tree[p] = max(tree[2*p], tree[2*p+1]);
	}
	int queryLeft(int s, int e, int ps, int pe, int p, lint v){
		if(e < ps || pe < s) return 1e9;
		if(tree[p] <= v) return 1e9;
		if(ps == pe) return ps;
		int pm = (ps + pe) / 2;
		if(s <= ps && pe <= e){
			if(tree[2*p] > v) return queryLeft(s, e, ps, pm, 2*p, v);
			return queryLeft(s, e, pm+1, pe, 2*p+1, v);
		}
		int r = queryLeft(s, e, ps, pm, 2*p, v);
		if(r < 1e7) return r;
		return queryLeft(s, e, pm+1, pe, 2*p+1, v);
	}
	int queryRight(int s, int e, int ps, int pe, int p, lint v){
		if(e < ps || pe < s) return -1e9;
		if(tree[p] <= v) return -1e9;
		if(ps == pe) return ps;
		int pm = (ps + pe) / 2;
		if(s <= ps && pe <= e){
			if(tree[2*p+1] > v) return queryRight(s, e, pm+1, pe, 2*p+1, v);
			return queryRight(s, e, ps, pm, 2*p, v);
		}
		int r = queryRight(s, e, pm+1, pe, 2*p+1, v);
		if(r > 0) return r;
		return queryRight(s, e, ps, pm, 2*p, v);
	}
}seg;

int n;
set<node> st;

void getNodeSet(int x, vector<node> &v){
	if(x < 2 || x >= n) return;
	int l = x, r = x;
	while(true){
		lint curSum = bit.query(l, r);
		if(curSum < min(a[l-1], a[r+1])){
			v.push_back({l, r, -1});
			if(l == 2 && r == n - 1) break;
			if(a[l-1] < a[r+1]) l--;
			else r++;
		}
		curSum = bit.query(l, r);
		int leftClosest = seg.queryRight(1, l - 1, 1, n, 1, curSum);
		int rightClosest = seg.queryLeft(r + 1, n, 1, n, 1,	curSum);
		l = leftClosest + 1;
		r = rightClosest - 1;
	}
}

void add(int x, int v){
	vector<node> l;
	getNodeSet(x-1, l);
	getNodeSet(x, l);
	getNodeSet(x+1, l);
	sort(all(l));
	reverse(all(l));
	l.resize(unique(all(l)) - l.begin());
	for(auto &x : l){
		auto it = st.lower_bound({x.s, x.e, -1});
		if(it != st.end() && it->s == x.s && it->e == x.e){
			//			cout << "erase interval " << x.s << " " << x.e << " " << it->cost << endl;
			subs.add(it->s - 1, -it->cost);
			st.erase(it);
		}
		else assert(0);
	}
	l.clear();
	bit.add(x, v);
	a[x] += v;
	seg.upd(x, 1, n, 1, a[x]);
	getNodeSet(x-1, l);
	getNodeSet(x, l);
	getNodeSet(x+1, l);
	sort(all(l));
	reverse(all(l));
	l.resize(unique(all(l)) - l.begin());
	for(auto &x : l){
		auto it = st.lower_bound({x.s, x.e, -1});
		if(it != st.end() && it->s == x.s && it->e == x.e){
			assert(0);
		}
		int sum = subs.query(x.s - 1, x.e);
		subs.add(x.s - 1, x.e - x.s + 1 - sum);
		st.insert({x.s, x.e, x.e - x.s + 1 - sum});
		//		cout << "Add interval " << x.s << " " << x.e << " " << x.e - x.s + 1 - sum << endl;
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	a[1] = a[n + 2] = 1e15;
	for(int i = 2; i <= n + 1; i++){
		cin >> a[i];
	}
	n += 2;
	seg.init(1, n, 1, a);
	for(int i = 1; i <= n; i++){
		bit.add(i, a[i]);
	}
	vector<node> l;
	for(int i = 2; i <= n - 1; i++){
		getNodeSet(i, l);
	}
	sort(all(l));
	reverse(all(l));
	l.resize(unique(all(l)) - l.begin());
	for(auto &x : l){
		auto it = st.lower_bound({x.s, x.e, -1});
		if(it != st.end() && it->s == x.s && it->e == x.e){
			assert(0);
		}
		int sum = subs.query(x.s - 1, x.e);
		subs.add(x.s - 1, x.e - x.s + 1 - sum);
		st.insert({x.s, x.e, x.e - x.s + 1 - sum});
		//		cout << "Add interval " << x.s << " " << x.e << " " << x.e - x.s + 1 - sum << endl;
	}
	int q; cin >> q;
	while(q--){
		int t, x, y; cin >> t >> x >> y;
		if(t == 1){
			x++;
			add(x, y - a[x]);
		}
		else{
			x++; y++;
			int sx = x, sy = y;
			for(int i = x; i < y; ){
				int j = seg.queryLeft(i + 1, y, 1, n, 1, bit.query(x, i));
				if(j > 1e8) break;
				if(bit.query(x, j - 1) < a[j]) sx = j;
				i = j;
			}
			for(int i = y; i > x; ){
				int j = seg.queryRight(x, i - 1, 1, n, 1, bit.query(i, y));
				if(j < 0) break;
				if(bit.query(j + 1, y) < a[j]) sy = j;
				i = j;
			}
			cout << sy - sx + 1 - subs.query(sx, sy - 1) << "\n";
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 11 ms 604 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 9 ms 616 KB Output is correct
8 Correct 6 ms 596 KB Output is correct
9 Correct 6 ms 660 KB Output is correct
10 Correct 2 ms 404 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 6 ms 468 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 510 ms 28648 KB Output is correct
3 Correct 591 ms 29364 KB Output is correct
4 Correct 510 ms 28808 KB Output is correct
5 Correct 608 ms 29364 KB Output is correct
6 Correct 90 ms 9420 KB Output is correct
7 Correct 288 ms 16584 KB Output is correct
8 Correct 107 ms 9400 KB Output is correct
9 Correct 282 ms 16476 KB Output is correct
10 Correct 386 ms 17436 KB Output is correct
11 Correct 421 ms 28888 KB Output is correct
12 Correct 131 ms 10560 KB Output is correct
13 Correct 121 ms 10880 KB Output is correct
14 Correct 121 ms 11644 KB Output is correct
15 Correct 134 ms 11704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 11 ms 604 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 9 ms 616 KB Output is correct
8 Correct 6 ms 596 KB Output is correct
9 Correct 6 ms 660 KB Output is correct
10 Correct 2 ms 404 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 6 ms 468 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 510 ms 28648 KB Output is correct
23 Correct 591 ms 29364 KB Output is correct
24 Correct 510 ms 28808 KB Output is correct
25 Correct 608 ms 29364 KB Output is correct
26 Correct 90 ms 9420 KB Output is correct
27 Correct 288 ms 16584 KB Output is correct
28 Correct 107 ms 9400 KB Output is correct
29 Correct 282 ms 16476 KB Output is correct
30 Correct 386 ms 17436 KB Output is correct
31 Correct 421 ms 28888 KB Output is correct
32 Correct 131 ms 10560 KB Output is correct
33 Correct 121 ms 10880 KB Output is correct
34 Correct 121 ms 11644 KB Output is correct
35 Correct 134 ms 11704 KB Output is correct
36 Correct 561 ms 29092 KB Output is correct
37 Correct 639 ms 30024 KB Output is correct
38 Correct 577 ms 28900 KB Output is correct
39 Correct 538 ms 29140 KB Output is correct
40 Correct 585 ms 28984 KB Output is correct
41 Correct 90 ms 9400 KB Output is correct
42 Correct 94 ms 9420 KB Output is correct
43 Correct 301 ms 16544 KB Output is correct
44 Correct 288 ms 16564 KB Output is correct
45 Correct 408 ms 20220 KB Output is correct
46 Correct 382 ms 18076 KB Output is correct
47 Correct 480 ms 16620 KB Output is correct
48 Correct 132 ms 11436 KB Output is correct
49 Correct 123 ms 11032 KB Output is correct
50 Correct 133 ms 11728 KB Output is correct
51 Correct 143 ms 11696 KB Output is correct
52 Correct 152 ms 11592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 510 ms 28648 KB Output is correct
3 Correct 591 ms 29364 KB Output is correct
4 Correct 510 ms 28808 KB Output is correct
5 Correct 608 ms 29364 KB Output is correct
6 Correct 90 ms 9420 KB Output is correct
7 Correct 288 ms 16584 KB Output is correct
8 Correct 107 ms 9400 KB Output is correct
9 Correct 282 ms 16476 KB Output is correct
10 Correct 386 ms 17436 KB Output is correct
11 Correct 421 ms 28888 KB Output is correct
12 Correct 131 ms 10560 KB Output is correct
13 Correct 121 ms 10880 KB Output is correct
14 Correct 121 ms 11644 KB Output is correct
15 Correct 134 ms 11704 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1020 ms 30596 KB Output is correct
18 Correct 872 ms 31400 KB Output is correct
19 Correct 967 ms 32328 KB Output is correct
20 Correct 1020 ms 33532 KB Output is correct
21 Correct 965 ms 30564 KB Output is correct
22 Correct 882 ms 31216 KB Output is correct
23 Correct 986 ms 32276 KB Output is correct
24 Correct 1050 ms 33340 KB Output is correct
25 Correct 965 ms 32632 KB Output is correct
26 Correct 1027 ms 33400 KB Output is correct
27 Correct 224 ms 12128 KB Output is correct
28 Correct 205 ms 12084 KB Output is correct
29 Correct 235 ms 12136 KB Output is correct
30 Correct 616 ms 16676 KB Output is correct
31 Correct 676 ms 16688 KB Output is correct
32 Correct 889 ms 29284 KB Output is correct
33 Correct 601 ms 19424 KB Output is correct
34 Correct 947 ms 29300 KB Output is correct
35 Correct 706 ms 29272 KB Output is correct
36 Correct 848 ms 22124 KB Output is correct
37 Correct 256 ms 12440 KB Output is correct
38 Correct 253 ms 12732 KB Output is correct
39 Correct 262 ms 13904 KB Output is correct
40 Correct 349 ms 14004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 510 ms 28648 KB Output is correct
3 Correct 591 ms 29364 KB Output is correct
4 Correct 510 ms 28808 KB Output is correct
5 Correct 608 ms 29364 KB Output is correct
6 Correct 90 ms 9420 KB Output is correct
7 Correct 288 ms 16584 KB Output is correct
8 Correct 107 ms 9400 KB Output is correct
9 Correct 282 ms 16476 KB Output is correct
10 Correct 386 ms 17436 KB Output is correct
11 Correct 421 ms 28888 KB Output is correct
12 Correct 131 ms 10560 KB Output is correct
13 Correct 121 ms 10880 KB Output is correct
14 Correct 121 ms 11644 KB Output is correct
15 Correct 134 ms 11704 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 3566 ms 28776 KB Output is correct
18 Correct 1773 ms 33720 KB Output is correct
19 Correct 2698 ms 31076 KB Output is correct
20 Correct 1591 ms 33768 KB Output is correct
21 Correct 3216 ms 29396 KB Output is correct
22 Correct 1751 ms 33692 KB Output is correct
23 Correct 3014 ms 31452 KB Output is correct
24 Correct 1640 ms 33848 KB Output is correct
25 Correct 2833 ms 30680 KB Output is correct
26 Correct 488 ms 12144 KB Output is correct
27 Correct 685 ms 11952 KB Output is correct
28 Correct 1026 ms 18460 KB Output is correct
29 Correct 528 ms 12108 KB Output is correct
30 Correct 606 ms 11832 KB Output is correct
31 Correct 1120 ms 18628 KB Output is correct
32 Correct 1374 ms 29152 KB Output is correct
33 Correct 1020 ms 29192 KB Output is correct
34 Correct 1288 ms 24260 KB Output is correct
35 Correct 993 ms 18984 KB Output is correct
36 Correct 1235 ms 29088 KB Output is correct
37 Correct 937 ms 14084 KB Output is correct
38 Correct 715 ms 13612 KB Output is correct
39 Correct 625 ms 13844 KB Output is correct
40 Correct 387 ms 13748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 11 ms 604 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 9 ms 616 KB Output is correct
8 Correct 6 ms 596 KB Output is correct
9 Correct 6 ms 660 KB Output is correct
10 Correct 2 ms 404 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 6 ms 468 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 510 ms 28648 KB Output is correct
23 Correct 591 ms 29364 KB Output is correct
24 Correct 510 ms 28808 KB Output is correct
25 Correct 608 ms 29364 KB Output is correct
26 Correct 90 ms 9420 KB Output is correct
27 Correct 288 ms 16584 KB Output is correct
28 Correct 107 ms 9400 KB Output is correct
29 Correct 282 ms 16476 KB Output is correct
30 Correct 386 ms 17436 KB Output is correct
31 Correct 421 ms 28888 KB Output is correct
32 Correct 131 ms 10560 KB Output is correct
33 Correct 121 ms 10880 KB Output is correct
34 Correct 121 ms 11644 KB Output is correct
35 Correct 134 ms 11704 KB Output is correct
36 Correct 561 ms 29092 KB Output is correct
37 Correct 639 ms 30024 KB Output is correct
38 Correct 577 ms 28900 KB Output is correct
39 Correct 538 ms 29140 KB Output is correct
40 Correct 585 ms 28984 KB Output is correct
41 Correct 90 ms 9400 KB Output is correct
42 Correct 94 ms 9420 KB Output is correct
43 Correct 301 ms 16544 KB Output is correct
44 Correct 288 ms 16564 KB Output is correct
45 Correct 408 ms 20220 KB Output is correct
46 Correct 382 ms 18076 KB Output is correct
47 Correct 480 ms 16620 KB Output is correct
48 Correct 132 ms 11436 KB Output is correct
49 Correct 123 ms 11032 KB Output is correct
50 Correct 133 ms 11728 KB Output is correct
51 Correct 143 ms 11696 KB Output is correct
52 Correct 152 ms 11592 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 1020 ms 30596 KB Output is correct
55 Correct 872 ms 31400 KB Output is correct
56 Correct 967 ms 32328 KB Output is correct
57 Correct 1020 ms 33532 KB Output is correct
58 Correct 965 ms 30564 KB Output is correct
59 Correct 882 ms 31216 KB Output is correct
60 Correct 986 ms 32276 KB Output is correct
61 Correct 1050 ms 33340 KB Output is correct
62 Correct 965 ms 32632 KB Output is correct
63 Correct 1027 ms 33400 KB Output is correct
64 Correct 224 ms 12128 KB Output is correct
65 Correct 205 ms 12084 KB Output is correct
66 Correct 235 ms 12136 KB Output is correct
67 Correct 616 ms 16676 KB Output is correct
68 Correct 676 ms 16688 KB Output is correct
69 Correct 889 ms 29284 KB Output is correct
70 Correct 601 ms 19424 KB Output is correct
71 Correct 947 ms 29300 KB Output is correct
72 Correct 706 ms 29272 KB Output is correct
73 Correct 848 ms 22124 KB Output is correct
74 Correct 256 ms 12440 KB Output is correct
75 Correct 253 ms 12732 KB Output is correct
76 Correct 262 ms 13904 KB Output is correct
77 Correct 349 ms 14004 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 3566 ms 28776 KB Output is correct
80 Correct 1773 ms 33720 KB Output is correct
81 Correct 2698 ms 31076 KB Output is correct
82 Correct 1591 ms 33768 KB Output is correct
83 Correct 3216 ms 29396 KB Output is correct
84 Correct 1751 ms 33692 KB Output is correct
85 Correct 3014 ms 31452 KB Output is correct
86 Correct 1640 ms 33848 KB Output is correct
87 Correct 2833 ms 30680 KB Output is correct
88 Correct 488 ms 12144 KB Output is correct
89 Correct 685 ms 11952 KB Output is correct
90 Correct 1026 ms 18460 KB Output is correct
91 Correct 528 ms 12108 KB Output is correct
92 Correct 606 ms 11832 KB Output is correct
93 Correct 1120 ms 18628 KB Output is correct
94 Correct 1374 ms 29152 KB Output is correct
95 Correct 1020 ms 29192 KB Output is correct
96 Correct 1288 ms 24260 KB Output is correct
97 Correct 993 ms 18984 KB Output is correct
98 Correct 1235 ms 29088 KB Output is correct
99 Correct 937 ms 14084 KB Output is correct
100 Correct 715 ms 13612 KB Output is correct
101 Correct 625 ms 13844 KB Output is correct
102 Correct 387 ms 13748 KB Output is correct
103 Correct 3125 ms 29948 KB Output is correct
104 Correct 1589 ms 31276 KB Output is correct
105 Correct 1310 ms 32936 KB Output is correct
106 Correct 1085 ms 33752 KB Output is correct
107 Correct 2991 ms 30708 KB Output is correct
108 Correct 1563 ms 31248 KB Output is correct
109 Correct 1560 ms 32112 KB Output is correct
110 Correct 1276 ms 34020 KB Output is correct
111 Correct 1269 ms 32608 KB Output is correct
112 Correct 1092 ms 33892 KB Output is correct
113 Correct 525 ms 11960 KB Output is correct
114 Correct 265 ms 12216 KB Output is correct
115 Correct 1125 ms 16284 KB Output is correct
116 Correct 847 ms 17228 KB Output is correct
117 Correct 302 ms 12044 KB Output is correct
118 Correct 653 ms 16932 KB Output is correct
119 Correct 619 ms 11996 KB Output is correct
120 Correct 1062 ms 18404 KB Output is correct
121 Correct 794 ms 17872 KB Output is correct
122 Correct 1388 ms 29084 KB Output is correct
123 Correct 1012 ms 20480 KB Output is correct
124 Correct 911 ms 29088 KB Output is correct
125 Correct 745 ms 28628 KB Output is correct
126 Correct 845 ms 28676 KB Output is correct
127 Correct 948 ms 13084 KB Output is correct
128 Correct 416 ms 13356 KB Output is correct
129 Correct 663 ms 13872 KB Output is correct
130 Correct 417 ms 13928 KB Output is correct