답안 #529646

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529646 2022-02-23T09:55:07 Z jamezzz 식물 비교 (IOI20_plants) C++17
100 / 100
1177 ms 95716 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pf printf
#define pb push_back
typedef pair<int,int> ii;

#define maxn 200005

int n,k;

inline int add(int a,int b){return (a+b)%n;}
inline int sub(int a,int b){return (a-b+n)%n;}

struct node{
	int s,e,m,lz;ii v;
	node *l,*r;
	node(int _s,int _e){
		s=_s;e=_e;m=(s+e)/2;lz=0;v={0,s};
		if(s!=e)l=new node(s,m),r=new node(m+1,e);
	}
	void ppo(){
		v.fi+=lz;
		if(lz!=0&&s!=e)l->lz+=lz,r->lz+=lz;
		lz=0;
	}
	void up(int x,int y,int nv){
		if(s==x&&e==y){lz+=nv;return;}
		if(y<=m)l->up(x,y,nv);
		else if(x>m)r->up(x,y,nv);
		else l->up(x,m,nv),r->up(m+1,y,nv);
		l->ppo();r->ppo();v=min(l->v,r->v);
	}
	ii qry(int x,int y){
		ppo();
		if(s==x&&e==y)return v;
		if(y<=m)return l->qry(x,y);
		if(x>m)return r->qry(x,y);
		return min(l->qry(x,m),r->qry(m+1,y));
	}
	void up2(int x,int y,int nv){
		if(x<=y)up(x,y,nv);
		else up(0,y,nv),up(x,n-1,nv);
	}
	ii qry2(int x,int y){
		if(x<=y)return qry(x,y);
		else return min(qry(0,y),qry(x,n-1));
	}
}*root;

int maxb,h[maxn],nx[maxn][20],pv[maxn][20],dnx[maxn][20],dpv[maxn][20];
stack<int> st;
set<ii> s;

void init(int _k,vector<int> r){
	n=r.size();k=_k;
	root=new node(0,n-1);
	int cnt=n-1;
	for(int i=0;i<n;++i){
		root->up(i,i,r[i]);
	}
	while(cnt>=0){
		if(st.empty()){
			st.push((root->qry(0,n-1)).se);
		}
		int cur=st.top();
		ii pr=root->qry2(sub(cur,k-1),sub(cur,1));
		if(pr.fi==0){
			st.push(pr.se);continue;
		}
		h[cur]=cnt--;
		root->up(cur,cur,maxn);
		root->up2(sub(cur,k-1),sub(cur,1),-1);
		st.pop();
	}
	memset(pv,-1,sizeof pv);
	memset(nx,-1,sizeof nx);
	for(int i=0;i<k-1;++i)s.insert({h[i],i});
	for(int i=n-1;i>=0;--i){
		auto it=s.upper_bound({h[i],0});
		if(it!=s.end()){
			nx[i][0]=(*it).se;
			dnx[i][0]=sub(nx[i][0],i);
		}
		s.erase(s.find({h[add(i,k-1)],add(i,k-1)}));
		s.insert({h[i],i});
	}
	s.clear();
	for(int i=sub(n,k-1);i<n;++i)s.insert({h[i],i});
	for(int i=0;i<n;++i){
		auto it=s.upper_bound({h[i],0});
		if(it!=s.end()){
			pv[i][0]=(*it).se;
			dpv[i][0]=sub(i,pv[i][0]);
		}
		s.erase(s.find({h[sub(i,k-1)],sub(i,k-1)}));
		s.insert({h[i],i});
	}
	for(int j=1;(1<<j)<=n;++j){
		maxb=j;
		for(int i=0;i<n;++i){
			if(nx[i][j-1]!=-1&&dnx[i][j-1]+dnx[nx[i][j-1]][j-1]<=n){
				nx[i][j]=nx[nx[i][j-1]][j-1];
				dnx[i][j]=dnx[i][j-1]+dnx[nx[i][j-1]][j-1];
			}
			if(pv[i][j-1]!=-1&&dpv[i][j-1]+dpv[pv[i][j-1]][j-1]<=n){
				pv[i][j]=pv[pv[i][j-1]][j-1];
				dpv[i][j]=dpv[i][j-1]+dpv[pv[i][j-1]][j-1];
			}
		}
	}
}

bool check(int x,int y){//is x<y
	int tmp=x;
	for(int i=maxb;i>=0;--i){
		int t=nx[x][i];
		if(t==-1)continue;
		if(sub(y,t)+sub(t,x)==sub(y,x)&&h[t]<=h[y])x=t;
	}
	if(sub(y,x)<k)return true;
	x=tmp;
	for(int i=maxb;i>=0;--i){
		int t=pv[x][i];
		if(t==-1)continue;
		if(sub(x,t)+sub(t,y)==sub(x,y)&&h[t]<=h[y])x=t;
	}
	if(sub(x,y)<k)return true;
	return false;
}

int compare_plants(int x,int y){
	int ans=-1;
	if(h[x]>h[y])ans=1,swap(x,y);
	if(!check(x,y))ans=0;
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 31576 KB Output is correct
2 Correct 12 ms 31504 KB Output is correct
3 Correct 12 ms 31564 KB Output is correct
4 Correct 12 ms 31564 KB Output is correct
5 Correct 11 ms 31604 KB Output is correct
6 Correct 63 ms 34372 KB Output is correct
7 Correct 127 ms 38576 KB Output is correct
8 Correct 362 ms 86336 KB Output is correct
9 Correct 378 ms 76576 KB Output is correct
10 Correct 427 ms 71620 KB Output is correct
11 Correct 414 ms 70756 KB Output is correct
12 Correct 497 ms 70756 KB Output is correct
13 Correct 506 ms 70752 KB Output is correct
14 Correct 459 ms 70704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31564 KB Output is correct
2 Correct 12 ms 31520 KB Output is correct
3 Correct 12 ms 31564 KB Output is correct
4 Correct 13 ms 31536 KB Output is correct
5 Correct 15 ms 31612 KB Output is correct
6 Correct 16 ms 31948 KB Output is correct
7 Correct 85 ms 35848 KB Output is correct
8 Correct 16 ms 31624 KB Output is correct
9 Correct 16 ms 31948 KB Output is correct
10 Correct 87 ms 35936 KB Output is correct
11 Correct 112 ms 35564 KB Output is correct
12 Correct 107 ms 35780 KB Output is correct
13 Correct 85 ms 35956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31564 KB Output is correct
2 Correct 12 ms 31520 KB Output is correct
3 Correct 12 ms 31564 KB Output is correct
4 Correct 13 ms 31536 KB Output is correct
5 Correct 15 ms 31612 KB Output is correct
6 Correct 16 ms 31948 KB Output is correct
7 Correct 85 ms 35848 KB Output is correct
8 Correct 16 ms 31624 KB Output is correct
9 Correct 16 ms 31948 KB Output is correct
10 Correct 87 ms 35936 KB Output is correct
11 Correct 112 ms 35564 KB Output is correct
12 Correct 107 ms 35780 KB Output is correct
13 Correct 85 ms 35956 KB Output is correct
14 Correct 148 ms 40112 KB Output is correct
15 Correct 1173 ms 92236 KB Output is correct
16 Correct 139 ms 40072 KB Output is correct
17 Correct 1177 ms 92256 KB Output is correct
18 Correct 744 ms 83284 KB Output is correct
19 Correct 718 ms 83376 KB Output is correct
20 Correct 1148 ms 95716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 31676 KB Output is correct
2 Correct 16 ms 31564 KB Output is correct
3 Correct 107 ms 34884 KB Output is correct
4 Correct 646 ms 86472 KB Output is correct
5 Correct 662 ms 86440 KB Output is correct
6 Correct 914 ms 89680 KB Output is correct
7 Correct 1031 ms 90228 KB Output is correct
8 Correct 1120 ms 94424 KB Output is correct
9 Correct 608 ms 89360 KB Output is correct
10 Correct 725 ms 89504 KB Output is correct
11 Correct 523 ms 73552 KB Output is correct
12 Correct 532 ms 73820 KB Output is correct
13 Correct 710 ms 81376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 31564 KB Output is correct
2 Correct 13 ms 31568 KB Output is correct
3 Correct 13 ms 31620 KB Output is correct
4 Correct 13 ms 31616 KB Output is correct
5 Correct 16 ms 31604 KB Output is correct
6 Correct 17 ms 31680 KB Output is correct
7 Correct 29 ms 32280 KB Output is correct
8 Correct 28 ms 32240 KB Output is correct
9 Correct 41 ms 32540 KB Output is correct
10 Correct 28 ms 32620 KB Output is correct
11 Correct 39 ms 32652 KB Output is correct
12 Correct 38 ms 32644 KB Output is correct
13 Correct 33 ms 32556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 31516 KB Output is correct
2 Correct 13 ms 31500 KB Output is correct
3 Correct 13 ms 31620 KB Output is correct
4 Correct 12 ms 31564 KB Output is correct
5 Correct 15 ms 31816 KB Output is correct
6 Correct 688 ms 86368 KB Output is correct
7 Correct 763 ms 86444 KB Output is correct
8 Correct 1094 ms 86768 KB Output is correct
9 Correct 1149 ms 93304 KB Output is correct
10 Correct 554 ms 88528 KB Output is correct
11 Correct 773 ms 92928 KB Output is correct
12 Correct 580 ms 88468 KB Output is correct
13 Correct 646 ms 88676 KB Output is correct
14 Correct 796 ms 88808 KB Output is correct
15 Correct 962 ms 89456 KB Output is correct
16 Correct 603 ms 88368 KB Output is correct
17 Correct 553 ms 88664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 31576 KB Output is correct
2 Correct 12 ms 31504 KB Output is correct
3 Correct 12 ms 31564 KB Output is correct
4 Correct 12 ms 31564 KB Output is correct
5 Correct 11 ms 31604 KB Output is correct
6 Correct 63 ms 34372 KB Output is correct
7 Correct 127 ms 38576 KB Output is correct
8 Correct 362 ms 86336 KB Output is correct
9 Correct 378 ms 76576 KB Output is correct
10 Correct 427 ms 71620 KB Output is correct
11 Correct 414 ms 70756 KB Output is correct
12 Correct 497 ms 70756 KB Output is correct
13 Correct 506 ms 70752 KB Output is correct
14 Correct 459 ms 70704 KB Output is correct
15 Correct 12 ms 31564 KB Output is correct
16 Correct 12 ms 31520 KB Output is correct
17 Correct 12 ms 31564 KB Output is correct
18 Correct 13 ms 31536 KB Output is correct
19 Correct 15 ms 31612 KB Output is correct
20 Correct 16 ms 31948 KB Output is correct
21 Correct 85 ms 35848 KB Output is correct
22 Correct 16 ms 31624 KB Output is correct
23 Correct 16 ms 31948 KB Output is correct
24 Correct 87 ms 35936 KB Output is correct
25 Correct 112 ms 35564 KB Output is correct
26 Correct 107 ms 35780 KB Output is correct
27 Correct 85 ms 35956 KB Output is correct
28 Correct 148 ms 40112 KB Output is correct
29 Correct 1173 ms 92236 KB Output is correct
30 Correct 139 ms 40072 KB Output is correct
31 Correct 1177 ms 92256 KB Output is correct
32 Correct 744 ms 83284 KB Output is correct
33 Correct 718 ms 83376 KB Output is correct
34 Correct 1148 ms 95716 KB Output is correct
35 Correct 13 ms 31676 KB Output is correct
36 Correct 16 ms 31564 KB Output is correct
37 Correct 107 ms 34884 KB Output is correct
38 Correct 646 ms 86472 KB Output is correct
39 Correct 662 ms 86440 KB Output is correct
40 Correct 914 ms 89680 KB Output is correct
41 Correct 1031 ms 90228 KB Output is correct
42 Correct 1120 ms 94424 KB Output is correct
43 Correct 608 ms 89360 KB Output is correct
44 Correct 725 ms 89504 KB Output is correct
45 Correct 523 ms 73552 KB Output is correct
46 Correct 532 ms 73820 KB Output is correct
47 Correct 710 ms 81376 KB Output is correct
48 Correct 13 ms 31564 KB Output is correct
49 Correct 13 ms 31568 KB Output is correct
50 Correct 13 ms 31620 KB Output is correct
51 Correct 13 ms 31616 KB Output is correct
52 Correct 16 ms 31604 KB Output is correct
53 Correct 17 ms 31680 KB Output is correct
54 Correct 29 ms 32280 KB Output is correct
55 Correct 28 ms 32240 KB Output is correct
56 Correct 41 ms 32540 KB Output is correct
57 Correct 28 ms 32620 KB Output is correct
58 Correct 39 ms 32652 KB Output is correct
59 Correct 38 ms 32644 KB Output is correct
60 Correct 33 ms 32556 KB Output is correct
61 Correct 83 ms 36560 KB Output is correct
62 Correct 121 ms 41664 KB Output is correct
63 Correct 465 ms 89412 KB Output is correct
64 Correct 604 ms 89496 KB Output is correct
65 Correct 824 ms 89736 KB Output is correct
66 Correct 895 ms 90352 KB Output is correct
67 Correct 1077 ms 94192 KB Output is correct
68 Correct 569 ms 89492 KB Output is correct
69 Correct 756 ms 93728 KB Output is correct
70 Correct 577 ms 89392 KB Output is correct
71 Correct 673 ms 89500 KB Output is correct
72 Correct 811 ms 89672 KB Output is correct
73 Correct 902 ms 90320 KB Output is correct
74 Correct 461 ms 89428 KB Output is correct
75 Correct 600 ms 89464 KB Output is correct