Submission #581749

# Submission time Handle Problem Language Result Execution time Memory
581749 2022-06-23T05:37:45 Z amunduzbaev Stranded Far From Home (BOI22_island) C++17
100 / 100
363 ms 39336 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;
#define int ll

const int N = 2e5 + 5;
int a[N], p[N], sz[N], par[N], cmp[N];
int sum[N], is[N], ner[N];
vector<int> edges[N];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m; cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		p[i] = i;
	}
	
	for(int i=0;i<m;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	
	sort(p + 1, p + n + 1, [&](int i, int j){
		return a[i] < a[j];
	});
	
	for(int i=1;i<=n;i++){
		cmp[i] = a[i];
		sz[i] = 1;
		par[i] = i;
	}
	vector<vector<ar<int, 2>>> last;
	function<int(int)> f = [&](int x) { return (par[x] == x ? x : f(par[x])); };
	auto merge = [&](int a, int b){
		a = f(a), b = f(b);
		if(a == b) return;
		if(sz[a] < sz[b]) swap(a, b);
		sz[a] += sz[b], par[b] = a;
		cmp[a] += cmp[b];
		last.back().push_back({a, b});
	};
	
	auto add = [&](int v){
		for(auto x : edges[v]){
			if(a[x] > a[v]) continue;
			merge(v, x);
		}
	};
	
	for(int i=1,j=1;i<=n;){
		last.push_back({});
		while(j<=n && a[p[j]] == a[p[i]]){
			add(p[j]);
			j++;
		}
		
		while(i<j){
			sum[p[i]] = cmp[f(p[i])];
			i++;
		}
	}
	//~ cout<<"here"<<endl;
	
	auto rem = [&](){
		reverse(last.back().begin(), last.back().end());
		for(auto [a, b] : last.back()){
			par[b] = b, cmp[a] -= cmp[b], sz[a] -= sz[b];
			ner[b] = ner[a];
		} last.pop_back();
	};
	
	for(int i=n,j=n;i>0;){
		while(j>0 && a[p[j]] == a[p[i]]){
			if(sum[p[j]] >= ner[f(p[j])]){
				is[p[j]] |= 1;
			}
			j--;
		}
		rem();
		
		while(i>j){
			if(is[p[i]]){
				for(auto x : edges[p[i]]){
					if(a[x] >= a[p[i]]) continue;
					ner[f(x)] = a[p[i]];
				}
			}
			i--;
		}
	}
	
	for(int i=1;i<=n;i++){
		cout<<is[i];
	} cout<<"\n";
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 4 ms 5332 KB Output is correct
5 Correct 5 ms 5368 KB Output is correct
6 Correct 5 ms 5212 KB Output is correct
7 Correct 5 ms 5332 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 7 ms 5332 KB Output is correct
11 Correct 5 ms 5332 KB Output is correct
12 Correct 5 ms 5332 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 188 ms 34944 KB Output is correct
4 Correct 172 ms 28484 KB Output is correct
5 Correct 206 ms 34124 KB Output is correct
6 Correct 232 ms 34696 KB Output is correct
7 Correct 239 ms 34948 KB Output is correct
8 Correct 218 ms 30520 KB Output is correct
9 Correct 178 ms 35556 KB Output is correct
10 Correct 148 ms 35204 KB Output is correct
11 Correct 131 ms 28880 KB Output is correct
12 Correct 209 ms 28428 KB Output is correct
13 Correct 167 ms 26552 KB Output is correct
14 Correct 148 ms 27820 KB Output is correct
15 Correct 188 ms 35648 KB Output is correct
16 Correct 123 ms 35928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 241 ms 33700 KB Output is correct
3 Correct 280 ms 34824 KB Output is correct
4 Correct 165 ms 28788 KB Output is correct
5 Correct 154 ms 26588 KB Output is correct
6 Correct 284 ms 35448 KB Output is correct
7 Correct 239 ms 36540 KB Output is correct
8 Correct 224 ms 36476 KB Output is correct
9 Correct 110 ms 35696 KB Output is correct
10 Correct 171 ms 32668 KB Output is correct
11 Correct 177 ms 28264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 285 ms 28140 KB Output is correct
3 Correct 231 ms 31736 KB Output is correct
4 Correct 354 ms 28860 KB Output is correct
5 Correct 244 ms 32576 KB Output is correct
6 Correct 297 ms 29668 KB Output is correct
7 Correct 166 ms 30348 KB Output is correct
8 Correct 117 ms 30284 KB Output is correct
9 Correct 149 ms 20460 KB Output is correct
10 Correct 254 ms 27580 KB Output is correct
11 Correct 193 ms 26824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 4 ms 5332 KB Output is correct
5 Correct 5 ms 5368 KB Output is correct
6 Correct 5 ms 5212 KB Output is correct
7 Correct 5 ms 5332 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 7 ms 5332 KB Output is correct
11 Correct 5 ms 5332 KB Output is correct
12 Correct 5 ms 5332 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 188 ms 34944 KB Output is correct
18 Correct 172 ms 28484 KB Output is correct
19 Correct 206 ms 34124 KB Output is correct
20 Correct 232 ms 34696 KB Output is correct
21 Correct 239 ms 34948 KB Output is correct
22 Correct 218 ms 30520 KB Output is correct
23 Correct 178 ms 35556 KB Output is correct
24 Correct 148 ms 35204 KB Output is correct
25 Correct 131 ms 28880 KB Output is correct
26 Correct 209 ms 28428 KB Output is correct
27 Correct 167 ms 26552 KB Output is correct
28 Correct 148 ms 27820 KB Output is correct
29 Correct 188 ms 35648 KB Output is correct
30 Correct 123 ms 35928 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 241 ms 33700 KB Output is correct
33 Correct 280 ms 34824 KB Output is correct
34 Correct 165 ms 28788 KB Output is correct
35 Correct 154 ms 26588 KB Output is correct
36 Correct 284 ms 35448 KB Output is correct
37 Correct 239 ms 36540 KB Output is correct
38 Correct 224 ms 36476 KB Output is correct
39 Correct 110 ms 35696 KB Output is correct
40 Correct 171 ms 32668 KB Output is correct
41 Correct 177 ms 28264 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 285 ms 28140 KB Output is correct
44 Correct 231 ms 31736 KB Output is correct
45 Correct 354 ms 28860 KB Output is correct
46 Correct 244 ms 32576 KB Output is correct
47 Correct 297 ms 29668 KB Output is correct
48 Correct 166 ms 30348 KB Output is correct
49 Correct 117 ms 30284 KB Output is correct
50 Correct 149 ms 20460 KB Output is correct
51 Correct 254 ms 27580 KB Output is correct
52 Correct 193 ms 26824 KB Output is correct
53 Correct 3 ms 5076 KB Output is correct
54 Correct 3 ms 4948 KB Output is correct
55 Correct 4 ms 5076 KB Output is correct
56 Correct 6 ms 5328 KB Output is correct
57 Correct 5 ms 5332 KB Output is correct
58 Correct 5 ms 5296 KB Output is correct
59 Correct 5 ms 5336 KB Output is correct
60 Correct 5 ms 5292 KB Output is correct
61 Correct 6 ms 5224 KB Output is correct
62 Correct 4 ms 5332 KB Output is correct
63 Correct 7 ms 5332 KB Output is correct
64 Correct 5 ms 5332 KB Output is correct
65 Correct 5 ms 5380 KB Output is correct
66 Correct 5 ms 5336 KB Output is correct
67 Correct 212 ms 37392 KB Output is correct
68 Correct 210 ms 28716 KB Output is correct
69 Correct 259 ms 34568 KB Output is correct
70 Correct 257 ms 34988 KB Output is correct
71 Correct 256 ms 34776 KB Output is correct
72 Correct 247 ms 29908 KB Output is correct
73 Correct 267 ms 35352 KB Output is correct
74 Correct 173 ms 35652 KB Output is correct
75 Correct 187 ms 29988 KB Output is correct
76 Correct 269 ms 28756 KB Output is correct
77 Correct 180 ms 26520 KB Output is correct
78 Correct 194 ms 27060 KB Output is correct
79 Correct 234 ms 37140 KB Output is correct
80 Correct 122 ms 37892 KB Output is correct
81 Correct 278 ms 35776 KB Output is correct
82 Correct 321 ms 35244 KB Output is correct
83 Correct 158 ms 27012 KB Output is correct
84 Correct 143 ms 27316 KB Output is correct
85 Correct 315 ms 38292 KB Output is correct
86 Correct 255 ms 39336 KB Output is correct
87 Correct 226 ms 39256 KB Output is correct
88 Correct 224 ms 31564 KB Output is correct
89 Correct 200 ms 25956 KB Output is correct
90 Correct 263 ms 28112 KB Output is correct
91 Correct 306 ms 32520 KB Output is correct
92 Correct 267 ms 32296 KB Output is correct
93 Correct 256 ms 31392 KB Output is correct
94 Correct 238 ms 28912 KB Output is correct
95 Correct 182 ms 30952 KB Output is correct
96 Correct 139 ms 29196 KB Output is correct
97 Correct 163 ms 19500 KB Output is correct
98 Correct 254 ms 26728 KB Output is correct
99 Correct 192 ms 27348 KB Output is correct
100 Correct 68 ms 11596 KB Output is correct
101 Correct 341 ms 37640 KB Output is correct
102 Correct 208 ms 25968 KB Output is correct
103 Correct 304 ms 29656 KB Output is correct
104 Correct 363 ms 34540 KB Output is correct