Submission #823179

# Submission time Handle Problem Language Result Execution time Memory
823179 2023-08-12T08:53:01 Z ymm Comparing Plants (IOI20_plants) C++17
100 / 100
766 ms 92500 KB
#include "plants.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= l; --x)
typedef long long ll;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
using namespace std;

const int N = 200'010;
const int lg = 20;
ll L[N][lg], R[N][lg];
int val[N], pos[N];
int n, k;

namespace seg1 {
	int a[2*N];
	void init() { fill(a, a+2*N, N); }

	void up(int p, int x) {
		p += N;
		while (p > 0) {
			a[p] = x;
			p /= 2;
		}
	}
	int get(int l, int r) {
		l += N;
		r += N;
		int ans = N;
		while (l < r) {
			if (l&1) ans = min(ans, a[l++]);
			if (r&1) ans = min(ans, a[--r]);
			l /= 2;
			r /= 2;
		}
		return ans;
	}
	int getc(int l, int r) {
		--r;
		l = (l%n+n)%n;
		r = (r%n+n)%n;
		++r;
		if (l >= r)
			return min(get(0, r), get(l, n));
		else
			return get(l, r);
	}
}

namespace seg2 {
	struct node {
		int mn;
		int lzy;
		int l, r;
		pii mxdif;
	} t[N*4];
	int sz;

	void merge(int i) {
		node &v = t[i], &l = t[2*i+1], &r = t[2*i+2];
		if (l.mn == r.mn) {
			v.mn = l.mn;
			v.l = l.l;
			v.r = r.r;
			v.mxdif = max(l.mxdif, r.mxdif);
			v.mxdif = max(v.mxdif, pii{r.l - l.r, r.l});
		} else {
			node &u = l.mn < r.mn? l: r;
			v = u;
			v.lzy = 0;
		}
	}
	void tag(int i, int x) { t[i].mn += x; t[i].lzy += x; }
	void ppg(int i) {
		if (t[i].lzy) {
			tag(2*i+1, t[i].lzy);
			tag(2*i+2, t[i].lzy);
			t[i].lzy = 0;
		}
	}

	void init(const vector<int> &vec, int i, int b, int e) {
		if (e-b == 1) {
			t[i].mn = vec[b];
			t[i].l = t[i].r = b;
			t[i].mxdif = {-1, -1};
			return;
		}
		init(vec, 2*i+1, b, (b+e)/2);
		init(vec, 2*i+2, (b+e)/2, e);
		merge(i);
	}
	void init(const vector<int> &vec) { sz = vec.size(); init(vec, 0, 0, sz); }

	void add(int l, int r, int x, int i, int b, int e) {
		if (l <= b && e <= r)
			return tag(i, x);
		if (r <= b || e <= l)
			return;
		ppg(i);
		add(l, r, x, 2*i+1, b, (b+e)/2);
		add(l, r, x, 2*i+2, (b+e)/2, e);
		merge(i);
	}
	void add(int l, int r, int x) { add(l, r, x, 0, 0, sz); }
	void addc(int l, int r, int x) {
		--r;
		l = (l%n+n)%n;
		r = (r%n+n)%n;
		++r;
		if (l >= r) {
			add(0, r, x);
			add(l, sz, x);
		} else {
			add(l, r, x);
		}
	}

	int get() {
		if (t[0].mxdif.first >= k)
			return t[0].mxdif.second;
		else
			return t[0].l;
	}
}

void init(int k, std::vector<int> r) {
	::k = k;
	n = r.size();
	seg2::init(r);
	LoopR (x,0,n) {
		int p = seg2::get();
		seg2::addc(p-k+1, p, -1);
		seg2::add(p, p+1, N);
		val[p] = x;
		pos[x] = p;
	}
	seg1::init();
	LoopR (i,0,n) {
		int l = seg1::getc(pos[i]-k+1, pos[i]);
		int r = seg1::getc(pos[i]+1, pos[i]+k);
		seg1::up(pos[i], i);
		L[pos[i]][0] = l == N? 0: (pos[i]-pos[l]+n)%n;
		R[pos[i]][0] = r == N? 0: (pos[r]-pos[i]+n)%n;
	}
	Loop (j,0,lg-1) {
		Loop (i,0,n) {
			int l = ((i - L[i][j])%n+n)%n;
			int r = ((i + R[i][j])%n+n)%n;
			L[i][j+1] = L[i][j] + L[l][j];
			R[i][j+1] = R[i][j] + R[r][j];
		}
	}
}

bool try_left(int x, int y)
{
	int x0 = x;
	ll dis = 0;
	LoopR (i,0,lg) {
		int x2 = ((x - L[x][i])%n+n)%n;
		if (val[x2] <= val[y]) {
			dis += L[x][i];
			x = x2;
		}
	}
	return (x0-y+n)%n <= dis;
}
bool try_right(int x, int y)
{
	int x0 = x;
	ll dis = 0;
	LoopR (i,0,lg) {
		int x2 = ((x + R[x][i])%n+n)%n;
		if (val[x2] <= val[y]) {
			dis += R[x][i];
			x = x2;
		}
	}
	return (y-x0+n)%n <= dis;
}
bool try_lr(int x, int y) { return try_left(x, y) || try_right(x, y); }

int compare_plants(int x, int y) {
	if (val[x] < val[y]) {
		if (try_lr(x, y))
			return -1;
		else
			return 0;
	} else {
		if (try_lr(y, x))
			return 1;
		else
			return 0;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 20564 KB Output is correct
2 Correct 9 ms 20684 KB Output is correct
3 Correct 8 ms 20564 KB Output is correct
4 Correct 8 ms 20564 KB Output is correct
5 Correct 9 ms 20564 KB Output is correct
6 Correct 121 ms 23420 KB Output is correct
7 Correct 154 ms 29996 KB Output is correct
8 Correct 407 ms 88768 KB Output is correct
9 Correct 402 ms 88656 KB Output is correct
10 Correct 463 ms 88764 KB Output is correct
11 Correct 460 ms 91588 KB Output is correct
12 Correct 442 ms 91700 KB Output is correct
13 Correct 504 ms 91680 KB Output is correct
14 Correct 544 ms 91676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 20564 KB Output is correct
2 Correct 8 ms 20644 KB Output is correct
3 Correct 10 ms 20564 KB Output is correct
4 Correct 9 ms 20576 KB Output is correct
5 Correct 10 ms 20692 KB Output is correct
6 Correct 12 ms 21064 KB Output is correct
7 Correct 119 ms 25048 KB Output is correct
8 Correct 11 ms 20820 KB Output is correct
9 Correct 13 ms 20996 KB Output is correct
10 Correct 118 ms 26940 KB Output is correct
11 Correct 112 ms 26864 KB Output is correct
12 Correct 140 ms 27064 KB Output is correct
13 Correct 119 ms 27052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 20564 KB Output is correct
2 Correct 8 ms 20644 KB Output is correct
3 Correct 10 ms 20564 KB Output is correct
4 Correct 9 ms 20576 KB Output is correct
5 Correct 10 ms 20692 KB Output is correct
6 Correct 12 ms 21064 KB Output is correct
7 Correct 119 ms 25048 KB Output is correct
8 Correct 11 ms 20820 KB Output is correct
9 Correct 13 ms 20996 KB Output is correct
10 Correct 118 ms 26940 KB Output is correct
11 Correct 112 ms 26864 KB Output is correct
12 Correct 140 ms 27064 KB Output is correct
13 Correct 119 ms 27052 KB Output is correct
14 Correct 162 ms 32212 KB Output is correct
15 Correct 725 ms 92356 KB Output is correct
16 Correct 152 ms 32276 KB Output is correct
17 Correct 707 ms 92468 KB Output is correct
18 Correct 550 ms 91980 KB Output is correct
19 Correct 584 ms 92424 KB Output is correct
20 Correct 759 ms 92500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 20564 KB Output is correct
2 Correct 9 ms 20564 KB Output is correct
3 Correct 116 ms 24064 KB Output is correct
4 Correct 525 ms 88788 KB Output is correct
5 Correct 527 ms 91756 KB Output is correct
6 Correct 593 ms 91972 KB Output is correct
7 Correct 588 ms 92256 KB Output is correct
8 Correct 766 ms 92436 KB Output is correct
9 Correct 465 ms 91780 KB Output is correct
10 Correct 489 ms 91696 KB Output is correct
11 Correct 523 ms 91608 KB Output is correct
12 Correct 617 ms 91840 KB Output is correct
13 Correct 558 ms 91904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 20564 KB Output is correct
2 Correct 11 ms 20692 KB Output is correct
3 Correct 8 ms 20584 KB Output is correct
4 Correct 10 ms 20588 KB Output is correct
5 Correct 9 ms 20712 KB Output is correct
6 Correct 11 ms 20768 KB Output is correct
7 Correct 34 ms 21336 KB Output is correct
8 Correct 33 ms 21320 KB Output is correct
9 Correct 32 ms 21312 KB Output is correct
10 Correct 27 ms 21304 KB Output is correct
11 Correct 36 ms 21296 KB Output is correct
12 Correct 34 ms 21296 KB Output is correct
13 Correct 29 ms 21424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 20564 KB Output is correct
2 Correct 8 ms 20660 KB Output is correct
3 Correct 8 ms 20580 KB Output is correct
4 Correct 8 ms 20560 KB Output is correct
5 Correct 10 ms 20956 KB Output is correct
6 Correct 426 ms 88764 KB Output is correct
7 Correct 476 ms 91184 KB Output is correct
8 Correct 598 ms 91368 KB Output is correct
9 Correct 598 ms 91452 KB Output is correct
10 Correct 420 ms 90920 KB Output is correct
11 Correct 547 ms 91556 KB Output is correct
12 Correct 469 ms 90800 KB Output is correct
13 Correct 445 ms 90980 KB Output is correct
14 Correct 507 ms 91176 KB Output is correct
15 Correct 535 ms 91384 KB Output is correct
16 Correct 424 ms 90848 KB Output is correct
17 Correct 432 ms 91020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 20564 KB Output is correct
2 Correct 9 ms 20684 KB Output is correct
3 Correct 8 ms 20564 KB Output is correct
4 Correct 8 ms 20564 KB Output is correct
5 Correct 9 ms 20564 KB Output is correct
6 Correct 121 ms 23420 KB Output is correct
7 Correct 154 ms 29996 KB Output is correct
8 Correct 407 ms 88768 KB Output is correct
9 Correct 402 ms 88656 KB Output is correct
10 Correct 463 ms 88764 KB Output is correct
11 Correct 460 ms 91588 KB Output is correct
12 Correct 442 ms 91700 KB Output is correct
13 Correct 504 ms 91680 KB Output is correct
14 Correct 544 ms 91676 KB Output is correct
15 Correct 9 ms 20564 KB Output is correct
16 Correct 8 ms 20644 KB Output is correct
17 Correct 10 ms 20564 KB Output is correct
18 Correct 9 ms 20576 KB Output is correct
19 Correct 10 ms 20692 KB Output is correct
20 Correct 12 ms 21064 KB Output is correct
21 Correct 119 ms 25048 KB Output is correct
22 Correct 11 ms 20820 KB Output is correct
23 Correct 13 ms 20996 KB Output is correct
24 Correct 118 ms 26940 KB Output is correct
25 Correct 112 ms 26864 KB Output is correct
26 Correct 140 ms 27064 KB Output is correct
27 Correct 119 ms 27052 KB Output is correct
28 Correct 162 ms 32212 KB Output is correct
29 Correct 725 ms 92356 KB Output is correct
30 Correct 152 ms 32276 KB Output is correct
31 Correct 707 ms 92468 KB Output is correct
32 Correct 550 ms 91980 KB Output is correct
33 Correct 584 ms 92424 KB Output is correct
34 Correct 759 ms 92500 KB Output is correct
35 Correct 9 ms 20564 KB Output is correct
36 Correct 9 ms 20564 KB Output is correct
37 Correct 116 ms 24064 KB Output is correct
38 Correct 525 ms 88788 KB Output is correct
39 Correct 527 ms 91756 KB Output is correct
40 Correct 593 ms 91972 KB Output is correct
41 Correct 588 ms 92256 KB Output is correct
42 Correct 766 ms 92436 KB Output is correct
43 Correct 465 ms 91780 KB Output is correct
44 Correct 489 ms 91696 KB Output is correct
45 Correct 523 ms 91608 KB Output is correct
46 Correct 617 ms 91840 KB Output is correct
47 Correct 558 ms 91904 KB Output is correct
48 Correct 10 ms 20564 KB Output is correct
49 Correct 11 ms 20692 KB Output is correct
50 Correct 8 ms 20584 KB Output is correct
51 Correct 10 ms 20588 KB Output is correct
52 Correct 9 ms 20712 KB Output is correct
53 Correct 11 ms 20768 KB Output is correct
54 Correct 34 ms 21336 KB Output is correct
55 Correct 33 ms 21320 KB Output is correct
56 Correct 32 ms 21312 KB Output is correct
57 Correct 27 ms 21304 KB Output is correct
58 Correct 36 ms 21296 KB Output is correct
59 Correct 34 ms 21296 KB Output is correct
60 Correct 29 ms 21424 KB Output is correct
61 Correct 136 ms 25812 KB Output is correct
62 Correct 162 ms 32160 KB Output is correct
63 Correct 456 ms 91680 KB Output is correct
64 Correct 517 ms 91800 KB Output is correct
65 Correct 511 ms 92000 KB Output is correct
66 Correct 565 ms 92324 KB Output is correct
67 Correct 698 ms 92412 KB Output is correct
68 Correct 506 ms 91728 KB Output is correct
69 Correct 638 ms 92476 KB Output is correct
70 Correct 558 ms 91684 KB Output is correct
71 Correct 592 ms 91856 KB Output is correct
72 Correct 567 ms 92016 KB Output is correct
73 Correct 588 ms 92224 KB Output is correct
74 Correct 464 ms 91744 KB Output is correct
75 Correct 476 ms 91828 KB Output is correct