답안 #787926

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
787926 2023-07-19T14:35:22 Z Sohsoh84 게임 (IOI13_game) C++17
100 / 100
4439 ms 237332 KB
#include "game.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define sep		' '
#define debug(x)	//cerr << #x << ": " << x << endl;
 
const ll MAXN1 = 1e6 + 10;
const ll MAXN2 = 2e7 + 10;
const ll INF = 1e9 + 1;
 
ll sg1[MAXN1], sg2[MAXN2], sz1, sz2, R, C;
int lc1[MAXN1], rc1[MAXN1], O[MAXN2];

// TODO: create node2 when neede
// O[v] < 0 => bache chap++ age -inf nabashe bache rast -O[v] va ...

inline int lc2(int v) {
	if (O[v] == 0) return 0;
	if (O[v] < 0) return v + 1;
	if (O[v] == INF) return 0;
	return O[v];
}

inline int rc2(int v) {
	if (O[v] == 0) return 0;
	if (O[v] > 0) return v + 1;
	if (-O[v] == INF) return 0;
	return -O[v];
}

inline int node2() {
	int v = ++sz2;
	//cerr << "what is this: " << v << endl;
	return v;
}

inline void cl(int v) {
	//cerr << "cl: " << v << endl;
	if (!O[v]) O[v] = -INF, node2();
	else O[v] = node2();
}

inline void cr(int v) {
	//cerr << "cr: " << v << endl;
	if (!O[v]) O[v] = INF, node2();
	else O[v] = -node2();
}
 
inline int node1() {
	int v = ++sz1;
	return v;
}
 
void update2_line(int y, ll val, int v, int bl2, int br2) {
	debug(v)
	if (bl2 == br2) {
		sg2[v] = val;		
		return;
	}
 
	int mid = (bl2 + br2) >> 1;
	if (y <= mid) {
		debug("laanati left")
		debug(O[v])
		if (!lc2(v)) cl(v);
		update2_line(y, val, lc2(v), bl2, mid);
	} else {
		debug("laanati rast")
		debug(O[v])
		if (!rc2(v)) cr(v);
		update2_line(y, val, rc2(v), mid + 1, br2);
	}

	sg2[v] = gcd(sg2[lc2(v)], sg2[rc2(v)]); // optimize
}
 
void update2_seg(int y, ll val, int v, int par_l, int par_r, int bl2, int br2) {
	debug(v)
	sg2[v] = gcd(sg2[par_l], sg2[par_r]);
	if (bl2 == br2) return;
 
	int mid = (bl2 + br2) >> 1;
	if (y <= mid) {
		debug("laaanati left")
		debug(O[v])
		if (!lc2(v)) cl(v);
		debug(O[v])
		update2_seg(y, val, lc2(v), lc2(par_l), lc2(par_r), bl2, mid);
	} else {
		debug("lanaati rast")
		if (!rc2(v)) cr(v);
		update2_seg(y, val, rc2(v), rc2(par_l), rc2(par_r), mid + 1, br2);
	}
 
}

inline int sg1c(int v) {
	if (!sg1[v]) {
		sg1[v] = node2();
	}

	return sg1[v];
}
 
void update1(int x, int y, ll val, int v, int bl1, int br1) {
	if (bl1 == br1) {	
	//	//cerr << "still fucking update1: " << bl1 << sep << br1 << endl << "and we are updating line : " << y << sep << val << endl;
		update2_line(y, val, sg1c(v), 0, C);
	} else {
		int mid = (bl1 + br1) >> 1;
		if (x <= mid) {
			if (!lc1[v]) lc1[v] = node1();
			update1(x, y, val, lc1[v], bl1, mid);
		} else {
			if (!rc1[v]) rc1[v] = node1();
			update1(x, y, val, rc1[v], mid + 1, br1);
		}
		
		//cerr << "fucking update seg: " << bl1 << sep << br1 << sep << y << sep << val << endl;
	//	//cerr << "still fucking update1: " << bl1 << sep << br1 << endl << "and we are updating segment: " << y << sep << val << endl;
		update2_seg(y, val, sg1c(v), sg1[lc1[v]], sg1[rc1[v]], 0, C);
	}
}
 
 
ll query2(int cl, int cr, int v, int bl2, int br2) {
	if (v == 0 || cl > br2 || cr < bl2) return 0;
	if (cl <= bl2 && cr >= br2) return sg2[v];
 
	int mid = (bl2 + br2) >> 1;
	return gcd(query2(cl, cr, lc2(v), bl2, mid), 
			query2(cl, cr, rc2(v), mid + 1, br2));
}
 
ll query1(int rl, int rr, int cl, int cr, int v, int bl1, int br1) {
	if (v == 0 || rl > br1 || rr < bl1) return 0; // optimize v == 0	
	if (rl <= bl1 && rr >= br1) {
		return query2(cl, cr, sg1[v], 0, C);
	}
 
	int mid = (bl1 + br1) >> 1;
	return gcd(query1(rl, rr, cl, cr, lc1[v], bl1, mid),
			query1(rl, rr, cl, cr, rc1[v], mid + 1, br1));
}
 
void init(int R_, int C_) {
	R = R_, C = C_;
	sz1 = sz2 = 1;
}
 
void update(int P, int Q, ll K) {
	update1(P, Q, K, 1, 0, R);

	//cerr << "________________________________________________________" << endl;
}
 
long long calculate(int P, int Q, int U, int V) {
	return query1(P, U, Q, V, 1, 0, R);
}
 
// TODO: change MAXN
// TODO: change init bounds
// TODO: use gcd2

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 388 ms 11960 KB Output is correct
5 Correct 272 ms 11984 KB Output is correct
6 Correct 358 ms 9364 KB Output is correct
7 Correct 413 ms 9020 KB Output is correct
8 Correct 275 ms 8000 KB Output is correct
9 Correct 393 ms 9100 KB Output is correct
10 Correct 357 ms 8772 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 687 ms 12836 KB Output is correct
13 Correct 1206 ms 6940 KB Output is correct
14 Correct 225 ms 5608 KB Output is correct
15 Correct 1378 ms 8396 KB Output is correct
16 Correct 413 ms 11676 KB Output is correct
17 Correct 581 ms 10144 KB Output is correct
18 Correct 870 ms 13048 KB Output is correct
19 Correct 813 ms 13272 KB Output is correct
20 Correct 764 ms 12628 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 356 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 394 ms 12056 KB Output is correct
13 Correct 270 ms 11840 KB Output is correct
14 Correct 360 ms 9420 KB Output is correct
15 Correct 387 ms 9064 KB Output is correct
16 Correct 280 ms 8052 KB Output is correct
17 Correct 371 ms 9212 KB Output is correct
18 Correct 390 ms 8944 KB Output is correct
19 Correct 689 ms 12744 KB Output is correct
20 Correct 1172 ms 7016 KB Output is correct
21 Correct 268 ms 5584 KB Output is correct
22 Correct 1372 ms 8364 KB Output is correct
23 Correct 405 ms 11728 KB Output is correct
24 Correct 596 ms 10148 KB Output is correct
25 Correct 902 ms 13084 KB Output is correct
26 Correct 783 ms 13432 KB Output is correct
27 Correct 778 ms 12660 KB Output is correct
28 Correct 434 ms 106136 KB Output is correct
29 Correct 1263 ms 117788 KB Output is correct
30 Correct 3493 ms 84288 KB Output is correct
31 Correct 3317 ms 66216 KB Output is correct
32 Correct 384 ms 10120 KB Output is correct
33 Correct 514 ms 11016 KB Output is correct
34 Correct 614 ms 111608 KB Output is correct
35 Correct 851 ms 63564 KB Output is correct
36 Correct 1740 ms 115644 KB Output is correct
37 Correct 1343 ms 115860 KB Output is correct
38 Correct 1465 ms 115256 KB Output is correct
39 Correct 1224 ms 91332 KB Output is correct
40 Correct 1 ms 240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 308 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 400 ms 11956 KB Output is correct
13 Correct 270 ms 11824 KB Output is correct
14 Correct 354 ms 9372 KB Output is correct
15 Correct 385 ms 9104 KB Output is correct
16 Correct 275 ms 7988 KB Output is correct
17 Correct 392 ms 9108 KB Output is correct
18 Correct 353 ms 8768 KB Output is correct
19 Correct 685 ms 12832 KB Output is correct
20 Correct 1165 ms 6932 KB Output is correct
21 Correct 224 ms 5568 KB Output is correct
22 Correct 1370 ms 8416 KB Output is correct
23 Correct 410 ms 11652 KB Output is correct
24 Correct 608 ms 10156 KB Output is correct
25 Correct 863 ms 13080 KB Output is correct
26 Correct 772 ms 13296 KB Output is correct
27 Correct 723 ms 12676 KB Output is correct
28 Correct 447 ms 106048 KB Output is correct
29 Correct 1246 ms 117960 KB Output is correct
30 Correct 3518 ms 84224 KB Output is correct
31 Correct 3322 ms 66228 KB Output is correct
32 Correct 382 ms 10196 KB Output is correct
33 Correct 519 ms 11144 KB Output is correct
34 Correct 628 ms 111568 KB Output is correct
35 Correct 914 ms 63732 KB Output is correct
36 Correct 1699 ms 115720 KB Output is correct
37 Correct 1444 ms 115804 KB Output is correct
38 Correct 1381 ms 115280 KB Output is correct
39 Correct 651 ms 211472 KB Output is correct
40 Correct 2074 ms 237332 KB Output is correct
41 Correct 4439 ms 167328 KB Output is correct
42 Correct 4145 ms 129312 KB Output is correct
43 Correct 905 ms 231904 KB Output is correct
44 Correct 480 ms 10544 KB Output is correct
45 Correct 1203 ms 91356 KB Output is correct
46 Correct 2384 ms 236076 KB Output is correct
47 Correct 2479 ms 236092 KB Output is correct
48 Correct 2414 ms 235604 KB Output is correct
49 Correct 1 ms 340 KB Output is correct