Submission #97811

# Submission time Handle Problem Language Result Execution time Memory
97811 2019-02-18T16:07:12 Z tincamatei Game (IOI13_game) C++14
80 / 100
13000 ms 8328 KB
#include "game.h"
#include <bits/stdc++.h>

using namespace std;

const int BIGBUCK = 785;
const int LILBUCK = 28;
const int MAX_N = 22000;

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

int R, C;

struct Cell {
	int r, c;
	long long val;
	bool operator< (const Cell &a) const {
		return r < a.r || (r == a.r && c < a.c);
	}
};

bool cmp(Cell a, Cell b) {
	return a.c < b.c;
}

struct Bucket {
	int x1, x2;
	int sizebuck, nrb;
	Cell cell[BIGBUCK];
	int y1[BIGBUCK / LILBUCK + 1], y2[BIGBUCK / LILBUCK + 1];
	long long buckRez[BIGBUCK / LILBUCK + 1];

	void init() {
		x1 = x2 = 0;
		sizebuck = nrb = 0;
		for(int i = 0; i < BIGBUCK; ++i)
			cell[i] = {0, 0, 0};
		for(int i = 0; i < BIGBUCK / LILBUCK + 1; ++i)
			y1[i] = y2[i] = buckRez[i] = 0;
	}

	void pushCell(Cell x) {
		cell[sizebuck++] = x;
	}

    void replaceCell(Cell x) {
        for(int i = 0; i < sizebuck; ++i)
            if(cell[i].r == x.r && cell[i].c == x.c)
                cell[i].val = x.val;
        this->build();
    }

	void build() {
		std::sort(cell, cell + sizebuck, cmp);
		for(int i = 0; i < sizebuck; ++i) {
			if(i % LILBUCK == 0) {
				buckRez[nrb] = 0;
				y1[nrb] = cell[i].c;
				++nrb;
			}
			buckRez[nrb - 1] = gcd2(buckRez[nrb - 1], cell[i].val);
			y2[nrb - 1] = cell[i].c;
		}
	}

	long long stankyleg(int yl, int yr) {
		long long rez = 0LL;
		for(int i = 0; i < nrb; ++i)
			if(yl <= y1[i] && y2[i] <= yr)
				rez = gcd2(rez, buckRez[i]);
			else if(!(yr < y1[i] || y2[i] < yl)) {
				for(int j = i * LILBUCK; j < (i + 1) * LILBUCK; ++j)
					if(yl <= cell[j].c && cell[j].c <= yr)
						rez = gcd2(rez, cell[j].val);
			}
		return rez;
	}

	void debug() {
		printf("number of cells in bucket: %d; number of bucklets: %d\n", sizebuck, nrb);
		printf("Row bounds %d %d\n", x1, x2);
		for(int i = 0; i < sizebuck; ++i)
			printf("Cell %d-> %d %d %lld\n", i, cell[i].r, cell[i].c, cell[i].val);
		for(int j = 0; j < nrb; ++j) {
			printf("Bucklet %d; bounds: %d %d; gcd: %lld\n", j, y1[j], y2[j], buckRez[j]);
		}
	}
};

map<pair<int, int>, long long> cellMap;
set<Cell> cells;

struct ThisIsEpic {
	int nrcells, nrb;
	Cell cell[MAX_N];
	Bucket dothe[MAX_N / BIGBUCK + 1];
    Cell extra[BIGBUCK];
    int nex;

	void init() {
	    nex = 0;
		nrcells = nrb = 0;
	}

    void lazyAdd(Cell x) {
        cell[nrcells++] = x;
        if(nex == BIGBUCK)
            this->build();
        else
            extra[nex++] = x;
    }

	void pushCell(Cell x) {
		if(nrcells % BIGBUCK == 0) {
			dothe[nrb].init();
			dothe[nrb].x1  = x.r;
			nrb++;
		}

		dothe[nrb - 1].x2 = x.r;
		dothe[nrb - 1].pushCell(x);
		cell[nrcells++] = x;
	}

	void build() {
		this->init();

		int i = 0;
		for(auto it: cells)
			pushCell(it);
		for(int i = 0; i < nrb; ++i)
			dothe[i].build();
	}

	long long BenShapiro(int xl, int xr, int yl, int yr) {
		long long rez = 0LL;
		for(int i = 0; i < nrb; ++i)
			if(xl <= dothe[i].x1 && dothe[i].x2 <= xr)
				rez = gcd2(rez, dothe[i].stankyleg(yl, yr));
			else if(!(xr < dothe[i].x1 || dothe[i].x2 < xl)) {
				for(int j = 0; j < dothe[i].sizebuck; ++j)
					if(xl <= dothe[i].cell[j].r && dothe[i].cell[j].r <= xr &&
					   yl <= dothe[i].cell[j].c && dothe[i].cell[j].c <= yr)
						rez = gcd2(rez, dothe[i].cell[j].val);
			}

        for(int i = 0; i < nex; ++i)
            if(xl <= extra[i].r && extra[i].r <= xr &&
			   yl <= extra[i].c && extra[i].c <= yr)
            rez = gcd2(rez, extra[i].val);
		return rez;
	}

	void debug() {
		printf("Number of cells: %d\nNumber of buckets: %d\n", nrcells, nrb);
		for(int i = 0; i < nrb; ++i) {
			printf("BUCKET %d\n", i);
			dothe[i].debug();
		}
		printf("------------------------------------------------------------------\n");
	}
} xdxd;

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

void update(int P, int Q, long long K) {
	//printf("UPDATE: %d %d %lld\n", P, Q, K);
	if(cellMap[make_pair(P, Q)] != 0) {
		cells.erase({P, Q, cellMap[make_pair(P, Q)] - 1});
        cells.insert({P, Q, K});
        xdxd.build();
	} else {
        cells.insert({P, Q, K});
        xdxd.lazyAdd({P, Q, K});
	}
	cellMap[make_pair(P, Q)] = K + 1;

	//printf("CELLS!!!!!!\n");
	//for(auto it: cells)
	//	printf("%d %d %d\n", it.r, it.c, it.val);
	//printf("/CELLS\n");
	//xdxd.debug();
}

long long calculate(int P, int Q, int U, int V) {
	return xdxd.BenShapiro(P, U, Q, V);
}

Compilation message

grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
  int res;
      ^~~
game.cpp: In member function 'void ThisIsEpic::build()':
game.cpp:136:7: warning: unused variable 'i' [-Wunused-variable]
   int i = 0;
       ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 256 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 256 KB Output is correct
4 Correct 6775 ms 6052 KB Output is correct
5 Correct 3615 ms 6228 KB Output is correct
6 Correct 3012 ms 3108 KB Output is correct
7 Correct 3045 ms 2816 KB Output is correct
8 Correct 1662 ms 2768 KB Output is correct
9 Correct 2950 ms 2996 KB Output is correct
10 Correct 2796 ms 2648 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 6579 ms 6044 KB Output is correct
13 Correct 11385 ms 2280 KB Output is correct
14 Correct 1073 ms 3804 KB Output is correct
15 Correct 11729 ms 5160 KB Output is correct
16 Correct 4748 ms 5544 KB Output is correct
17 Correct 2620 ms 5316 KB Output is correct
18 Correct 4091 ms 5944 KB Output is correct
19 Correct 3558 ms 6224 KB Output is correct
20 Correct 3495 ms 5592 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 356 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 6327 ms 5948 KB Output is correct
13 Correct 3797 ms 6260 KB Output is correct
14 Correct 2887 ms 3156 KB Output is correct
15 Correct 3037 ms 2860 KB Output is correct
16 Correct 1673 ms 2800 KB Output is correct
17 Correct 2907 ms 2988 KB Output is correct
18 Correct 3058 ms 2644 KB Output is correct
19 Correct 6488 ms 6016 KB Output is correct
20 Correct 11033 ms 2468 KB Output is correct
21 Correct 978 ms 3756 KB Output is correct
22 Correct 11862 ms 5220 KB Output is correct
23 Correct 4705 ms 5640 KB Output is correct
24 Correct 2652 ms 5320 KB Output is correct
25 Correct 4001 ms 5960 KB Output is correct
26 Correct 3273 ms 5924 KB Output is correct
27 Correct 3450 ms 5348 KB Output is correct
28 Correct 2035 ms 4416 KB Output is correct
29 Correct 9210 ms 7548 KB Output is correct
30 Correct 12295 ms 4424 KB Output is correct
31 Correct 11035 ms 4280 KB Output is correct
32 Correct 1155 ms 2936 KB Output is correct
33 Correct 2142 ms 3080 KB Output is correct
34 Correct 4745 ms 4792 KB Output is correct
35 Correct 2700 ms 4492 KB Output is correct
36 Correct 4532 ms 5068 KB Output is correct
37 Correct 3694 ms 5036 KB Output is correct
38 Correct 3554 ms 4636 KB Output is correct
39 Correct 3400 ms 4600 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 1 ms 356 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 6239 ms 6052 KB Output is correct
13 Correct 3658 ms 6396 KB Output is correct
14 Correct 2861 ms 3192 KB Output is correct
15 Correct 2766 ms 2824 KB Output is correct
16 Correct 1615 ms 2840 KB Output is correct
17 Correct 2796 ms 2968 KB Output is correct
18 Correct 2738 ms 2468 KB Output is correct
19 Correct 6118 ms 6036 KB Output is correct
20 Correct 10927 ms 4976 KB Output is correct
21 Correct 1091 ms 3964 KB Output is correct
22 Correct 10762 ms 5336 KB Output is correct
23 Correct 4688 ms 5624 KB Output is correct
24 Correct 2582 ms 5260 KB Output is correct
25 Correct 3801 ms 5976 KB Output is correct
26 Correct 3265 ms 6100 KB Output is correct
27 Correct 3320 ms 5512 KB Output is correct
28 Correct 1885 ms 4288 KB Output is correct
29 Correct 7778 ms 7496 KB Output is correct
30 Correct 11589 ms 4340 KB Output is correct
31 Correct 10520 ms 4080 KB Output is correct
32 Correct 1102 ms 2680 KB Output is correct
33 Correct 2028 ms 2724 KB Output is correct
34 Correct 4697 ms 4404 KB Output is correct
35 Correct 2788 ms 4120 KB Output is correct
36 Correct 4465 ms 4772 KB Output is correct
37 Correct 3813 ms 4992 KB Output is correct
38 Correct 3522 ms 4472 KB Output is correct
39 Correct 2053 ms 6136 KB Output is correct
40 Correct 10406 ms 8328 KB Output is correct
41 Execution timed out 13007 ms 5848 KB Time limit exceeded
42 Halted 0 ms 0 KB -