Submission #364360

# Submission time Handle Problem Language Result Execution time Memory
364360 2021-02-09T03:04:11 Z dennisstar Comparing Plants (IOI20_plants) C++17
100 / 100
3474 ms 223504 KB
#include "plants.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int N, K;

int st[1<<19], lz[1<<19];
void spr(int i, bool b) {
	st[i]+=lz[i];
	if (!b) for (auto &j:{i*2, i*2+1}) lz[j]+=lz[i];
	lz[i]=0;
}
void init(int i, int s, int e, vector<int> &r) {
	if (s==e) { st[i]=r[s]; return ; }
	int m=(s+e)/2;
	init(i*2, s, m, r);
	init(i*2+1, m+1, e, r);
	st[i]=max(st[i*2], st[i*2+1]);
}
void upd(int i, int s, int e, int l, int r, int v) {
	spr(i, s==e);
	if (e<l||r<s||r<l) return ;
	if (l<=s&&e<=r) {
		lz[i]=v;
		spr(i, s==e);
		return ;
	}
	int m=(s+e)/2;
	upd(i*2, s, m, l, r, v);
	upd(i*2+1, m+1, e, l, r, v);
	st[i]=max(st[i*2], st[i*2+1]);
}
int get(int i, int s, int e, int l, int r) {
	spr(i, s==e);
	if (st[i]!=K-1||e<l||r<s||r<l) return -1;
	if (s==e) return s;
	int m=(s+e)/2, v=get(i*2, s, m, l, r);
	return v!=-1?v:get(i*2+1, m+1, e, l, r);
}
int get(int l, int r) {
	if (l<0) {
		int v=get(1, 0, N-1, l+N, N-1);
		return v!=-1?v:get(1, 0, N-1, 0, r);
	}
	if (r>=N) {
		int v=get(1, 0, N-1, l, N-1);
		return v!=-1?v:get(1, 0, N-1, 0, r-N);
	}
	return get(1, 0, N-1, l, r);
}
int val(int i, int s, int e, int t) {
	spr(i, s==e);
	if (s==e) return st[i];
	int m=(s+e)/2;
	if (t<=m) return val(i*2, s, m, t);
	return val(i*2+1, m+1, e, t);
}

vector<int> lv;
vector<vector<pair<ll, int>>> L, R;

void init(int K, vector<int> r) {
	auto val = [&](int x) {
		if (get(x, x)==-1||get(x-K+1, x-1)!=-1) return false;
		return true;
	};
	auto upd = [&](int l, int r, int v) {
		if (l<0) {
			::upd(1, 0, N-1, 0, r, v);
			::upd(1, 0, N-1, l+N, N-1, v);
		}
		else ::upd(1, 0, N-1, l, r, v);
	};

	::N=r.size(), ::K=K;
	for (auto &i:r) i=K-1-i;
	init(1, 0, N-1, r);

	lv.resize(N);
	int t=0;
	vector<int> lg;
	for (int i=0; i<N; i++) if (val(i)) lg.emplace_back(i);
	while (st[1]>=0) {
		++t;
		vector<int> nx;
		for (auto &i:lg) {
			if (lv[i]) continue;
			lv[i]=t;
			upd(i, i, -(1e9));
			upd(i-K+1, i-1, 1);
			int li=get(i-K+1, i-1), ri=get(i+1, i+K-1);
			if (li!=-1&&val(li)) nx.emplace_back(li);
			if (ri!=-1&&val(ri)) nx.emplace_back(ri);
		}
		swap(lg, nx);
	}

	L.resize(N);
	R.resize(N);
	set<pair<ll, int>> lr;
	for (int i=N-K+1; i<N; i++) lr.emplace(lv[i], i);
	for (int i=0; i<N; i++) {
		auto k=lr.lower_bound({lv[i], i});
		if (k!=lr.end()) L[i].emplace_back((i-(k->second)+N)%N, k->second);
		int b=(i-K+1+N)%N;
		lr.erase({lv[b], b});
		lr.emplace(lv[i], i);
	}
	lr.clear();
	for (int i=0; i<K-1; i++) lr.emplace(lv[i], i);
	for (int i=N-1; i>=0; i--) {
		auto k=lr.lower_bound({lv[i], i});
		if (k!=lr.end()) R[i].emplace_back(((k->second)-i+N)%N, k->second);
		int b=(i+K-1)%N;
		lr.erase({lv[b], b});
		lr.emplace(lv[i], i);
	}
	for (int j=0; j<20; j++) for (int i=0; i<N; i++) {
		if (L[i].size()>j&&L[L[i][j].second].size()>j) {
			int b=L[i][j].second;
			L[i].emplace_back(L[b][j].first+L[i][j].first, L[b][j].second);
		}
		if (R[i].size()>j&&R[R[i][j].second].size()>j) {
			int b=R[i][j].second;
			R[i].emplace_back(R[b][j].first+R[i][j].first, R[b][j].second);
		}
	}
}

bool cmp(int x, int y) {
	if (lv[x]>=lv[y]) return false;
	int l=x, r=x;
	ll ld=0, rd=0;
	for (int i=19; i>=0; i--) {
		if (L[l].size()>i&&lv[L[l][i].second]<lv[y]) ld+=L[l][i].first, l=L[l][i].second;
		if (R[r].size()>i&&lv[R[r][i].second]<lv[y]) rd+=R[r][i].first, r=R[r][i].second;
	}
	for (int i=-1; i<=1; i++) if (x-ld-K+1<=y+N*i&&y+N*i<=x+rd+K-1) return true;
	return false;
}

int compare_plants(int x, int y) {
	if (cmp(x, y)) return 1;
	if (cmp(y, x)) return -1;
	return 0;
}

Compilation message

plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:121:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  121 |   if (L[i].size()>j&&L[L[i][j].second].size()>j) {
      |       ~~~~~~~~~~~^~
plants.cpp:121:46: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  121 |   if (L[i].size()>j&&L[L[i][j].second].size()>j) {
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~^~
plants.cpp:125:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  125 |   if (R[i].size()>j&&R[R[i][j].second].size()>j) {
      |       ~~~~~~~~~~~^~
plants.cpp:125:46: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  125 |   if (R[i].size()>j&&R[R[i][j].second].size()>j) {
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~^~
plants.cpp: In function 'bool cmp(int, int)':
plants.cpp:137:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  137 |   if (L[l].size()>i&&lv[L[l][i].second]<lv[y]) ld+=L[l][i].first, l=L[l][i].second;
      |       ~~~~~~~~~~~^~
plants.cpp:138:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  138 |   if (R[r].size()>i&&lv[R[r][i].second]<lv[y]) rd+=R[r][i].first, r=R[r][i].second;
      |       ~~~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 75 ms 3180 KB Output is correct
7 Correct 176 ms 7916 KB Output is correct
8 Correct 820 ms 35996 KB Output is correct
9 Correct 927 ms 47724 KB Output is correct
10 Correct 1046 ms 66972 KB Output is correct
11 Correct 1094 ms 72812 KB Output is correct
12 Correct 1163 ms 82704 KB Output is correct
13 Correct 1325 ms 106552 KB Output is correct
14 Correct 1223 ms 106476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 8 ms 1004 KB Output is correct
7 Correct 179 ms 6636 KB Output is correct
8 Correct 3 ms 492 KB Output is correct
9 Correct 8 ms 1004 KB Output is correct
10 Correct 173 ms 6636 KB Output is correct
11 Correct 139 ms 5100 KB Output is correct
12 Correct 139 ms 5356 KB Output is correct
13 Correct 163 ms 6636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 8 ms 1004 KB Output is correct
7 Correct 179 ms 6636 KB Output is correct
8 Correct 3 ms 492 KB Output is correct
9 Correct 8 ms 1004 KB Output is correct
10 Correct 173 ms 6636 KB Output is correct
11 Correct 139 ms 5100 KB Output is correct
12 Correct 139 ms 5356 KB Output is correct
13 Correct 163 ms 6636 KB Output is correct
14 Correct 386 ms 16420 KB Output is correct
15 Correct 3304 ms 201884 KB Output is correct
16 Correct 381 ms 16492 KB Output is correct
17 Correct 3170 ms 205712 KB Output is correct
18 Correct 1646 ms 116040 KB Output is correct
19 Correct 1546 ms 115948 KB Output is correct
20 Correct 3474 ms 223504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 118 ms 4172 KB Output is correct
4 Correct 1466 ms 103020 KB Output is correct
5 Correct 1526 ms 111620 KB Output is correct
6 Correct 1799 ms 123444 KB Output is correct
7 Correct 2161 ms 128108 KB Output is correct
8 Correct 3008 ms 191200 KB Output is correct
9 Correct 1383 ms 86124 KB Output is correct
10 Correct 1398 ms 108012 KB Output is correct
11 Correct 1314 ms 106604 KB Output is correct
12 Correct 1306 ms 106476 KB Output is correct
13 Correct 1617 ms 112352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 3 ms 364 KB Output is correct
7 Correct 20 ms 1004 KB Output is correct
8 Correct 22 ms 1260 KB Output is correct
9 Correct 23 ms 1388 KB Output is correct
10 Correct 25 ms 1388 KB Output is correct
11 Correct 21 ms 1388 KB Output is correct
12 Correct 22 ms 1536 KB Output is correct
13 Correct 22 ms 1516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 4 ms 748 KB Output is correct
6 Correct 1236 ms 74644 KB Output is correct
7 Correct 1446 ms 117580 KB Output is correct
8 Correct 1704 ms 130156 KB Output is correct
9 Correct 2685 ms 180496 KB Output is correct
10 Correct 1071 ms 88044 KB Output is correct
11 Correct 1829 ms 165420 KB Output is correct
12 Correct 1231 ms 105076 KB Output is correct
13 Correct 1404 ms 113772 KB Output is correct
14 Correct 1585 ms 125804 KB Output is correct
15 Correct 1939 ms 130704 KB Output is correct
16 Correct 1231 ms 107812 KB Output is correct
17 Correct 1261 ms 110972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 75 ms 3180 KB Output is correct
7 Correct 176 ms 7916 KB Output is correct
8 Correct 820 ms 35996 KB Output is correct
9 Correct 927 ms 47724 KB Output is correct
10 Correct 1046 ms 66972 KB Output is correct
11 Correct 1094 ms 72812 KB Output is correct
12 Correct 1163 ms 82704 KB Output is correct
13 Correct 1325 ms 106552 KB Output is correct
14 Correct 1223 ms 106476 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 8 ms 1004 KB Output is correct
21 Correct 179 ms 6636 KB Output is correct
22 Correct 3 ms 492 KB Output is correct
23 Correct 8 ms 1004 KB Output is correct
24 Correct 173 ms 6636 KB Output is correct
25 Correct 139 ms 5100 KB Output is correct
26 Correct 139 ms 5356 KB Output is correct
27 Correct 163 ms 6636 KB Output is correct
28 Correct 386 ms 16420 KB Output is correct
29 Correct 3304 ms 201884 KB Output is correct
30 Correct 381 ms 16492 KB Output is correct
31 Correct 3170 ms 205712 KB Output is correct
32 Correct 1646 ms 116040 KB Output is correct
33 Correct 1546 ms 115948 KB Output is correct
34 Correct 3474 ms 223504 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 118 ms 4172 KB Output is correct
38 Correct 1466 ms 103020 KB Output is correct
39 Correct 1526 ms 111620 KB Output is correct
40 Correct 1799 ms 123444 KB Output is correct
41 Correct 2161 ms 128108 KB Output is correct
42 Correct 3008 ms 191200 KB Output is correct
43 Correct 1383 ms 86124 KB Output is correct
44 Correct 1398 ms 108012 KB Output is correct
45 Correct 1314 ms 106604 KB Output is correct
46 Correct 1306 ms 106476 KB Output is correct
47 Correct 1617 ms 112352 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 1 ms 364 KB Output is correct
52 Correct 1 ms 364 KB Output is correct
53 Correct 3 ms 364 KB Output is correct
54 Correct 20 ms 1004 KB Output is correct
55 Correct 22 ms 1260 KB Output is correct
56 Correct 23 ms 1388 KB Output is correct
57 Correct 25 ms 1388 KB Output is correct
58 Correct 21 ms 1388 KB Output is correct
59 Correct 22 ms 1536 KB Output is correct
60 Correct 22 ms 1516 KB Output is correct
61 Correct 93 ms 5356 KB Output is correct
62 Correct 170 ms 9708 KB Output is correct
63 Correct 1037 ms 48620 KB Output is correct
64 Correct 1378 ms 75580 KB Output is correct
65 Correct 1584 ms 118800 KB Output is correct
66 Correct 1896 ms 131044 KB Output is correct
67 Correct 2932 ms 180924 KB Output is correct
68 Correct 1293 ms 89068 KB Output is correct
69 Correct 2120 ms 166636 KB Output is correct
70 Correct 1393 ms 105964 KB Output is correct
71 Correct 1532 ms 114680 KB Output is correct
72 Correct 1805 ms 126860 KB Output is correct
73 Correct 2088 ms 131732 KB Output is correct
74 Correct 1035 ms 57964 KB Output is correct
75 Correct 1306 ms 115388 KB Output is correct