답안 #109667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109667 2019-05-07T09:36:10 Z jangwonyoung Alternating Current (BOI18_alternating) C++14
100 / 100
428 ms 15576 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=1e5+1,M=1e5+1;
int n,m;
int u[M],v[M];
vector<int>add[M];
bool in[M];
set<int>s;
vector<int>adj[M];
int col[M];
bool ok=true;
void dfs(int id){
	for(auto cur:adj[id]){
		if(col[cur]==col[id]) ok=false;
		if(!col[cur]){
			col[cur]=3-col[id];
			dfs(cur);
		}
	}
}
int lz[262144],mi[262144];
void push(int id){
	lz[id*2]+=lz[id];
	mi[id*2]+=lz[id];
	lz[id*2+1]+=lz[id];
	mi[id*2+1]+=lz[id];
	lz[id]=0;
}
void upd(int id,int l,int r,int ql,int qr,int v){
	if(l>qr || r<ql) return;
	if(ql<=l && r<=qr){
		lz[id]+=v;mi[id]+=v;return;
	}
	push(id);
	int mid=(l+r)/2;
	upd(id*2,l,mid,ql,qr,v);upd(id*2+1,mid+1,r,ql,qr,v);
	mi[id]=min(mi[id*2],mi[id*2+1]);
}
int qry(int id,int l,int r,int ql,int qr){
	if(l>qr || r<ql) return 1e9;
	if(ql<=l && r<=qr) return mi[id];
	push(id);
	int mid=(l+r)/2;
	return min(qry(id*2,l,mid,ql,qr),qry(id*2+1,mid+1,r,ql,qr));
}
int main(){
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(int i=1; i<=m ;i++){
		cin >> u[i] >> v[i];
		add[u[i]-1].pb(i);add[v[i]].pb(i);
		if(u[i]>v[i] || u[i]==1) s.insert(i),in[i]=true;
	}
	for(int i=1; i<=n ;i++){
		if(s.size()<2) return cout << "impossible\n",0;
		if(s.size()==2){
			int x=*s.begin(),y=*s.rbegin();
			adj[x].pb(y);adj[y].pb(x);
		}
		for(auto cur:add[i]){
			if(in[cur]) in[cur]=false,s.erase(cur);
			else in[cur]=true,s.insert(cur);
		}
	}
	for(int i=1; i<=m ;i++){
		if(!col[i]){col[i]=1;dfs(i);}
		if(col[i]==1){
			if(u[i]<=v[i]) upd(1,1,n,u[i],v[i],1);
			else upd(1,1,n,u[i],n,1),upd(1,1,n,1,v[i],1);
		}
	}
	if(!ok) return cout << "impossible\n",0;
	for(int i=1; i<=m ;i++){
		if(col[i]!=1){cout << "0";continue;}
		int val;
		if(u[i]<=v[i]) val=qry(1,1,n,u[i],v[i]);
		else val=min(qry(1,1,n,u[i],n),qry(1,1,n,1,v[i]));
		if(val==1){cout << "1";continue;}
		if(u[i]<=v[i]) upd(1,1,n,u[i],v[i],-1);
		else upd(1,1,n,u[i],n,-1),upd(1,1,n,1,v[i],-1);
		cout << "0";
	}
	cout << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 4992 KB Output is correct
10 Correct 9 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 4992 KB Output is correct
24 Correct 6 ms 4992 KB Output is correct
25 Correct 6 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 6 ms 4992 KB Output is correct
28 Correct 9 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 6 ms 5120 KB Output is correct
31 Correct 8 ms 4992 KB Output is correct
32 Correct 6 ms 4992 KB Output is correct
33 Correct 6 ms 5120 KB Output is correct
34 Correct 7 ms 5120 KB Output is correct
35 Correct 6 ms 4992 KB Output is correct
36 Correct 8 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 7 ms 4992 KB Output is correct
39 Correct 7 ms 4992 KB Output is correct
40 Correct 7 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 4992 KB Output is correct
10 Correct 9 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 4992 KB Output is correct
24 Correct 6 ms 4992 KB Output is correct
25 Correct 6 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 6 ms 4992 KB Output is correct
28 Correct 9 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 6 ms 5120 KB Output is correct
31 Correct 8 ms 4992 KB Output is correct
32 Correct 6 ms 4992 KB Output is correct
33 Correct 6 ms 5120 KB Output is correct
34 Correct 7 ms 5120 KB Output is correct
35 Correct 6 ms 4992 KB Output is correct
36 Correct 8 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 7 ms 4992 KB Output is correct
39 Correct 7 ms 4992 KB Output is correct
40 Correct 7 ms 5120 KB Output is correct
41 Correct 6 ms 4992 KB Output is correct
42 Correct 7 ms 5120 KB Output is correct
43 Correct 6 ms 5120 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 6 ms 5120 KB Output is correct
46 Correct 6 ms 5120 KB Output is correct
47 Correct 6 ms 5120 KB Output is correct
48 Correct 7 ms 4992 KB Output is correct
49 Correct 8 ms 5120 KB Output is correct
50 Correct 7 ms 5120 KB Output is correct
51 Correct 6 ms 4992 KB Output is correct
52 Correct 6 ms 4992 KB Output is correct
53 Correct 8 ms 4992 KB Output is correct
54 Correct 7 ms 5120 KB Output is correct
55 Correct 6 ms 5120 KB Output is correct
56 Correct 6 ms 5120 KB Output is correct
57 Correct 6 ms 5120 KB Output is correct
58 Correct 6 ms 5120 KB Output is correct
59 Correct 7 ms 5120 KB Output is correct
60 Correct 6 ms 4992 KB Output is correct
61 Correct 7 ms 4992 KB Output is correct
62 Correct 5 ms 5120 KB Output is correct
63 Correct 7 ms 5120 KB Output is correct
64 Correct 6 ms 5120 KB Output is correct
65 Correct 6 ms 5120 KB Output is correct
66 Correct 8 ms 4992 KB Output is correct
67 Correct 6 ms 5120 KB Output is correct
68 Correct 7 ms 5120 KB Output is correct
69 Correct 6 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 4992 KB Output is correct
10 Correct 9 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 4992 KB Output is correct
24 Correct 6 ms 4992 KB Output is correct
25 Correct 6 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 6 ms 4992 KB Output is correct
28 Correct 9 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 6 ms 5120 KB Output is correct
31 Correct 8 ms 4992 KB Output is correct
32 Correct 6 ms 4992 KB Output is correct
33 Correct 6 ms 5120 KB Output is correct
34 Correct 7 ms 5120 KB Output is correct
35 Correct 6 ms 4992 KB Output is correct
36 Correct 8 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 7 ms 4992 KB Output is correct
39 Correct 7 ms 4992 KB Output is correct
40 Correct 7 ms 5120 KB Output is correct
41 Correct 6 ms 4992 KB Output is correct
42 Correct 7 ms 5120 KB Output is correct
43 Correct 6 ms 5120 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 6 ms 5120 KB Output is correct
46 Correct 6 ms 5120 KB Output is correct
47 Correct 6 ms 5120 KB Output is correct
48 Correct 7 ms 4992 KB Output is correct
49 Correct 8 ms 5120 KB Output is correct
50 Correct 7 ms 5120 KB Output is correct
51 Correct 6 ms 4992 KB Output is correct
52 Correct 6 ms 4992 KB Output is correct
53 Correct 8 ms 4992 KB Output is correct
54 Correct 7 ms 5120 KB Output is correct
55 Correct 6 ms 5120 KB Output is correct
56 Correct 6 ms 5120 KB Output is correct
57 Correct 6 ms 5120 KB Output is correct
58 Correct 6 ms 5120 KB Output is correct
59 Correct 7 ms 5120 KB Output is correct
60 Correct 6 ms 4992 KB Output is correct
61 Correct 7 ms 4992 KB Output is correct
62 Correct 5 ms 5120 KB Output is correct
63 Correct 7 ms 5120 KB Output is correct
64 Correct 6 ms 5120 KB Output is correct
65 Correct 6 ms 5120 KB Output is correct
66 Correct 8 ms 4992 KB Output is correct
67 Correct 6 ms 5120 KB Output is correct
68 Correct 7 ms 5120 KB Output is correct
69 Correct 6 ms 5120 KB Output is correct
70 Correct 7 ms 5120 KB Output is correct
71 Correct 7 ms 5120 KB Output is correct
72 Correct 6 ms 5120 KB Output is correct
73 Correct 7 ms 5120 KB Output is correct
74 Correct 7 ms 5120 KB Output is correct
75 Correct 7 ms 4992 KB Output is correct
76 Correct 7 ms 5120 KB Output is correct
77 Correct 8 ms 5120 KB Output is correct
78 Correct 6 ms 5120 KB Output is correct
79 Correct 6 ms 4992 KB Output is correct
80 Correct 6 ms 5120 KB Output is correct
81 Correct 7 ms 5120 KB Output is correct
82 Correct 6 ms 5120 KB Output is correct
83 Correct 7 ms 5120 KB Output is correct
84 Correct 7 ms 5120 KB Output is correct
85 Correct 7 ms 5120 KB Output is correct
86 Correct 7 ms 5036 KB Output is correct
87 Correct 9 ms 5120 KB Output is correct
88 Correct 7 ms 5120 KB Output is correct
89 Correct 7 ms 5120 KB Output is correct
90 Correct 10 ms 5248 KB Output is correct
91 Correct 7 ms 5120 KB Output is correct
92 Correct 7 ms 5120 KB Output is correct
93 Correct 7 ms 5120 KB Output is correct
94 Correct 8 ms 4992 KB Output is correct
95 Correct 7 ms 5120 KB Output is correct
96 Correct 8 ms 5120 KB Output is correct
97 Correct 8 ms 5120 KB Output is correct
98 Correct 7 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 11940 KB Output is correct
2 Correct 9 ms 6108 KB Output is correct
3 Correct 24 ms 8576 KB Output is correct
4 Correct 114 ms 13420 KB Output is correct
5 Correct 161 ms 13748 KB Output is correct
6 Correct 72 ms 8828 KB Output is correct
7 Correct 173 ms 13944 KB Output is correct
8 Correct 14 ms 8064 KB Output is correct
9 Correct 12 ms 7680 KB Output is correct
10 Correct 160 ms 14072 KB Output is correct
11 Correct 133 ms 12704 KB Output is correct
12 Correct 165 ms 14320 KB Output is correct
13 Correct 10 ms 6128 KB Output is correct
14 Correct 8 ms 6016 KB Output is correct
15 Correct 186 ms 10616 KB Output is correct
16 Correct 35 ms 11000 KB Output is correct
17 Correct 345 ms 13688 KB Output is correct
18 Correct 227 ms 10232 KB Output is correct
19 Correct 9 ms 5376 KB Output is correct
20 Correct 70 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 4992 KB Output is correct
10 Correct 9 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 4992 KB Output is correct
24 Correct 6 ms 4992 KB Output is correct
25 Correct 6 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 6 ms 4992 KB Output is correct
28 Correct 9 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 6 ms 5120 KB Output is correct
31 Correct 8 ms 4992 KB Output is correct
32 Correct 6 ms 4992 KB Output is correct
33 Correct 6 ms 5120 KB Output is correct
34 Correct 7 ms 5120 KB Output is correct
35 Correct 6 ms 4992 KB Output is correct
36 Correct 8 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 7 ms 4992 KB Output is correct
39 Correct 7 ms 4992 KB Output is correct
40 Correct 7 ms 5120 KB Output is correct
41 Correct 6 ms 4992 KB Output is correct
42 Correct 7 ms 5120 KB Output is correct
43 Correct 6 ms 5120 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 6 ms 5120 KB Output is correct
46 Correct 6 ms 5120 KB Output is correct
47 Correct 6 ms 5120 KB Output is correct
48 Correct 7 ms 4992 KB Output is correct
49 Correct 8 ms 5120 KB Output is correct
50 Correct 7 ms 5120 KB Output is correct
51 Correct 6 ms 4992 KB Output is correct
52 Correct 6 ms 4992 KB Output is correct
53 Correct 8 ms 4992 KB Output is correct
54 Correct 7 ms 5120 KB Output is correct
55 Correct 6 ms 5120 KB Output is correct
56 Correct 6 ms 5120 KB Output is correct
57 Correct 6 ms 5120 KB Output is correct
58 Correct 6 ms 5120 KB Output is correct
59 Correct 7 ms 5120 KB Output is correct
60 Correct 6 ms 4992 KB Output is correct
61 Correct 7 ms 4992 KB Output is correct
62 Correct 5 ms 5120 KB Output is correct
63 Correct 7 ms 5120 KB Output is correct
64 Correct 6 ms 5120 KB Output is correct
65 Correct 6 ms 5120 KB Output is correct
66 Correct 8 ms 4992 KB Output is correct
67 Correct 6 ms 5120 KB Output is correct
68 Correct 7 ms 5120 KB Output is correct
69 Correct 6 ms 5120 KB Output is correct
70 Correct 7 ms 5120 KB Output is correct
71 Correct 7 ms 5120 KB Output is correct
72 Correct 6 ms 5120 KB Output is correct
73 Correct 7 ms 5120 KB Output is correct
74 Correct 7 ms 5120 KB Output is correct
75 Correct 7 ms 4992 KB Output is correct
76 Correct 7 ms 5120 KB Output is correct
77 Correct 8 ms 5120 KB Output is correct
78 Correct 6 ms 5120 KB Output is correct
79 Correct 6 ms 4992 KB Output is correct
80 Correct 6 ms 5120 KB Output is correct
81 Correct 7 ms 5120 KB Output is correct
82 Correct 6 ms 5120 KB Output is correct
83 Correct 7 ms 5120 KB Output is correct
84 Correct 7 ms 5120 KB Output is correct
85 Correct 7 ms 5120 KB Output is correct
86 Correct 7 ms 5036 KB Output is correct
87 Correct 9 ms 5120 KB Output is correct
88 Correct 7 ms 5120 KB Output is correct
89 Correct 7 ms 5120 KB Output is correct
90 Correct 10 ms 5248 KB Output is correct
91 Correct 7 ms 5120 KB Output is correct
92 Correct 7 ms 5120 KB Output is correct
93 Correct 7 ms 5120 KB Output is correct
94 Correct 8 ms 4992 KB Output is correct
95 Correct 7 ms 5120 KB Output is correct
96 Correct 8 ms 5120 KB Output is correct
97 Correct 8 ms 5120 KB Output is correct
98 Correct 7 ms 5120 KB Output is correct
99 Correct 62 ms 11940 KB Output is correct
100 Correct 9 ms 6108 KB Output is correct
101 Correct 24 ms 8576 KB Output is correct
102 Correct 114 ms 13420 KB Output is correct
103 Correct 161 ms 13748 KB Output is correct
104 Correct 72 ms 8828 KB Output is correct
105 Correct 173 ms 13944 KB Output is correct
106 Correct 14 ms 8064 KB Output is correct
107 Correct 12 ms 7680 KB Output is correct
108 Correct 160 ms 14072 KB Output is correct
109 Correct 133 ms 12704 KB Output is correct
110 Correct 165 ms 14320 KB Output is correct
111 Correct 10 ms 6128 KB Output is correct
112 Correct 8 ms 6016 KB Output is correct
113 Correct 186 ms 10616 KB Output is correct
114 Correct 35 ms 11000 KB Output is correct
115 Correct 345 ms 13688 KB Output is correct
116 Correct 227 ms 10232 KB Output is correct
117 Correct 9 ms 5376 KB Output is correct
118 Correct 70 ms 12152 KB Output is correct
119 Correct 9 ms 6832 KB Output is correct
120 Correct 145 ms 15576 KB Output is correct
121 Correct 74 ms 11388 KB Output is correct
122 Correct 6 ms 4992 KB Output is correct
123 Correct 10 ms 5580 KB Output is correct
124 Correct 41 ms 7692 KB Output is correct
125 Correct 54 ms 10232 KB Output is correct
126 Correct 11 ms 7268 KB Output is correct
127 Correct 34 ms 8448 KB Output is correct
128 Correct 177 ms 13688 KB Output is correct
129 Correct 106 ms 11768 KB Output is correct
130 Correct 103 ms 12460 KB Output is correct
131 Correct 11 ms 6900 KB Output is correct
132 Correct 9 ms 7168 KB Output is correct
133 Correct 428 ms 14892 KB Output is correct
134 Correct 133 ms 9720 KB Output is correct
135 Correct 9 ms 5632 KB Output is correct
136 Correct 51 ms 8412 KB Output is correct
137 Correct 80 ms 9208 KB Output is correct
138 Correct 12 ms 7244 KB Output is correct