답안 #555089

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
555089 2022-04-30T07:11:50 Z kshitij_sodani Fish 2 (JOI22_fish2) C++14
48 / 100
4000 ms 191928 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'

llo it[100001];
vector<llo> aa[100001];
llo pre[100001];
vector<pair<llo,llo>> bb[100001];
vector<pair<llo,llo>> cc[100001];
pair<llo,llo> ne[100001];
llo fin[100001];
llo tree2[4*100001][2];
vector<pair<pair<llo,llo>,pair<llo,llo>>> ee[100001];
vector<pair<llo,llo>> ff[100001];
void build(llo no,llo l,llo r){
	if(l==r){
		tree2[no][0]=it[l]-pre[l];
		tree2[no][1]=it[l]+pre[l+1];
	}
	else{
		llo mid=(l+r)/2;
		build(no*2+1,l,mid);
		build(no*2+2,mid+1,r);
		tree2[no][0]=max(tree2[no*2+1][0],tree2[no*2+2][0]);
		tree2[no][1]=max(tree2[no*2+1][1],tree2[no*2+2][1]);
	}
}
llo query(llo no,llo l,llo r,llo aa,llo bb,llo ind){
	if(r<aa or l>bb or aa>bb){
		return -(llo)1e18;
	}
	if(aa<=l and r<=bb){
		return tree2[no][ind];
	}
	llo mid=(l+r)/2;
	return max(query(no*2+1,l,mid,aa,bb,ind),query(no*2+2,mid+1,r,aa,bb,ind));
}

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ord tree<pair<llo,llo>,null_type,less<pair<llo,llo>>,rb_tree_tag,tree_order_statistics_node_update>
ord tree3[4*100001];
void update2(llo no,llo l,llo r,llo i,llo j){
	tree3[no].insert({j,i});
	if(l<r){
		llo mid=(l+r)/2;
		if(i<=mid){
			update2(no*2+1,l,mid,i,j);
		}
		else{
			update2(no*2+2,mid+1,r,i,j);
		}
	}
}
llo query2(llo no,llo l,llo r,llo aa,llo bb,llo i){
	if(r<aa or l>bb or aa>bb){
		return 0;
	}
	if(aa<=l and r<=bb){
		if(tree3[no].size()==0){
			return 0;
		}

		//for(auto j:tree3[no]){
		//	cout<<j.a<<","<<j.b<<endl;
		//}
		return tree3[no].order_of_key({i,-1});
	}
	llo mid=(l+r)/2;
	return query2(no*2+1,l,mid,aa,bb,i)+query2(no*2+2,mid+1,r,aa,bb,i);
}
llo cot[100001];
llo mi[100001];
llo vis[100001];
llo re[100001];
llo le[100001];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	llo n,q;
	cin>>n;
	for(llo i=0;i<n;i++){
		cin>>it[i];
		pre[i+1]=pre[i]+it[i];
	}
	cin>>q;
	if(q<=1000){
		while(q--){
			int tt,l,r;
			cin>>tt>>l>>r;
			if(tt==1){
				l--;
				it[l]=r;
				for(llo i=0;i<n;i++){
					pre[i+1]=pre[i]+it[i];
				}
			}
			else{
				l--;
				r--;
				int ind=l-1;
				for(int i=l+1;i<=r;i++){
					if(it[i]>(pre[i]-pre[l])){
						ind=i-1;
					}
				}
				for(int i=l;i<=r+1;i++){
					vis[i]=0;
				}
/*				for(int j=l;j<=ind;j++){
					vis[j]=0;
				}*/
				vis[l]++;
				vis[ind+1]--;
				ind=r+1;
				for(int j=r-1;j>=l;j--){
					if(it[j]>(pre[r+1]-pre[j+1])){
						ind=j+1;
					}
				}
				vis[ind]++;
				vis[r+1]--;
				vector<pair<llo,llo>> ss;
				for(int i=l;i<=r;i++){
					while(ss.size()){
						if(ss.back().a<=it[i]){
							ss.pop_back();
						}
						else{
							break;
						}
					}
					if(ss.size()){
						le[i]=ss.back().b;
					}
					else{
						le[i]=-1;
					}
					ss.pb({it[i],i});
				}
				ss.clear();
				for(int i=r;i>=l;i--){
					while(ss.size()){
						if(ss.back().a<=it[i]){
							ss.pop_back();
						}
						else{
							break;
						}
					}
					if(ss.size()){
						re[i]=ss.back().b;
					}
					else{
						re[i]=-1;
					}
					ss.pb({it[i],i});
				}
				ss.clear();
				for(int i=l+1;i<=r-1;i++){
					if(le[i]>=0 and re[i]>=0){
						if(it[le[i]]>pre[re[i]]-pre[le[i]+1]){
							if(it[re[i]]>pre[re[i]]-pre[le[i]+1]){
								vis[le[i]+1]++;
								vis[re[i]]--;
								//cout<<le[i]+1<<":"<<re[i]-1<<endl;
							}
						}
					}
				}
				llo su=0;
				llo su2=0;
				for(int j=l;j<=r;j++){
					su+=vis[j];
					if(su==0){
						su2++;
					}
				}
				cout<<su2<<endl;
			}
		}
		return 0;
	}

	for(llo i=0;i<n;i++){
		ne[i]={-1,-1};
	}
	llo ax=0;
	for(llo i=1;i<n;i++){
		if(it[i]<it[i-1]){
			aa[i].pb(i);
		}
		for(auto j:aa[i-1]){
			if(pre[i+1]-pre[j]<it[j-1]){
				aa[i].pb(j);
			}
		}

		if(i<n-1){
			for(auto j:aa[i]){
				if(pre[i+1]-pre[j]<it[i+1]){
					bb[j-1].pb({i,j});
					cc[i+1].pb({i,j});
					ax++;
					//cout<<i<<":"<<j<<endl;
				}
			}
		}

	}
	assert(ax<=2*n);
	multiset<pair<llo,llo>> cur;
	llo ans=n;

	for(llo i=0;i<n;i++){
		for(auto j:cc[i]){
			auto jj=cur.find({j.a,-j.b});
			cur.erase(jj);
		}
		if(cur.size()){
			pair<llo,llo> no=*(cur.begin());
			no.b=-no.b;
			ans--;
			ne[i]={no.b,no.a};
		}
		else{
		}
		for(auto j:bb[i]){
			cur.insert({j.a,-j.b});
		}
	}		
	build(0,0,n-1);
	llo zz=0;
	llo qq=q;

	while(q--){
		llo tt,l,r;
		cin>>tt>>l>>r;
		if(tt==1){
			l--;
			it[l]=r;

			for(llo i=0;i<n;i++){
				ne[i]={-1,-1};
				aa[i].clear();
				bb[i].clear();
				cc[i].clear();
			}
			for(int i=0;i<n;i++){
				pre[i+1]=pre[i]+it[i];
			}
			for(llo i=1;i<n;i++){
				if(it[i]<it[i-1]){
					aa[i].pb(i);
				}
				for(auto j:aa[i-1]){
					if(pre[i+1]-pre[j]<it[j-1]){
						aa[i].pb(j);
					}
				}

				if(i<n-1){
					for(auto j:aa[i]){
						if(pre[i+1]-pre[j]<it[i+1]){
							bb[j-1].pb({i,j});
							cc[i+1].pb({i,j});
							//cout<<i<<":"<<j<<endl;

						}
					}
				}
			}
			multiset<pair<llo,llo>> cur;
			llo ans=n;
			for(llo i=0;i<n;i++){
				for(auto j:cc[i]){
					auto jj=cur.find({j.a,-j.b});
					cur.erase(jj);
				}
				if(cur.size()){
					pair<llo,llo> no=*(cur.begin());
					no.b=-no.b;
					ans--;
					ne[i]={no.b,no.a};
				}
				else{
				}
				for(auto j:bb[i]){
					cur.insert({j.a,-j.b});
				}
			}		
			build(0,0,n-1);
		}
		else{
			l--;
			r--;
			if(l==r){
				fin[zz]=1;
				if(n<=500 and qq<=500){
					cout<<1<<endl;
				}
				zz++;
				/*if(n<=500 and qq<=500){
					cout<<1<<endl;
				}*/
				continue;
			}
			llo ind=r+1;
			for(llo j=19;j>=0;j--){
				if(ind-(1<<j)>l){
					if(query(0,0,n-1,ind-(1<<j),r,0)+pre[l]<=0){
						//cout<<query(0,0,n-1,ind-(1<<j),r,0)+pre[l]<<","<<ind-(1<<j)<<","<<r<<endl;
						ind-=(1<<j);
					}
				}
			}
			ind--;
			//cout<<ind<<":"<<endl;
		/*	llo ind=low-1;
			for(llo i=l+1;i<=r;i++){
				if(it[i]-(pre[i]-pre[l])>0){
					ind=i;
				}
			}*/
			llo ind2=l-1;
			for(llo j=19;j>=0;j--){
				if(ind2+(1<<j)<r){
					if(query(0,0,n-1,l,ind2+(1<<j),1)-pre[r+1]<=0){
						ind2+=(1<<j);
					}
				}
			}
			ind2++;
			/*for(llo i=r-1;i>=l;i--){
				if(it[i]-(pre[r+1]-pre[i+1])>0){
					ind2=i;
				}
			}
			ind2++;*/

			llo su=0;
			if(ind>ind2){
				fin[zz]=0;
			}
			else{
				ee[l].pb({{ind,ind2},{r,zz}});
			}
			if(n<=500 and qq<=500){
				for(llo j=ind;j<=ind2;j++){
					if(ne[j].a>=0){
						if(ne[j].a>l and ne[j].b<r){
							continue;
						}
					}
					su++;
				}
				cout<<su<<endl;
			}
		
			zz++;
		}
	}

	for(llo i=0;i<n;i++){
		if(ne[i].a>=0){
			//cout<<i<<":"<<ne[i].a<<":"<<ne[i].b<<endl;
			ff[ne[i].a].pb({i,ne[i].b});
		}

	}
	for(int i=0;i<n;i++){
		cot[i]=n;
	}
	for(llo i=n-1;i>=0;i--){
		for(auto j:ee[i]){
			llo xx=(j.a.b-j.a.a+1)-query2(0,0,n-1,j.a.a,j.a.b,j.b.a);
			/*for(int ii=j.a.a;ii<=j.a.b;ii++){
				if(cot[ii]<j.b.a){

				}
				else{
					xx++;
				}
			}*/
		//	cout<<i<<","<<j.a.a<<","<<j.a.b<<","<<j.b.a<<","<<j.b.b<<endl;
			//cout<<xx<<endl;
			//xx=(j.a.b-j.a.a+1)-xx;
			fin[j.b.b]=xx;
		}
		for(auto j:ff[i]){
			//cout<<j.a<<"::"<<j.b<<endl;
			cot[j.a]=j.b;
			update2(0,0,n-1,j.a,j.b);
		}
	}

		if(n<=500 and qq<=500){
			return 0;
		}
	for(llo i=0;i<zz;i++){
		cout<<fin[i]<<endl;
	}








	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 49616 KB Output is correct
2 Correct 33 ms 49604 KB Output is correct
3 Correct 34 ms 49636 KB Output is correct
4 Correct 34 ms 49636 KB Output is correct
5 Correct 33 ms 49652 KB Output is correct
6 Correct 34 ms 49616 KB Output is correct
7 Correct 36 ms 49656 KB Output is correct
8 Correct 35 ms 49572 KB Output is correct
9 Correct 36 ms 49620 KB Output is correct
10 Correct 35 ms 49572 KB Output is correct
11 Correct 38 ms 49576 KB Output is correct
12 Correct 34 ms 49620 KB Output is correct
13 Correct 35 ms 49564 KB Output is correct
14 Correct 35 ms 49568 KB Output is correct
15 Correct 35 ms 49644 KB Output is correct
16 Correct 35 ms 49600 KB Output is correct
17 Correct 34 ms 49684 KB Output is correct
18 Correct 35 ms 49588 KB Output is correct
19 Correct 38 ms 49568 KB Output is correct
20 Correct 39 ms 49576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 49640 KB Output is correct
2 Correct 48 ms 53580 KB Output is correct
3 Correct 48 ms 53456 KB Output is correct
4 Correct 45 ms 53520 KB Output is correct
5 Correct 45 ms 53516 KB Output is correct
6 Correct 47 ms 53452 KB Output is correct
7 Correct 45 ms 53552 KB Output is correct
8 Correct 49 ms 53420 KB Output is correct
9 Correct 46 ms 53444 KB Output is correct
10 Correct 47 ms 53600 KB Output is correct
11 Correct 52 ms 53452 KB Output is correct
12 Correct 43 ms 53484 KB Output is correct
13 Correct 43 ms 53552 KB Output is correct
14 Correct 45 ms 53544 KB Output is correct
15 Correct 46 ms 53448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 49616 KB Output is correct
2 Correct 33 ms 49604 KB Output is correct
3 Correct 34 ms 49636 KB Output is correct
4 Correct 34 ms 49636 KB Output is correct
5 Correct 33 ms 49652 KB Output is correct
6 Correct 34 ms 49616 KB Output is correct
7 Correct 36 ms 49656 KB Output is correct
8 Correct 35 ms 49572 KB Output is correct
9 Correct 36 ms 49620 KB Output is correct
10 Correct 35 ms 49572 KB Output is correct
11 Correct 38 ms 49576 KB Output is correct
12 Correct 34 ms 49620 KB Output is correct
13 Correct 35 ms 49564 KB Output is correct
14 Correct 35 ms 49568 KB Output is correct
15 Correct 35 ms 49644 KB Output is correct
16 Correct 35 ms 49600 KB Output is correct
17 Correct 34 ms 49684 KB Output is correct
18 Correct 35 ms 49588 KB Output is correct
19 Correct 38 ms 49568 KB Output is correct
20 Correct 39 ms 49576 KB Output is correct
21 Correct 34 ms 49640 KB Output is correct
22 Correct 48 ms 53580 KB Output is correct
23 Correct 48 ms 53456 KB Output is correct
24 Correct 45 ms 53520 KB Output is correct
25 Correct 45 ms 53516 KB Output is correct
26 Correct 47 ms 53452 KB Output is correct
27 Correct 45 ms 53552 KB Output is correct
28 Correct 49 ms 53420 KB Output is correct
29 Correct 46 ms 53444 KB Output is correct
30 Correct 47 ms 53600 KB Output is correct
31 Correct 52 ms 53452 KB Output is correct
32 Correct 43 ms 53484 KB Output is correct
33 Correct 43 ms 53552 KB Output is correct
34 Correct 45 ms 53544 KB Output is correct
35 Correct 46 ms 53448 KB Output is correct
36 Correct 564 ms 53548 KB Output is correct
37 Correct 807 ms 53876 KB Output is correct
38 Correct 899 ms 53904 KB Output is correct
39 Correct 280 ms 54240 KB Output is correct
40 Correct 981 ms 53960 KB Output is correct
41 Correct 998 ms 54524 KB Output is correct
42 Correct 1234 ms 54520 KB Output is correct
43 Correct 995 ms 53888 KB Output is correct
44 Correct 1151 ms 53728 KB Output is correct
45 Correct 605 ms 54104 KB Output is correct
46 Correct 820 ms 53964 KB Output is correct
47 Correct 1189 ms 53944 KB Output is correct
48 Correct 495 ms 53656 KB Output is correct
49 Correct 1403 ms 53812 KB Output is correct
50 Correct 674 ms 53964 KB Output is correct
51 Correct 2790 ms 54160 KB Output is correct
52 Correct 2996 ms 54160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 49640 KB Output is correct
2 Correct 48 ms 53580 KB Output is correct
3 Correct 48 ms 53456 KB Output is correct
4 Correct 45 ms 53520 KB Output is correct
5 Correct 45 ms 53516 KB Output is correct
6 Correct 47 ms 53452 KB Output is correct
7 Correct 45 ms 53552 KB Output is correct
8 Correct 49 ms 53420 KB Output is correct
9 Correct 46 ms 53444 KB Output is correct
10 Correct 47 ms 53600 KB Output is correct
11 Correct 52 ms 53452 KB Output is correct
12 Correct 43 ms 53484 KB Output is correct
13 Correct 43 ms 53552 KB Output is correct
14 Correct 45 ms 53544 KB Output is correct
15 Correct 46 ms 53448 KB Output is correct
16 Correct 36 ms 49692 KB Output is correct
17 Correct 1868 ms 184704 KB Output is correct
18 Correct 1899 ms 179924 KB Output is correct
19 Correct 1743 ms 185020 KB Output is correct
20 Correct 1767 ms 185296 KB Output is correct
21 Correct 1742 ms 185396 KB Output is correct
22 Correct 1765 ms 179844 KB Output is correct
23 Correct 1733 ms 185124 KB Output is correct
24 Correct 1960 ms 185412 KB Output is correct
25 Correct 1936 ms 185492 KB Output is correct
26 Correct 1958 ms 185648 KB Output is correct
27 Correct 1339 ms 118192 KB Output is correct
28 Correct 1214 ms 118256 KB Output is correct
29 Correct 1128 ms 118228 KB Output is correct
30 Correct 1574 ms 165708 KB Output is correct
31 Correct 1705 ms 181788 KB Output is correct
32 Correct 1723 ms 191928 KB Output is correct
33 Correct 1792 ms 183712 KB Output is correct
34 Correct 1683 ms 191476 KB Output is correct
35 Correct 1761 ms 191332 KB Output is correct
36 Correct 1610 ms 186140 KB Output is correct
37 Correct 1707 ms 178460 KB Output is correct
38 Correct 1489 ms 159620 KB Output is correct
39 Correct 1296 ms 148692 KB Output is correct
40 Correct 1402 ms 148900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 49640 KB Output is correct
2 Correct 48 ms 53580 KB Output is correct
3 Correct 48 ms 53456 KB Output is correct
4 Correct 45 ms 53520 KB Output is correct
5 Correct 45 ms 53516 KB Output is correct
6 Correct 47 ms 53452 KB Output is correct
7 Correct 45 ms 53552 KB Output is correct
8 Correct 49 ms 53420 KB Output is correct
9 Correct 46 ms 53444 KB Output is correct
10 Correct 47 ms 53600 KB Output is correct
11 Correct 52 ms 53452 KB Output is correct
12 Correct 43 ms 53484 KB Output is correct
13 Correct 43 ms 53552 KB Output is correct
14 Correct 45 ms 53544 KB Output is correct
15 Correct 46 ms 53448 KB Output is correct
16 Correct 35 ms 49612 KB Output is correct
17 Execution timed out 4090 ms 64968 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 49616 KB Output is correct
2 Correct 33 ms 49604 KB Output is correct
3 Correct 34 ms 49636 KB Output is correct
4 Correct 34 ms 49636 KB Output is correct
5 Correct 33 ms 49652 KB Output is correct
6 Correct 34 ms 49616 KB Output is correct
7 Correct 36 ms 49656 KB Output is correct
8 Correct 35 ms 49572 KB Output is correct
9 Correct 36 ms 49620 KB Output is correct
10 Correct 35 ms 49572 KB Output is correct
11 Correct 38 ms 49576 KB Output is correct
12 Correct 34 ms 49620 KB Output is correct
13 Correct 35 ms 49564 KB Output is correct
14 Correct 35 ms 49568 KB Output is correct
15 Correct 35 ms 49644 KB Output is correct
16 Correct 35 ms 49600 KB Output is correct
17 Correct 34 ms 49684 KB Output is correct
18 Correct 35 ms 49588 KB Output is correct
19 Correct 38 ms 49568 KB Output is correct
20 Correct 39 ms 49576 KB Output is correct
21 Correct 34 ms 49640 KB Output is correct
22 Correct 48 ms 53580 KB Output is correct
23 Correct 48 ms 53456 KB Output is correct
24 Correct 45 ms 53520 KB Output is correct
25 Correct 45 ms 53516 KB Output is correct
26 Correct 47 ms 53452 KB Output is correct
27 Correct 45 ms 53552 KB Output is correct
28 Correct 49 ms 53420 KB Output is correct
29 Correct 46 ms 53444 KB Output is correct
30 Correct 47 ms 53600 KB Output is correct
31 Correct 52 ms 53452 KB Output is correct
32 Correct 43 ms 53484 KB Output is correct
33 Correct 43 ms 53552 KB Output is correct
34 Correct 45 ms 53544 KB Output is correct
35 Correct 46 ms 53448 KB Output is correct
36 Correct 564 ms 53548 KB Output is correct
37 Correct 807 ms 53876 KB Output is correct
38 Correct 899 ms 53904 KB Output is correct
39 Correct 280 ms 54240 KB Output is correct
40 Correct 981 ms 53960 KB Output is correct
41 Correct 998 ms 54524 KB Output is correct
42 Correct 1234 ms 54520 KB Output is correct
43 Correct 995 ms 53888 KB Output is correct
44 Correct 1151 ms 53728 KB Output is correct
45 Correct 605 ms 54104 KB Output is correct
46 Correct 820 ms 53964 KB Output is correct
47 Correct 1189 ms 53944 KB Output is correct
48 Correct 495 ms 53656 KB Output is correct
49 Correct 1403 ms 53812 KB Output is correct
50 Correct 674 ms 53964 KB Output is correct
51 Correct 2790 ms 54160 KB Output is correct
52 Correct 2996 ms 54160 KB Output is correct
53 Correct 36 ms 49692 KB Output is correct
54 Correct 1868 ms 184704 KB Output is correct
55 Correct 1899 ms 179924 KB Output is correct
56 Correct 1743 ms 185020 KB Output is correct
57 Correct 1767 ms 185296 KB Output is correct
58 Correct 1742 ms 185396 KB Output is correct
59 Correct 1765 ms 179844 KB Output is correct
60 Correct 1733 ms 185124 KB Output is correct
61 Correct 1960 ms 185412 KB Output is correct
62 Correct 1936 ms 185492 KB Output is correct
63 Correct 1958 ms 185648 KB Output is correct
64 Correct 1339 ms 118192 KB Output is correct
65 Correct 1214 ms 118256 KB Output is correct
66 Correct 1128 ms 118228 KB Output is correct
67 Correct 1574 ms 165708 KB Output is correct
68 Correct 1705 ms 181788 KB Output is correct
69 Correct 1723 ms 191928 KB Output is correct
70 Correct 1792 ms 183712 KB Output is correct
71 Correct 1683 ms 191476 KB Output is correct
72 Correct 1761 ms 191332 KB Output is correct
73 Correct 1610 ms 186140 KB Output is correct
74 Correct 1707 ms 178460 KB Output is correct
75 Correct 1489 ms 159620 KB Output is correct
76 Correct 1296 ms 148692 KB Output is correct
77 Correct 1402 ms 148900 KB Output is correct
78 Correct 35 ms 49612 KB Output is correct
79 Execution timed out 4090 ms 64968 KB Time limit exceeded
80 Halted 0 ms 0 KB -