Submission #437319

# Submission time Handle Problem Language Result Execution time Memory
437319 2021-06-26T07:13:00 Z denverjin Game (IOI13_game) C++14
80 / 100
2650 ms 96820 KB
#include "game.h"
#include <bits/stdc++.h>

typedef long long i64;

i64 gcd(i64 X, i64 Y) {
	i64 tmp;
	while (X != Y && Y != 0) {
		tmp = X;
		X = Y;
		Y = tmp % Y;
	}
	return X;
}

struct Node {
	int x, lx, rx;
	i64 v, s;
	int sz;
	int ls, rs;
} t[22000 * 30];

const int null = 0;
int ndsz;

int new_node(int x, i64 v) {
	int i = ++ ndsz;
	t[i].x = t[i].lx = t[i].rx = x;
	t[i].v = t[i].s = v;
	t[i].sz = 1;
	t[i].ls = t[i].rs = null;
	return i;
}

void pushup(int x) {
	t[x].sz = t[t[x].ls].sz + 1 + t[t[x].rs].sz;
	t[x].s = gcd(t[t[x].ls].s, gcd(t[x].v, t[t[x].rs].s));
	t[x].lx = t[x].ls ? t[t[x].ls].lx : t[x].x;
	t[x].rx = t[x].rs ? t[t[x].rs].rx : t[x].x;
}

void split(int x, i64 v, int &a, int &b) {
	if (!x) return void(a = b = null);
	if (t[x].x <= v) a = x, split(t[x].rs, v, t[a].rs, b), pushup(a);
	else b = x, split(t[x].ls, v, a, t[b].ls), pushup(b);
}

std::mt19937 rng;

int rand(int l, int r) { return rng() % (r - l + 1) + l; }

int merge(int a, int b) {
	if (!a || !b) return a | b;
	if (rand(1, t[a].sz + t[b].sz) <= t[a].sz) return t[a].rs = merge(t[a].rs, b), pushup(a), a;
	else return t[b].ls = merge(a, t[b].ls), pushup(b), b;
}

void change(int &i, int p, i64 v) {
	int a, b, c;
	split(i, p, b, c);
	split(b, p - 1, a, b);
	if (!b) b = new_node(p, v);
	else t[b].v = t[b].s = v;
	i = merge(merge(a, b), c);
}

i64 query(int i, int l, int r) {
	if (i == null) return 0;
	if (l <= t[i].lx && t[i].rx <= r) return t[i].s;
	if (r < t[i].lx || t[i].rx < l) return 0;
	i64 ans = 0;
	if (l <= t[i].x && t[i].x <= r) ans = gcd(ans, t[i].v);
	if (l < t[i].x && t[i].ls) ans = gcd(ans, query(t[i].ls, l, r));
	if (r > t[i].x && t[i].rs) ans = gcd(ans, query(t[i].rs, l, r));
	return ans;
}

struct SegNode {
	int nd;
	SegNode *ls, *rs;
};

void change(int x, int y, i64 v, SegNode *&i, int a, int b) {
	if (!i) i = new SegNode{0, nullptr, nullptr};
	if (a + 1 == b) return change(i->nd, y, v);
	int m = (a + b) >> 1;
	if (x < m) change(x, y, v, i->ls, a, m);
	else change(x, y, v, i->rs, m, b);
	change(i->nd, y, gcd(i->ls ? query(i->ls->nd, y, y) : 0, i->rs ? query(i->rs->nd, y, y) : 0));
}

i64 query(int l, int r, int L, int R, SegNode *i, int a, int b) {
	if (!i) return 0;
	if (l <= a && b <= r) return query(i->nd, L, R);
	if (r <= a || b <= l) return 0;
	int m = (a + b) >> 1;
	return gcd(query(l, r, L, R, i->ls, a, m), query(l, r, L, R, i->rs, m, b));
}

int R, C;
SegNode *rt;

void init(int _R, int _C) {
	R = _R; C = _C;
	rt = nullptr;
}

void update(int P, int Q, i64 K) {
	change(P, Q, K, rt, 0, R);
}

i64 calculate(int P, int Q, int U, int V) {
	return query(P, U + 1, Q, V, rt, 0, R);
}

#ifdef DEBUG
#include "grader.c"
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 700 ms 6240 KB Output is correct
5 Correct 349 ms 6688 KB Output is correct
6 Correct 666 ms 3340 KB Output is correct
7 Correct 736 ms 3012 KB Output is correct
8 Correct 438 ms 2948 KB Output is correct
9 Correct 751 ms 3312 KB Output is correct
10 Correct 691 ms 2832 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1051 ms 7928 KB Output is correct
13 Correct 1673 ms 3016 KB Output is correct
14 Correct 337 ms 816 KB Output is correct
15 Correct 1729 ms 3640 KB Output is correct
16 Correct 344 ms 5572 KB Output is correct
17 Correct 892 ms 4228 KB Output is correct
18 Correct 1490 ms 6032 KB Output is correct
19 Correct 1458 ms 6092 KB Output is correct
20 Correct 1278 ms 5640 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 750 ms 6536 KB Output is correct
13 Correct 336 ms 6656 KB Output is correct
14 Correct 660 ms 3368 KB Output is correct
15 Correct 758 ms 3036 KB Output is correct
16 Correct 443 ms 2920 KB Output is correct
17 Correct 723 ms 3424 KB Output is correct
18 Correct 685 ms 2756 KB Output is correct
19 Correct 1012 ms 8076 KB Output is correct
20 Correct 1617 ms 3016 KB Output is correct
21 Correct 328 ms 872 KB Output is correct
22 Correct 1736 ms 3836 KB Output is correct
23 Correct 354 ms 5572 KB Output is correct
24 Correct 865 ms 4084 KB Output is correct
25 Correct 1585 ms 6048 KB Output is correct
26 Correct 1315 ms 6096 KB Output is correct
27 Correct 1408 ms 5444 KB Output is correct
28 Correct 550 ms 21188 KB Output is correct
29 Correct 1697 ms 24128 KB Output is correct
30 Correct 2084 ms 17020 KB Output is correct
31 Correct 1920 ms 13536 KB Output is correct
32 Correct 519 ms 884 KB Output is correct
33 Correct 646 ms 1368 KB Output is correct
34 Correct 467 ms 21048 KB Output is correct
35 Correct 1168 ms 11688 KB Output is correct
36 Correct 2600 ms 21788 KB Output is correct
37 Correct 2033 ms 21620 KB Output is correct
38 Correct 2029 ms 21148 KB Output is correct
39 Correct 1601 ms 16868 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 711 ms 6276 KB Output is correct
13 Correct 329 ms 6724 KB Output is correct
14 Correct 660 ms 3440 KB Output is correct
15 Correct 742 ms 3060 KB Output is correct
16 Correct 452 ms 2828 KB Output is correct
17 Correct 720 ms 3144 KB Output is correct
18 Correct 687 ms 2828 KB Output is correct
19 Correct 1035 ms 8036 KB Output is correct
20 Correct 1677 ms 3120 KB Output is correct
21 Correct 315 ms 836 KB Output is correct
22 Correct 1733 ms 3712 KB Output is correct
23 Correct 355 ms 5540 KB Output is correct
24 Correct 862 ms 4036 KB Output is correct
25 Correct 1549 ms 6268 KB Output is correct
26 Correct 1298 ms 6132 KB Output is correct
27 Correct 1257 ms 5552 KB Output is correct
28 Correct 542 ms 20820 KB Output is correct
29 Correct 1668 ms 24024 KB Output is correct
30 Correct 2084 ms 16992 KB Output is correct
31 Correct 1921 ms 13148 KB Output is correct
32 Correct 517 ms 864 KB Output is correct
33 Correct 656 ms 1316 KB Output is correct
34 Correct 461 ms 21092 KB Output is correct
35 Correct 1180 ms 11752 KB Output is correct
36 Correct 2650 ms 21624 KB Output is correct
37 Correct 2009 ms 21744 KB Output is correct
38 Correct 2013 ms 21036 KB Output is correct
39 Runtime error 865 ms 96820 KB Execution killed with signal 11
40 Halted 0 ms 0 KB -