답안 #115079

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
115079 2019-06-05T07:33:10 Z 윤교준(#2865) Iqea (innopolis2018_final_C) C++14
17 / 100
2000 ms 61968 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
using namespace std;
typedef pair<int, int> pii;

const int MAXN = 100055;
const int MAXQ = 100055;
const int MAXK = MAXN;

int getDist(pii, pii);
int getDist(pii, int);

struct DJF {
	DJF() { init(); }
	int ud[MAXN];

	void init() { iota(ud, ud+MAXN, 0); }
	int uf(int i) { return i == ud[i] ? i : (ud[i] = uf(ud[i])); }
	void uf(int a, int b) { ud[uf(b)] = uf(a); }
};

struct BIT {
	pii *d;
	int n;

	void init(int _n) {
		n = _n+5;
		d = new pii[n];
		fill(d, d+n, pii(INF, -1));
	}
	void upd(int x, pii r) {
		for(x += 2; x < n; x += rb(x))
			if(r < d[x]) d[x] = r;
	}
	pii get(int x) {
		pii r(INF, -1);
		for(x += 2; x; x -= rb(x))
			if(d[x] < r) r = d[x];
		return r;
	}
};

struct TRE {
	vector<int> G[MAXN];
	int prt[17][MAXN], dep[MAXN];

	void add(int a, int b) {
		G[a].eb(b);
		G[b].eb(a);
	}
	void dfs(int i) {
		for(int v : G[i]) if(!dep[v]) {
			dep[v] = dep[i] + 1;
			prt[0][v] = i;
			dfs(v);
		}
	}
	void cal() {
		for(int i = 1; i < MAXN; i++) if(!dep[i]) {
			dep[i] = 1;
			dfs(i);
		}
		for(int j = 1; j < 17; j++) for(int i = 1; i < MAXN; i++)
			prt[j][i] = prt[j-1][prt[j-1][i]];
	}
	int lca(int a, int b) {
		if(dep[a] > dep[b]) swap(a, b);
		const int dt = dep[b] - dep[a];
		for(int i = 0; i < 17; i++) if(dt & (1<<i)) b = prt[i][b];
		if(a == b) return a;
		for(int i = 17; i--;) if(prt[i][a] != prt[i][b]) {
			a = prt[i][a];
			b = prt[i][b];
		}
		return prt[0][a];
	}
	int get(int a, int b) {
		return dep[a] + dep[b] - (dep[lca(a, b)] << 1);
	}
} treX, treY;

struct NOD {
	NOD() : sy(INF), ey(-INF) {}
	BIT bitu, bitd;
	int x, sy, ey;

	void init() {
		int l = ey-sy+1;
		bitu.init(l);
		bitd.init(l);
	}

	void addCell(pii p) {
		x = p.first;
		if(p.second < sy) sy = p.second;
		if(ey < p.second) ey = p.second;
	}

	int getY(pii p) {
		int s = sy, e = ey; for(int m, l, r; s < e;) {
			m = (s+e) >> 1;
			l = ::getDist(p, pii(x, m));
			r = ::getDist(p, pii(x, m+1));
			if(l < r) e = m;
			else s = m+1;
		}
		return s;
	}

	void addHubo(pii p, int idx) {
		int s = getY(p), d = ::getDist(p, pii(x, s));
		bitu.upd(s-sy, pii(-s+d, idx));
		bitd.upd(ey-s, pii(s+d, idx));
	}
	int get(pii p) {
		int r = INF, y = getY(p);
		pii up = bitu.get(y-sy), dp = bitd.get(ey-y);
		if(0 < up.second) {
			int t = ::getDist(p, up.second);
			if(t < r) r = t;
		}
		if(0 < dp.second) {
			int t = ::getDist(p, dp.second);
			if(t < r) r = t;
		}
		return r;
	}
} nod[MAXK];
vector<int> G[MAXK];
int prt[MAXK], cnt[MAXK];

map<pii, int> MPX, MPY, MPK;
pii P[MAXN], QP[MAXQ];

int N, Q, K, Krt;

int getDist(pii a, pii b) {
	return treX.get(MPX[a], MPX[b]) + treY.get(MPY[a], MPY[b]);
}
int getDist(pii p, int idx) { return getDist(p, QP[idx]); }

void predfs(int i) {
	cnt[i] = 1;
	for(int v : G[i]) if(!cnt[v]) {
		predfs(v);
		cnt[i] += cnt[v];
	}
}
void cent(int &rt) {
	int N = cnt[rt];
	for(int hi, hc;;) {
		hi = hc = -1;
		for(int v : G[rt]) {
			if(N <= cnt[v] || cnt[v] <= hc) continue;
			hc = cnt[v];
			hi = v;
		}
		if(hc*2 <= N) break;
		cnt[rt] = N - hc;
		cnt[hi] = N;
		rt = hi;
	}
}
int centdfs(int i) {
	cent(i);
	for(int v : G[i]) if(cnt[v] < cnt[i]) {
		v = centdfs(v);
		prt[v] = i;
	}
	return i;
}

void initK() {
	{
		vector<int> V;
		for(auto &v : MPX) V.eb(v.second);
		sorv(V); univ(V);
		K = sz(V);
		for(int i = 1; i <= N; i++)
			MPK[P[i]] = int(lower_bound(allv(V), MPX[P[i]]) - V.begin()) + 1;
	}

	for(int i = 1, idx; i <= N; i++) {
		idx = MPK[P[i]];
		nod[idx].addCell(P[i]);

		pii p(P[i].first + 1, P[i].second);
		auto it = MPK.find(p);
		if(MPK.end() != it) {
			G[idx].eb(it->second);
			G[it->second].eb(idx);
		}
	}

	for(int i = 1; i <= K; i++) {
		sorv(G[i]);
		univ(G[i]);
		nod[i].init();
	}

	predfs(1);
	Krt = centdfs(1);
}

void initTree(map<pii, int> &MP, TRE &tree, int dx) {
	DJF djf;
	for(int i = 1; i <= N; i++) MP[P[i]] = i;
	for(int i = 1; i <= N; i++) {
		pii p(P[i].first + dx, P[i].second + !dx);
		auto it = MP.find(p);
		if(MP.end() != it) djf.uf(i, it->second);
	}
	for(int i = 1; i <= N; i++) MP[P[i]] = djf.uf(i);
	for(int i = 1; i <= N; i++) {
		pii p(P[i].first + !dx, P[i].second + dx);
		auto it = MP.find(p);
		if(MP.end() != it) tree.add(djf.uf(i), it->second);
	}
	tree.cal();
}

void upd(pii p, int idx) {
	for(int i = MPK[p]; i;) {
		//printf("upd : p = (%d, %d), idx = %d, i = %d, prt = %d\n", p.first, p.second, idx, i, prt[i]);
		nod[i].addHubo(p, idx);
		i = prt[i];
	}
}
int get(pii p) {
	int r = INF;
	for(int i = MPK[p], t; i;) {
		t = nod[i].get(p);
		//printf("get : p = (%d, %d), i = %d, t = %d, prt = %d\n", p.first, p.second, i, t, prt[i]);
		if(t < r) r = t;
		i = prt[i];
	}
	return r;
}

int main() {
	ios::sync_with_stdio(false);

	cin >> N;
	for(int i = 1; i <= N; i++)
		cin >> P[i].first >> P[i].second;

	initTree(MPX, treX, 0);
	initTree(MPY, treY, 1);
	initK();

	cin >> Q;
	for(int qi = 1, t, x, y; qi <= Q; qi++) {
		cin >> t >> x >> y;
		if(1 == t) {
			QP[qi] = pii(x, y);
			upd(pii(x, y), qi);
		} else {
			t = get(pii(x, y));
			printf("%d\n", INF <= t ? -1 : t);
		}
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 25848 KB Output is correct
2 Correct 23 ms 25856 KB Output is correct
3 Correct 25 ms 25856 KB Output is correct
4 Correct 151 ms 27100 KB Output is correct
5 Correct 182 ms 27132 KB Output is correct
6 Correct 151 ms 27384 KB Output is correct
7 Correct 110 ms 27672 KB Output is correct
8 Correct 108 ms 27692 KB Output is correct
9 Correct 107 ms 27640 KB Output is correct
10 Correct 132 ms 27384 KB Output is correct
11 Correct 120 ms 27460 KB Output is correct
12 Correct 124 ms 27456 KB Output is correct
13 Correct 114 ms 27640 KB Output is correct
14 Correct 131 ms 27640 KB Output is correct
15 Correct 109 ms 27512 KB Output is correct
16 Correct 115 ms 27684 KB Output is correct
17 Correct 113 ms 27640 KB Output is correct
18 Correct 136 ms 27384 KB Output is correct
19 Correct 152 ms 27336 KB Output is correct
20 Correct 128 ms 27544 KB Output is correct
21 Correct 131 ms 27624 KB Output is correct
22 Correct 114 ms 27768 KB Output is correct
23 Correct 97 ms 27640 KB Output is correct
24 Correct 81 ms 27784 KB Output is correct
25 Correct 71 ms 27896 KB Output is correct
26 Correct 86 ms 27640 KB Output is correct
27 Correct 83 ms 27384 KB Output is correct
28 Correct 82 ms 27768 KB Output is correct
29 Correct 84 ms 27900 KB Output is correct
30 Correct 77 ms 27768 KB Output is correct
31 Correct 73 ms 27848 KB Output is correct
32 Correct 115 ms 27428 KB Output is correct
33 Correct 103 ms 27640 KB Output is correct
34 Correct 76 ms 27652 KB Output is correct
35 Correct 113 ms 27512 KB Output is correct
36 Correct 62 ms 28152 KB Output is correct
37 Correct 117 ms 27512 KB Output is correct
38 Correct 97 ms 27512 KB Output is correct
39 Correct 96 ms 27512 KB Output is correct
40 Correct 101 ms 27512 KB Output is correct
41 Correct 85 ms 27768 KB Output is correct
42 Correct 92 ms 27768 KB Output is correct
43 Correct 64 ms 27896 KB Output is correct
44 Correct 107 ms 27512 KB Output is correct
45 Correct 101 ms 27520 KB Output is correct
46 Correct 85 ms 27384 KB Output is correct
47 Correct 135 ms 27384 KB Output is correct
48 Correct 129 ms 27384 KB Output is correct
49 Correct 132 ms 27412 KB Output is correct
50 Correct 141 ms 27392 KB Output is correct
51 Correct 140 ms 27384 KB Output is correct
52 Correct 161 ms 27504 KB Output is correct
53 Correct 110 ms 27512 KB Output is correct
54 Correct 118 ms 27468 KB Output is correct
55 Correct 127 ms 27384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 25848 KB Output is correct
2 Correct 23 ms 25856 KB Output is correct
3 Correct 25 ms 25856 KB Output is correct
4 Correct 151 ms 27100 KB Output is correct
5 Correct 182 ms 27132 KB Output is correct
6 Correct 151 ms 27384 KB Output is correct
7 Correct 110 ms 27672 KB Output is correct
8 Correct 108 ms 27692 KB Output is correct
9 Correct 107 ms 27640 KB Output is correct
10 Correct 132 ms 27384 KB Output is correct
11 Correct 120 ms 27460 KB Output is correct
12 Correct 124 ms 27456 KB Output is correct
13 Correct 114 ms 27640 KB Output is correct
14 Correct 131 ms 27640 KB Output is correct
15 Correct 109 ms 27512 KB Output is correct
16 Correct 115 ms 27684 KB Output is correct
17 Correct 113 ms 27640 KB Output is correct
18 Correct 136 ms 27384 KB Output is correct
19 Correct 152 ms 27336 KB Output is correct
20 Correct 128 ms 27544 KB Output is correct
21 Correct 131 ms 27624 KB Output is correct
22 Correct 114 ms 27768 KB Output is correct
23 Correct 97 ms 27640 KB Output is correct
24 Correct 81 ms 27784 KB Output is correct
25 Correct 71 ms 27896 KB Output is correct
26 Correct 86 ms 27640 KB Output is correct
27 Correct 83 ms 27384 KB Output is correct
28 Correct 82 ms 27768 KB Output is correct
29 Correct 84 ms 27900 KB Output is correct
30 Correct 77 ms 27768 KB Output is correct
31 Correct 73 ms 27848 KB Output is correct
32 Correct 115 ms 27428 KB Output is correct
33 Correct 103 ms 27640 KB Output is correct
34 Correct 76 ms 27652 KB Output is correct
35 Correct 113 ms 27512 KB Output is correct
36 Correct 62 ms 28152 KB Output is correct
37 Correct 117 ms 27512 KB Output is correct
38 Correct 97 ms 27512 KB Output is correct
39 Correct 96 ms 27512 KB Output is correct
40 Correct 101 ms 27512 KB Output is correct
41 Correct 85 ms 27768 KB Output is correct
42 Correct 92 ms 27768 KB Output is correct
43 Correct 64 ms 27896 KB Output is correct
44 Correct 107 ms 27512 KB Output is correct
45 Correct 101 ms 27520 KB Output is correct
46 Correct 85 ms 27384 KB Output is correct
47 Correct 135 ms 27384 KB Output is correct
48 Correct 129 ms 27384 KB Output is correct
49 Correct 132 ms 27412 KB Output is correct
50 Correct 141 ms 27392 KB Output is correct
51 Correct 140 ms 27384 KB Output is correct
52 Correct 161 ms 27504 KB Output is correct
53 Correct 110 ms 27512 KB Output is correct
54 Correct 118 ms 27468 KB Output is correct
55 Correct 127 ms 27384 KB Output is correct
56 Execution timed out 2040 ms 28784 KB Time limit exceeded
57 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2041 ms 56656 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2066 ms 61968 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 25848 KB Output is correct
2 Correct 23 ms 25856 KB Output is correct
3 Correct 25 ms 25856 KB Output is correct
4 Correct 151 ms 27100 KB Output is correct
5 Correct 182 ms 27132 KB Output is correct
6 Correct 151 ms 27384 KB Output is correct
7 Correct 110 ms 27672 KB Output is correct
8 Correct 108 ms 27692 KB Output is correct
9 Correct 107 ms 27640 KB Output is correct
10 Correct 132 ms 27384 KB Output is correct
11 Correct 120 ms 27460 KB Output is correct
12 Correct 124 ms 27456 KB Output is correct
13 Correct 114 ms 27640 KB Output is correct
14 Correct 131 ms 27640 KB Output is correct
15 Correct 109 ms 27512 KB Output is correct
16 Correct 115 ms 27684 KB Output is correct
17 Correct 113 ms 27640 KB Output is correct
18 Correct 136 ms 27384 KB Output is correct
19 Correct 152 ms 27336 KB Output is correct
20 Correct 128 ms 27544 KB Output is correct
21 Correct 131 ms 27624 KB Output is correct
22 Correct 114 ms 27768 KB Output is correct
23 Correct 97 ms 27640 KB Output is correct
24 Correct 81 ms 27784 KB Output is correct
25 Correct 71 ms 27896 KB Output is correct
26 Correct 86 ms 27640 KB Output is correct
27 Correct 83 ms 27384 KB Output is correct
28 Correct 82 ms 27768 KB Output is correct
29 Correct 84 ms 27900 KB Output is correct
30 Correct 77 ms 27768 KB Output is correct
31 Correct 73 ms 27848 KB Output is correct
32 Correct 115 ms 27428 KB Output is correct
33 Correct 103 ms 27640 KB Output is correct
34 Correct 76 ms 27652 KB Output is correct
35 Correct 113 ms 27512 KB Output is correct
36 Correct 62 ms 28152 KB Output is correct
37 Correct 117 ms 27512 KB Output is correct
38 Correct 97 ms 27512 KB Output is correct
39 Correct 96 ms 27512 KB Output is correct
40 Correct 101 ms 27512 KB Output is correct
41 Correct 85 ms 27768 KB Output is correct
42 Correct 92 ms 27768 KB Output is correct
43 Correct 64 ms 27896 KB Output is correct
44 Correct 107 ms 27512 KB Output is correct
45 Correct 101 ms 27520 KB Output is correct
46 Correct 85 ms 27384 KB Output is correct
47 Correct 135 ms 27384 KB Output is correct
48 Correct 129 ms 27384 KB Output is correct
49 Correct 132 ms 27412 KB Output is correct
50 Correct 141 ms 27392 KB Output is correct
51 Correct 140 ms 27384 KB Output is correct
52 Correct 161 ms 27504 KB Output is correct
53 Correct 110 ms 27512 KB Output is correct
54 Correct 118 ms 27468 KB Output is correct
55 Correct 127 ms 27384 KB Output is correct
56 Execution timed out 2040 ms 28784 KB Time limit exceeded
57 Halted 0 ms 0 KB -