답안 #804173

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
804173 2023-08-03T07:27:29 Z kshitij_sodani 송신탑 (IOI22_towers) C++17
23 / 100
4000 ms 31552 KB
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
#include "towers.h"

#include <vector>
int st=0;
int n;
vector<int> it;
int tree[4*200001][2];

void build(int no,int l,int r){
	if(l==r){
		tree[no][0]=it[l];
		tree[no][1]=it[l];
	}
	else{
		int mid=(l+r)/2;
		build(no*2+1,l,mid);
		build(no*2+2,mid+1,r);
		tree[no][0]=min(tree[no*2+1][0],tree[no*2+2][0]);
		tree[no][1]=max(tree[no*2+1][1],tree[no*2+2][1]);
	}
}
int query(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb or aa>bb){
		return 1e9+1;
	}
	if(aa<=l and r<=bb){
		return tree[no][0];
	}
	int mid=(l+r)/2;
	return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
}
int query2(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb or aa>bb){
		return -1;
	}
	if(aa<=l and r<=bb){
		return tree[no][1];
	}
	int mid=(l+r)/2;
	return max(query2(no*2+1,l,mid,aa,bb),query2(no*2+2,mid+1,r,aa,bb));
}
vector<int> mm;

void init(int nn, vector<int> ita) {
	n=nn;
	it=ita;
	st=0;
	build(0,0,n-1);
	
}
int le[100001];
int re2[100001];
int cc[100001][20];
int dd[100001][20];
int val[100001];
int tree2[4*200001];
int tree3[4*200001];

void build2(int no,int l,int r){
	if(l==r){
		tree2[no]=le[l];
		tree3[no]=re2[l];
	}
	else{
		int mid=(l+r)/2;
		build2(no*2+1,l,mid);
		build2(no*2+2,mid+1,r);
		tree2[no]=min(tree2[no*2+1],tree2[no*2+2]);
		tree3[no]=max(tree3[no*2+1],tree3[no*2+2]);
	}
}
int find(int no,int l,int r,int aa,int bb,int x){
	//find first with val less than x
	if(r<aa or l>bb or aa>bb){
		return -1;
	}
	if(l==r){
		if(tree2[no]<x){
			return l;
		}
		return -1;
	}
	if(tree2[no]>=x){
		return -1;
	}
	int mid=(l+r)/2;
	int xx=find(no*2+1,l,mid,aa,bb,x);
	if(xx>=0){
		return xx;
	}
	return find(no*2+2,mid+1,r,aa,bb,x);
}
int find2(int no,int l,int r,int aa,int bb,int x){
	//find first with val less than x
	if(r<aa or l>bb or aa>bb){
		return -1;
	}
	if(l==r){
		if(tree3[no]>x){
			return l;
		}
		return -1;
	}
	if(tree3[no]<=x){
		return -1;
	}
	int mid=(l+r)/2;
	int xx=find2(no*2+2,mid+1,r,aa,bb,x);
	if(xx>=0){
		return xx;
	}
	return find2(no*2+1,l,mid,aa,bb,x);
}
vector<int> adj[100001];
set<int> xa;
vector<pair<int,int>> yy;
int cot[100001*100];
int ll[100001*100];
int rr[100001*100];
int ba[100001];
int ne=0;
int update(int no,int l,int r,int i,int x){
	if(l==r){
		int sta=ne;
		ne++;
		ll[sta]=-1;
		rr[sta]=-1;
		cot[sta]=cot[no]+x;
		return sta;
	}
	else{
		int mid=(l+r)/2;
		int sta=ne;
		ne++;
		if(i<=mid){
			rr[sta]=rr[no];
			ll[sta]=update(ll[no],l,mid,i,x);
		}
		else{
			ll[sta]=ll[no];
			rr[sta]=update(rr[no],mid+1,r,i,x);
		}
		cot[sta]=0;
		if(ll[sta]>=0){
			cot[sta]+=cot[ll[sta]];
		}
		if(rr[sta]>=0){
			cot[sta]+=cot[rr[sta]];
		}
		return sta;
	}
}
int query5(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb or aa>bb){
		return 0;
	}
	if(aa<=l and r<=bb){
		return cot[no];
	}
	int mid=(l+r)/2;
	int su=0;
	if(ll[no]>=0){
		su+=query5(ll[no],l,mid,aa,bb);
	}
	if(rr[no]>=0){
		su+=query5(rr[no],mid+1,r,aa,bb);
	}
	return su;
}	
void dfs(int no,int par=-1){
	if(par==-1){
		par=ne;
		ll[ne]=-1;
		rr[ne]=-1;
		ne++;
	}
	else{
	}
	ba[no]=update(par,0,n-1,val[no],1);
	for(auto j:adj[no]){
		dfs(j,ba[no]);
	}
}
void rec(int l,int r){
	mm.clear();
	vector<pair<llo,llo>> ss;
	for(int i=r;i>=l;i--){
		while(ss.size()){
			if(ss.back().a<=it[i]){
				ss.pop_back();
			}
			else{
				break;
			}
		}
		re2[i]=n;
		if(ss.size()){
			re2[i]=ss.back().b;
		}
		ss.pb({it[i],i});
	}
	ss.clear();
	for(int i=l;i<=r;i++){
		while(ss.size()){
			if(ss.back().a<it[i]){
				ss.pop_back();
			}
			else{
				break;
			}
		}
		le[i]=-1;
		if(ss.size()){
			le[i]=ss.back().b;
		}
		ss.pb({it[i],i});
	}
	for(int i=0;i<n;i++){
		int x=it[i]-query(0,0,n-1,le[i]+1,i);
		int y=it[i]-query(0,0,n-1,i,re2[i]-1);
		val[i]=min(x,y);
		xa.insert(val[i]);
		mm.pb(min(x,y));
	}
	int ind7=0;
	map<int,int> kk;
	for(auto j:xa){
		kk[j]=ind7;
		yy.pb({j,ind7});
		ind7++;
	}
	for(int i=0;i<n;i++){
		val[i]=kk[val[i]];
	}
	dfs(n);

	for(int i=0;i<n;i++){
		cc[i][0]=le[i];
		dd[i][0]=re2[i];
		adj[dd[i][0]].pb(i);
	//	cout<<i<<":"<<le[i]<<endl;
	//	cout<<i<<":"<<re2[i]<<endl;
		if(re2[i]==n){
			dd[i][0]=-1;
		}
	}
	for(int j=1;j<20;j++){
		for(int i=0;i<n;i++){
			if(cc[i][j-1]==-1){
				cc[i][j]=-1;
			}
			else{
				cc[i][j]=cc[cc[i][j-1]][j-1];
			}
		}
	}
	for(int j=1;j<20;j++){
		for(int i=0;i<n;i++){
			if(dd[i][j-1]==-1){
				dd[i][j]=-1;
			}
			else{
				dd[i][j]=dd[dd[i][j-1]][j-1];
			}
		}
	}
	build2(0,0,n-1);
	sort(mm.begin(),mm.end());

}

int max_towers(int l, int r, int d) {
	if(st==0){
		rec(0,n-1);
		st=1;
	}
	int de=yy.size();

	for(int j=19;j>=0;j--){
		if(de-(1<<j)>=0){
			if(yy[de-(1<<j)].a>=d){
				de-=(1<<j);
			}
		}	
	}

	if(de==yy.size()){
		return 1;
	}
	
	llo low=mm.size();
	llo su=0;
	for(int i=l;i<=r;i++){
		if(val[i]>=de){
			su++;
		}
	}
	int ind=find(0,0,n-1,l,r,l);

	int ind2=ind;
	for(int j=19;j>=0;j--){
		if(dd[ind2][j]<=r and dd[ind2][j]>=0){
			ind2=dd[ind2][j];
		}
	}
	vector<llo> ss;
	int ind3=-1;
	if(query(0,0,n-1,l,ind-1)>it[ind]-d){
		ind3=ind;
		for(int j=19;j>=0;j--){
			if(dd[ind][j]<=r and dd[ind][j]>=0){
				if(query(0,0,n-1,l,dd[ind][j]-1)>it[dd[ind][j]]-d){
					ind=dd[ind][j];
				}
			}
		}		

		while(true){
			ss.pb(ind3);
			if(ind3==ind){
				break;
			}
			ind3=dd[ind3][0];
		}
	}
	pair<int,int> ll={ind3,ind};

	//from ind3 to ind
	//return su;
	vector<int> tt;
	ind=-1;
	ind=find2(0,0,n-1,l,r,r);

	ind3=-1;
	if(query(0,0,n-1,ind+1,r)<=it[ind]-d){
	}
	else{
		ind3=ind;
		for(int j=19;j>=0;j--){
			if(cc[ind][j]>=l){
				if(query(0,0,n-1,cc[ind][j]+1,r)>it[cc[ind][j]]-d){
					ind=cc[ind][j];
				}
			}
		}
		while(true){
			tt.pb(ind3);
			if(ind3==ind){
				break;
			}
			ind3=cc[ind3][0];
		}
	}
	pair<int,int> rr={ind3,ind};
	for(auto j:ss){
		if(val[j]>=de){
			su--;
		}
	}
	for(auto j:tt){
		if(val[j]>=de){
			su--;
		}
	}
	if(ll.a>=0 and rr.a>=0){
		if(rr.b==ind2 and ll.b==ind2){
			if(val[rr.b]>=de){
				su--;
			}
		}
	}
	
	su++;
	return su;
}

Compilation message

towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:293:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  293 |  if(de==yy.size()){
      |     ~~^~~~~~~~~~~
towers.cpp:297:6: warning: unused variable 'low' [-Wunused-variable]
  297 |  llo low=mm.size();
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4090 ms 18008 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2768 KB Output is correct
2 Correct 3 ms 3152 KB Output is correct
3 Correct 3 ms 3152 KB Output is correct
4 Correct 3 ms 3280 KB Output is correct
5 Correct 3 ms 3280 KB Output is correct
6 Correct 2 ms 3280 KB Output is correct
7 Correct 3 ms 3280 KB Output is correct
8 Correct 2 ms 3152 KB Output is correct
9 Correct 2 ms 3216 KB Output is correct
10 Correct 3 ms 3152 KB Output is correct
11 Correct 2 ms 3152 KB Output is correct
12 Correct 1 ms 2640 KB Output is correct
13 Correct 2 ms 3152 KB Output is correct
14 Correct 2 ms 3152 KB Output is correct
15 Correct 2 ms 3152 KB Output is correct
16 Correct 3 ms 3280 KB Output is correct
17 Correct 3 ms 3220 KB Output is correct
18 Correct 2 ms 3152 KB Output is correct
19 Correct 2 ms 3152 KB Output is correct
20 Correct 2 ms 3152 KB Output is correct
21 Correct 3 ms 3280 KB Output is correct
22 Correct 3 ms 3280 KB Output is correct
23 Correct 2 ms 3152 KB Output is correct
24 Correct 2 ms 3224 KB Output is correct
25 Correct 2 ms 2896 KB Output is correct
26 Correct 3 ms 3152 KB Output is correct
27 Correct 2 ms 3152 KB Output is correct
28 Correct 3 ms 3280 KB Output is correct
29 Correct 3 ms 3236 KB Output is correct
30 Correct 3 ms 3280 KB Output is correct
31 Correct 3 ms 3280 KB Output is correct
32 Correct 2 ms 3152 KB Output is correct
33 Correct 3 ms 3216 KB Output is correct
34 Correct 2 ms 3152 KB Output is correct
35 Correct 2 ms 3152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2768 KB Output is correct
2 Correct 3 ms 3152 KB Output is correct
3 Correct 3 ms 3152 KB Output is correct
4 Correct 3 ms 3280 KB Output is correct
5 Correct 3 ms 3280 KB Output is correct
6 Correct 2 ms 3280 KB Output is correct
7 Correct 3 ms 3280 KB Output is correct
8 Correct 2 ms 3152 KB Output is correct
9 Correct 2 ms 3216 KB Output is correct
10 Correct 3 ms 3152 KB Output is correct
11 Correct 2 ms 3152 KB Output is correct
12 Correct 1 ms 2640 KB Output is correct
13 Correct 2 ms 3152 KB Output is correct
14 Correct 2 ms 3152 KB Output is correct
15 Correct 2 ms 3152 KB Output is correct
16 Correct 3 ms 3280 KB Output is correct
17 Correct 3 ms 3220 KB Output is correct
18 Correct 2 ms 3152 KB Output is correct
19 Correct 2 ms 3152 KB Output is correct
20 Correct 2 ms 3152 KB Output is correct
21 Correct 3 ms 3280 KB Output is correct
22 Correct 3 ms 3280 KB Output is correct
23 Correct 2 ms 3152 KB Output is correct
24 Correct 2 ms 3224 KB Output is correct
25 Correct 2 ms 2896 KB Output is correct
26 Correct 3 ms 3152 KB Output is correct
27 Correct 2 ms 3152 KB Output is correct
28 Correct 3 ms 3280 KB Output is correct
29 Correct 3 ms 3236 KB Output is correct
30 Correct 3 ms 3280 KB Output is correct
31 Correct 3 ms 3280 KB Output is correct
32 Correct 2 ms 3152 KB Output is correct
33 Correct 3 ms 3216 KB Output is correct
34 Correct 2 ms 3152 KB Output is correct
35 Correct 2 ms 3152 KB Output is correct
36 Correct 52 ms 19652 KB Output is correct
37 Correct 81 ms 29788 KB Output is correct
38 Correct 84 ms 29792 KB Output is correct
39 Correct 90 ms 31496 KB Output is correct
40 Correct 100 ms 31480 KB Output is correct
41 Correct 95 ms 31396 KB Output is correct
42 Correct 99 ms 31500 KB Output is correct
43 Correct 65 ms 29376 KB Output is correct
44 Correct 59 ms 27460 KB Output is correct
45 Correct 63 ms 29040 KB Output is correct
46 Correct 55 ms 26728 KB Output is correct
47 Correct 82 ms 29720 KB Output is correct
48 Correct 90 ms 31472 KB Output is correct
49 Correct 91 ms 31380 KB Output is correct
50 Correct 55 ms 27516 KB Output is correct
51 Correct 60 ms 29368 KB Output is correct
52 Correct 83 ms 29796 KB Output is correct
53 Correct 98 ms 31500 KB Output is correct
54 Correct 98 ms 31552 KB Output is correct
55 Correct 56 ms 27456 KB Output is correct
56 Correct 61 ms 29284 KB Output is correct
57 Correct 79 ms 28956 KB Output is correct
58 Correct 82 ms 29776 KB Output is correct
59 Correct 88 ms 29812 KB Output is correct
60 Correct 92 ms 31504 KB Output is correct
61 Correct 94 ms 31500 KB Output is correct
62 Correct 92 ms 31416 KB Output is correct
63 Correct 96 ms 31472 KB Output is correct
64 Correct 64 ms 29496 KB Output is correct
65 Correct 54 ms 27480 KB Output is correct
66 Correct 62 ms 28880 KB Output is correct
67 Correct 55 ms 27324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2283 ms 29556 KB Output is correct
2 Correct 3247 ms 29696 KB Output is correct
3 Incorrect 3151 ms 29812 KB 18409th lines differ - on the 1st token, expected: '1', found: '-1'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 686 ms 9256 KB Output is correct
2 Execution timed out 4083 ms 29704 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2768 KB Output is correct
2 Correct 3 ms 3152 KB Output is correct
3 Correct 3 ms 3152 KB Output is correct
4 Correct 3 ms 3280 KB Output is correct
5 Correct 3 ms 3280 KB Output is correct
6 Correct 2 ms 3280 KB Output is correct
7 Correct 3 ms 3280 KB Output is correct
8 Correct 2 ms 3152 KB Output is correct
9 Correct 2 ms 3216 KB Output is correct
10 Correct 3 ms 3152 KB Output is correct
11 Correct 2 ms 3152 KB Output is correct
12 Correct 1 ms 2640 KB Output is correct
13 Correct 2 ms 3152 KB Output is correct
14 Correct 2 ms 3152 KB Output is correct
15 Correct 2 ms 3152 KB Output is correct
16 Correct 3 ms 3280 KB Output is correct
17 Correct 3 ms 3220 KB Output is correct
18 Correct 2 ms 3152 KB Output is correct
19 Correct 2 ms 3152 KB Output is correct
20 Correct 2 ms 3152 KB Output is correct
21 Correct 3 ms 3280 KB Output is correct
22 Correct 3 ms 3280 KB Output is correct
23 Correct 2 ms 3152 KB Output is correct
24 Correct 2 ms 3224 KB Output is correct
25 Correct 2 ms 2896 KB Output is correct
26 Correct 3 ms 3152 KB Output is correct
27 Correct 2 ms 3152 KB Output is correct
28 Correct 3 ms 3280 KB Output is correct
29 Correct 3 ms 3236 KB Output is correct
30 Correct 3 ms 3280 KB Output is correct
31 Correct 3 ms 3280 KB Output is correct
32 Correct 2 ms 3152 KB Output is correct
33 Correct 3 ms 3216 KB Output is correct
34 Correct 2 ms 3152 KB Output is correct
35 Correct 2 ms 3152 KB Output is correct
36 Correct 52 ms 19652 KB Output is correct
37 Correct 81 ms 29788 KB Output is correct
38 Correct 84 ms 29792 KB Output is correct
39 Correct 90 ms 31496 KB Output is correct
40 Correct 100 ms 31480 KB Output is correct
41 Correct 95 ms 31396 KB Output is correct
42 Correct 99 ms 31500 KB Output is correct
43 Correct 65 ms 29376 KB Output is correct
44 Correct 59 ms 27460 KB Output is correct
45 Correct 63 ms 29040 KB Output is correct
46 Correct 55 ms 26728 KB Output is correct
47 Correct 82 ms 29720 KB Output is correct
48 Correct 90 ms 31472 KB Output is correct
49 Correct 91 ms 31380 KB Output is correct
50 Correct 55 ms 27516 KB Output is correct
51 Correct 60 ms 29368 KB Output is correct
52 Correct 83 ms 29796 KB Output is correct
53 Correct 98 ms 31500 KB Output is correct
54 Correct 98 ms 31552 KB Output is correct
55 Correct 56 ms 27456 KB Output is correct
56 Correct 61 ms 29284 KB Output is correct
57 Correct 79 ms 28956 KB Output is correct
58 Correct 82 ms 29776 KB Output is correct
59 Correct 88 ms 29812 KB Output is correct
60 Correct 92 ms 31504 KB Output is correct
61 Correct 94 ms 31500 KB Output is correct
62 Correct 92 ms 31416 KB Output is correct
63 Correct 96 ms 31472 KB Output is correct
64 Correct 64 ms 29496 KB Output is correct
65 Correct 54 ms 27480 KB Output is correct
66 Correct 62 ms 28880 KB Output is correct
67 Correct 55 ms 27324 KB Output is correct
68 Correct 2283 ms 29556 KB Output is correct
69 Correct 3247 ms 29696 KB Output is correct
70 Incorrect 3151 ms 29812 KB 18409th lines differ - on the 1st token, expected: '1', found: '-1'
71 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4090 ms 18008 KB Time limit exceeded
2 Halted 0 ms 0 KB -