Submission #553850

# Submission time Handle Problem Language Result Execution time Memory
553850 2022-04-27T07:41:37 Z amunduzbaev Fish 2 (JOI22_fish2) C++17
31 / 100
1106 ms 16844 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;

struct ST2{
	int tree[N<<2];
	void sett(int i, int v, int lx = 0, int rx = N, int x = 1){
		if(lx == rx) {
			tree[x] = v; 
			return;
		} int m = (lx + rx) >> 1;
		if(i <= m) sett(i, v, lx, m, x<<1);
		else sett(i, v, m+1, rx, x<<1|1);
		tree[x] = max(tree[x<<1], tree[x<<1|1]);
	}
	
	int get_l(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return -1;
		if(lx >= l && rx <= r){
			if(tree[x] >= v){
				if(lx == rx) return lx;
				int m = (lx + rx) >> 1;
				if(tree[x<<1] >= v) return get_l(l, r, v, lx, m, x<<1);
				else return get_l(l, r, v, m+1, rx, x<<1|1);
			} else return -1;
		} int m = (lx + rx) >> 1;
		int res = get_l(l, r, v, lx, m, x<<1);
		if(~res) return res;
		return get_l(l, r, v, m+1, rx, x<<1|1);
	}
	
	int get_r(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return -1;
		if(lx >= l && rx <= r){
			if(tree[x] >= v){
				if(lx == rx) return lx;
				int m = (lx + rx) >> 1;
				if(tree[x<<1|1] >= v) return get_r(l, r, v, m+1, rx, x<<1|1);
				else return get_r(l, r, v, lx, m, x<<1);
			} else return -1;
		} int m = (lx + rx) >> 1;
		int res = get_r(l, r, v, m+1, rx, x<<1|1);
		if(~res) return res;
		return get_r(l, r, v, lx, m, x<<1);
	}
}tt;

int a[N], pref[N];

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;
	a[n+2] = a[n+1] + 1;
	for(int i=0;i<N;i++){
		tt.sett(i, a[i]);
	}
	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;
	};
	
	auto par = [&](int i, int d){
		if(d){
			return tt.get_l(i, N, a[i] + 1);
		} else {
			return tt.get_r(0, i, a[i] + 1);
		}
	};
	auto p2 = [&](int i, int d){
		if(d){
			return tt.get_l(i, N, a[i] * 2);
		} else {
			return tt.get_r(0, i, a[i] * 2);
		}
	};
	
	for(int i=1;i<=n;i++){
		int j = par(i, 1);
		int mx = i;
		while(j-1<=n-(i == 1)){
			// cout<<"here"<<endl;
			check(i, j-1);
			int P = p2(mx, 1);
			if(P == j){
				mx = j;
				j = par(j, 1);
			} else {
				int v = j;
				j = P;
				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);
			int i, v; cin>>i>>v;
			auto it = ss.lower_bound({i+1, 0});
			
		} 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;
					int P = p2(mx, 1);
					if(P == j){
						mx = j;
						j = par(j, 1);
					} else {
						int v = j;
						j = P;
						mx = v;
					}
				}
			}
			
			{
				int j=par(r, 0), mx = r;
				while(l <= j){
					if(pref[r] - pref[j] < a[j]) R = j;
					int P = p2(mx, 0);
					if(P == j){
						mx = j;
						j = par(j, 0);
					} else {
						int v = j;
						j = P;
						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:177:9: warning: variable 'it' set but not used [-Wunused-but-set-variable]
  177 |    auto it = ss.lower_bound({i+1, 0});
      |         ^~
fish2.cpp:116:7: warning: variable 'del' set but not used [-Wunused-but-set-variable]
  116 |  auto del = [&](int l, int r){
      |       ^~~
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 13012 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6484 KB Output is correct
2 Correct 332 ms 15640 KB Output is correct
3 Correct 378 ms 14880 KB Output is correct
4 Correct 319 ms 15524 KB Output is correct
5 Correct 365 ms 14924 KB Output is correct
6 Correct 95 ms 12936 KB Output is correct
7 Correct 199 ms 11760 KB Output is correct
8 Correct 96 ms 12876 KB Output is correct
9 Correct 194 ms 11776 KB Output is correct
10 Correct 233 ms 12580 KB Output is correct
11 Correct 270 ms 12520 KB Output is correct
12 Correct 108 ms 12620 KB Output is correct
13 Correct 119 ms 12672 KB Output is correct
14 Correct 121 ms 14388 KB Output is correct
15 Correct 123 ms 14304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 13012 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6484 KB Output is correct
2 Correct 332 ms 15640 KB Output is correct
3 Correct 378 ms 14880 KB Output is correct
4 Correct 319 ms 15524 KB Output is correct
5 Correct 365 ms 14924 KB Output is correct
6 Correct 95 ms 12936 KB Output is correct
7 Correct 199 ms 11760 KB Output is correct
8 Correct 96 ms 12876 KB Output is correct
9 Correct 194 ms 11776 KB Output is correct
10 Correct 233 ms 12580 KB Output is correct
11 Correct 270 ms 12520 KB Output is correct
12 Correct 108 ms 12620 KB Output is correct
13 Correct 119 ms 12672 KB Output is correct
14 Correct 121 ms 14388 KB Output is correct
15 Correct 123 ms 14304 KB Output is correct
16 Correct 10 ms 6484 KB Output is correct
17 Correct 925 ms 15404 KB Output is correct
18 Correct 768 ms 16780 KB Output is correct
19 Correct 902 ms 15684 KB Output is correct
20 Correct 918 ms 15684 KB Output is correct
21 Correct 853 ms 15612 KB Output is correct
22 Correct 752 ms 16844 KB Output is correct
23 Correct 825 ms 15304 KB Output is correct
24 Correct 946 ms 16020 KB Output is correct
25 Correct 890 ms 15764 KB Output is correct
26 Correct 1064 ms 15876 KB Output is correct
27 Correct 358 ms 13596 KB Output is correct
28 Correct 320 ms 13620 KB Output is correct
29 Correct 350 ms 13620 KB Output is correct
30 Correct 861 ms 12116 KB Output is correct
31 Correct 941 ms 12204 KB Output is correct
32 Correct 1106 ms 13120 KB Output is correct
33 Correct 671 ms 13260 KB Output is correct
34 Correct 1014 ms 12636 KB Output is correct
35 Correct 712 ms 12128 KB Output is correct
36 Correct 793 ms 13664 KB Output is correct
37 Correct 314 ms 13152 KB Output is correct
38 Correct 297 ms 13032 KB Output is correct
39 Correct 380 ms 15132 KB Output is correct
40 Correct 341 ms 15124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6484 KB Output is correct
2 Correct 332 ms 15640 KB Output is correct
3 Correct 378 ms 14880 KB Output is correct
4 Correct 319 ms 15524 KB Output is correct
5 Correct 365 ms 14924 KB Output is correct
6 Correct 95 ms 12936 KB Output is correct
7 Correct 199 ms 11760 KB Output is correct
8 Correct 96 ms 12876 KB Output is correct
9 Correct 194 ms 11776 KB Output is correct
10 Correct 233 ms 12580 KB Output is correct
11 Correct 270 ms 12520 KB Output is correct
12 Correct 108 ms 12620 KB Output is correct
13 Correct 119 ms 12672 KB Output is correct
14 Correct 121 ms 14388 KB Output is correct
15 Correct 123 ms 14304 KB Output is correct
16 Runtime error 15 ms 13012 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 13012 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -