답안 #553815

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
553815 2022-04-27T06:47:49 Z amunduzbaev Fish 2 (JOI22_fish2) C++17
31 / 100
281 ms 40660 KB
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define ar array

const int N = 1e5 + 5;
const int inf = 1e9;

struct ST{
	ar<int, 2> tree[N << 2];
	int p[N << 2];
	
	void push(int x, int lx, int rx){
		if(lx == rx) return;
		tree[x<<1][0] += p[x], p[x<<1] += p[x];
		tree[x<<1|1][0] += p[x], p[x<<1|1] += p[x];
		p[x] = 0;
	}
	
	void add(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return;
		if(lx >= l && rx <= r){
			tree[x][0] += v;
			p[x] += v;
			return;
		} int m = (lx + rx) >> 1;
		push(x, lx, rx);
		add(l, r, v, lx, m, x<<1);
		add(l, r, v, m+1, rx, x<<1|1);
		if(tree[x<<1][0] == tree[x<<1|1][0]) tree[x] = {tree[x<<1][0], tree[x<<1][1] + tree[x<<1|1][1]};
		else tree[x] = min(tree[x<<1], tree[x<<1|1]);
	}
	
	ar<int, 2> get(int l, int r, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return {N, N};
		if(lx >= l && rx <= r){
			return tree[x];
		}
		push(x, lx, rx);
		int m = (lx + rx) >> 1;
		auto L = get(l, r, lx, m, x<<1), R = get(l, r, m+1, rx, x<<1|1);
		if(L[0] == R[0]) return {L[0], L[1] + R[1]};
		else return min(L, R);
	}
	
	void build(int lx = 0, int rx = N, int x = 1){
		tree[x][1] = rx - lx + 1;
		if(lx == rx) return;
		int m = (lx + rx) >> 1;
		build(lx, m, x<<1);
		build(m+1, rx, x<<1|1);
	}
}tree;

int a[N];
int pref[N], par[N][2], p2[N][2];
int st[N][20];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	tree.build();
	int n; cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		pref[i] = pref[i-1] + a[i];
	}
	a[0] = a[n+1] = inf * N;
	
	for(int i=0;i<=n+1;i++) st[i][0] = a[i];
	for(int j=1;j<20;j++){
		for(int i=0;i+(1 << (j-1))<N;i++){
			st[i][j] = max(st[i][j-1], st[i+(1 << (j-1))][j-1]);
		}
	}
	
	auto get = [&](int l, int r){
		int lg = __lg(r - l + 1);
		return max(st[l][lg], st[r - (1 << lg) + 1][lg]);
	};
	
	for(int i=1;i<=n;i++){
		{ int l = 0, r = i;
			while(l < r){
				int m = (l + r + 1) >> 1;
				if(get(m, i) > a[i]) l = m;
				else r = m - 1;
			} par[i][0] = l;
		} 
		{ int l = 0, r = i;
			while(l < r){
				int m = (l + r + 1) >> 1;
				if(get(m, i) >= a[i] * 2) l = m;
				else r = m - 1;
			} p2[i][0] = l;
		}
		{ int l = i, r = n + 1;
			while(l < r){
				int m = (l + r) >> 1;
				if(get(i, m) > a[i]) r = m;
				else l = m + 1;
			} par[i][1] = l;
		}
		{ int l = i, r = n + 1;
			while(l < r){
				int m = (l + r) >> 1;
				if(get(i, m) >= a[i] * 2) r = m;
				else l = m + 1;
			} p2[i][1] = l;
		}
	}
	
	set<ar<int, 2>> ss;
	auto del = [&](int l, int r){
		tree.add(l, r, -1);
		ss.erase({l, r});
	};
	
	auto add = [&](int l, int r){
		tree.add(l, r, 1);
		ss.insert({l, r});
	};
	
	auto check = [&](int l, int r){
		if(a[r+1] > pref[r] - pref[l-1] && a[l-1] > pref[r] - pref[l-1]){
			add(l, r);
			return true;
		} return false;
	};
	par[n+1][1] = N, par[0][0] = 0;
	for(int i=1;i<=n;i++){
		int j = par[i][1];
		int mx = i;
		while(j-1<=n-(i == 1)){
			// cout<<mx<<" "<<j<<endl;
			check(i, j-1);
			if(p2[mx][1] == j){
				mx = j;
				j = par[j][1];
			} else {
				int v = j;
				j = p2[mx][1];
				mx = v;
			}
		}
	}
	
	// for(auto x : ss){
	// 	cout<<x[0]<<" "<<x[1]<<"\n";
	// }
	// cout<<"\n";
	
	int q; cin>>q;
	while(q--){
		int t; cin>>t;
		if(t == 1){
			assert(false);
		} else {
			int l, r; cin>>l>>r;
			int L = l, R = r;
			{
				int j=par[l][1], mx = l;
				while(j<=r){
					if(pref[j-1] - pref[l-1] < a[j]) L = j;
					if(p2[mx][1] == j){
						mx = j;
						j = par[j][1];
					} else {
						int v = j;
						j = p2[mx][1];
						mx = v;
					}
				}
			}
			
			{
				int j=par[r][0], mx = r;
				while(l <= j){
					if(pref[r] - pref[j] < a[j]) R = j;
					if(p2[mx][0] == j){
						mx = j;
						j = par[j][0];
					} else {
						int v = j;
						j = p2[mx][0];
						mx = v;
					}
				}
			}
			// cout<<L<<" "<<R<<"\n";
			cout<<tree.get(L, R)[1]<<"\n";
		}
	}
}

/*

5
6 4 2 2 6
2
2 1 5 
2 1 3

*/

Compilation message

fish2.cpp: In function 'int main()':
fish2.cpp:115:7: warning: variable 'del' set but not used [-Wunused-but-set-variable]
  115 |  auto del = [&](int l, int r){
      |       ^~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 46 ms 40660 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 20052 KB Output is correct
2 Correct 128 ms 32132 KB Output is correct
3 Correct 115 ms 31488 KB Output is correct
4 Correct 119 ms 32132 KB Output is correct
5 Correct 111 ms 31592 KB Output is correct
6 Correct 100 ms 29516 KB Output is correct
7 Correct 86 ms 28348 KB Output is correct
8 Correct 97 ms 29448 KB Output is correct
9 Correct 88 ms 28380 KB Output is correct
10 Correct 95 ms 29288 KB Output is correct
11 Correct 91 ms 29072 KB Output is correct
12 Correct 105 ms 29260 KB Output is correct
13 Correct 105 ms 29272 KB Output is correct
14 Correct 101 ms 30976 KB Output is correct
15 Correct 105 ms 30928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 46 ms 40660 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 20052 KB Output is correct
2 Correct 128 ms 32132 KB Output is correct
3 Correct 115 ms 31488 KB Output is correct
4 Correct 119 ms 32132 KB Output is correct
5 Correct 111 ms 31592 KB Output is correct
6 Correct 100 ms 29516 KB Output is correct
7 Correct 86 ms 28348 KB Output is correct
8 Correct 97 ms 29448 KB Output is correct
9 Correct 88 ms 28380 KB Output is correct
10 Correct 95 ms 29288 KB Output is correct
11 Correct 91 ms 29072 KB Output is correct
12 Correct 105 ms 29260 KB Output is correct
13 Correct 105 ms 29272 KB Output is correct
14 Correct 101 ms 30976 KB Output is correct
15 Correct 105 ms 30928 KB Output is correct
16 Correct 28 ms 20092 KB Output is correct
17 Correct 212 ms 33540 KB Output is correct
18 Correct 258 ms 35156 KB Output is correct
19 Correct 215 ms 33832 KB Output is correct
20 Correct 224 ms 33676 KB Output is correct
21 Correct 211 ms 33620 KB Output is correct
22 Correct 229 ms 35148 KB Output is correct
23 Correct 203 ms 33416 KB Output is correct
24 Correct 212 ms 33904 KB Output is correct
25 Correct 212 ms 33860 KB Output is correct
26 Correct 217 ms 33996 KB Output is correct
27 Correct 193 ms 32400 KB Output is correct
28 Correct 216 ms 32468 KB Output is correct
29 Correct 230 ms 32352 KB Output is correct
30 Correct 168 ms 30156 KB Output is correct
31 Correct 174 ms 30144 KB Output is correct
32 Correct 215 ms 31176 KB Output is correct
33 Correct 219 ms 31572 KB Output is correct
34 Correct 193 ms 30688 KB Output is correct
35 Correct 172 ms 30120 KB Output is correct
36 Correct 216 ms 31876 KB Output is correct
37 Correct 173 ms 31084 KB Output is correct
38 Correct 193 ms 30980 KB Output is correct
39 Correct 281 ms 33532 KB Output is correct
40 Correct 218 ms 33340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 20052 KB Output is correct
2 Correct 128 ms 32132 KB Output is correct
3 Correct 115 ms 31488 KB Output is correct
4 Correct 119 ms 32132 KB Output is correct
5 Correct 111 ms 31592 KB Output is correct
6 Correct 100 ms 29516 KB Output is correct
7 Correct 86 ms 28348 KB Output is correct
8 Correct 97 ms 29448 KB Output is correct
9 Correct 88 ms 28380 KB Output is correct
10 Correct 95 ms 29288 KB Output is correct
11 Correct 91 ms 29072 KB Output is correct
12 Correct 105 ms 29260 KB Output is correct
13 Correct 105 ms 29272 KB Output is correct
14 Correct 101 ms 30976 KB Output is correct
15 Correct 105 ms 30928 KB Output is correct
16 Runtime error 46 ms 40572 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 46 ms 40660 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -