Submission #115091

# Submission time Handle Problem Language Result Execution time Memory
115091 2019-06-05T08:32:46 Z 윤교준(#2865) Iqea (innopolis2018_final_C) C++14
36 / 100
2000 ms 205516 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 long long ll;
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 RMQ {
	pii d[18][MAXN*2];
	pii A[MAXN*2];
	int B[MAXN*2];
	int L;

	void cal() {
		for(int i = 0, s, e;; i++) {
			s = 1 << i; e = s << 1;
			if(L < s) break;
			for(int j = s; j < e; j++)
				B[j] = i;
		}

		for(int i = 1; i <= L; i++) d[0][i] = A[i];
		for(int j = 1; j < 18; j++) for(int i = 1, e; i <= L; i++) {
			e = i + (1 << (j-1));
			d[j][i] = e <= L ? min(d[j-1][i], d[j-1][e]) : d[j-1][i];
		}
	}

	pii get(int s, int e) {
		int k = B[e-s+1];
		return min(d[k][s], d[k][e-(1<<k)+1]);
	}
};

struct TRE {
	RMQ rmq;

	vector<int> G[MAXN];
	int dep[MAXN], AS[MAXN], AE[MAXN];
	int A[MAXN*2], An;

	void add(int a, int b) {
		G[a].eb(b);
		G[b].eb(a);
	}
	void dfs(int i) {
		A[++An] = i;
		AS[i] = An;
		for(int v : G[i]) if(!dep[v]) {
			dep[v] = dep[i] + 1;
			dfs(v);
			A[++An] = i;
		}
		AE[i] = An;
	}
	void cal() {
		for(int i = 1; i < MAXN; i++) if(!dep[i]) {
			dep[i] = 1;
			dfs(i);
		}

		rmq.L = An;
		for(int i = 1, j; i <= An; i++) {
			j = A[i];
			rmq.A[i] = pii(dep[j], j);
		}
		rmq.cal();
	}
	int lca(int a, int b) {
		pii r = rmq.get(min(AS[a], AS[b]), max(AE[a], AE[b]));
		return r.second;
	}
	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(d-s, idx));
		bitd.upd(ey-s, pii(d+s, 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];

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

int N, Q, K, Krt;

inline ll hsh(const pii &p) { return ll(p.first) << 30 | p.second; }
int getDist(pii a, pii b) {
	return treX.get(MPX[hsh(a)], MPX[hsh(b)]) + treY.get(MPY[hsh(a)], MPY[hsh(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[hsh(P[i])] = int(lower_bound(allv(V), MPX[hsh(P[i])]) - V.begin()) + 1;
	}

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

		pii p(P[i].first + 1, P[i].second);
		auto it = MPK.find(hsh(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(unordered_map<ll, int> &MP, TRE &tree, int dx) {
	DJF djf;
	for(int i = 1; i <= N; i++) MP[hsh(P[i])] = i;
	for(int i = 1; i <= N; i++) {
		pii p(P[i].first + dx, P[i].second + !dx);
		auto it = MP.find(hsh(p));
		if(MP.end() != it) djf.uf(i, it->second);
	}
	for(int i = 1; i <= N; i++) MP[hsh(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(hsh(p));
		if(MP.end() != it) tree.add(djf.uf(i), it->second);
	}
	tree.cal();
}

void upd(pii p, int idx) {
	for(int i = MPK[hsh(p)]; i;) {
		nod[i].addHubo(p, idx);
		i = prt[i];
	}
}
int get(pii p) {
	int r = INF;
	for(int i = MPK[hsh(p)], t; i;) {
		t = nod[i].get(p);
		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;
}
# Verdict Execution time Memory Grader output
1 Correct 67 ms 76280 KB Output is correct
2 Correct 69 ms 76152 KB Output is correct
3 Correct 71 ms 76280 KB Output is correct
4 Correct 117 ms 77176 KB Output is correct
5 Correct 115 ms 77180 KB Output is correct
6 Correct 113 ms 77176 KB Output is correct
7 Correct 98 ms 77560 KB Output is correct
8 Correct 100 ms 77632 KB Output is correct
9 Correct 105 ms 77688 KB Output is correct
10 Correct 108 ms 77304 KB Output is correct
11 Correct 111 ms 77380 KB Output is correct
12 Correct 110 ms 77588 KB Output is correct
13 Correct 103 ms 77488 KB Output is correct
14 Correct 107 ms 77560 KB Output is correct
15 Correct 107 ms 77468 KB Output is correct
16 Correct 113 ms 77568 KB Output is correct
17 Correct 112 ms 77644 KB Output is correct
18 Correct 112 ms 77392 KB Output is correct
19 Correct 117 ms 77304 KB Output is correct
20 Correct 105 ms 77564 KB Output is correct
21 Correct 106 ms 77648 KB Output is correct
22 Correct 97 ms 77688 KB Output is correct
23 Correct 96 ms 77656 KB Output is correct
24 Correct 94 ms 77688 KB Output is correct
25 Correct 89 ms 77944 KB Output is correct
26 Correct 106 ms 77688 KB Output is correct
27 Correct 100 ms 77428 KB Output is correct
28 Correct 93 ms 77740 KB Output is correct
29 Correct 98 ms 77816 KB Output is correct
30 Correct 95 ms 77816 KB Output is correct
31 Correct 101 ms 77896 KB Output is correct
32 Correct 103 ms 77512 KB Output is correct
33 Correct 106 ms 77488 KB Output is correct
34 Correct 93 ms 77800 KB Output is correct
35 Correct 103 ms 77436 KB Output is correct
36 Correct 88 ms 78072 KB Output is correct
37 Correct 108 ms 77528 KB Output is correct
38 Correct 102 ms 77432 KB Output is correct
39 Correct 103 ms 77660 KB Output is correct
40 Correct 105 ms 77688 KB Output is correct
41 Correct 97 ms 77664 KB Output is correct
42 Correct 99 ms 77584 KB Output is correct
43 Correct 86 ms 77816 KB Output is correct
44 Correct 103 ms 77512 KB Output is correct
45 Correct 100 ms 77432 KB Output is correct
46 Correct 88 ms 77436 KB Output is correct
47 Correct 108 ms 77304 KB Output is correct
48 Correct 106 ms 77304 KB Output is correct
49 Correct 113 ms 77280 KB Output is correct
50 Correct 110 ms 77304 KB Output is correct
51 Correct 120 ms 77312 KB Output is correct
52 Correct 115 ms 77304 KB Output is correct
53 Correct 103 ms 77452 KB Output is correct
54 Correct 109 ms 77312 KB Output is correct
55 Correct 108 ms 77404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 76280 KB Output is correct
2 Correct 69 ms 76152 KB Output is correct
3 Correct 71 ms 76280 KB Output is correct
4 Correct 117 ms 77176 KB Output is correct
5 Correct 115 ms 77180 KB Output is correct
6 Correct 113 ms 77176 KB Output is correct
7 Correct 98 ms 77560 KB Output is correct
8 Correct 100 ms 77632 KB Output is correct
9 Correct 105 ms 77688 KB Output is correct
10 Correct 108 ms 77304 KB Output is correct
11 Correct 111 ms 77380 KB Output is correct
12 Correct 110 ms 77588 KB Output is correct
13 Correct 103 ms 77488 KB Output is correct
14 Correct 107 ms 77560 KB Output is correct
15 Correct 107 ms 77468 KB Output is correct
16 Correct 113 ms 77568 KB Output is correct
17 Correct 112 ms 77644 KB Output is correct
18 Correct 112 ms 77392 KB Output is correct
19 Correct 117 ms 77304 KB Output is correct
20 Correct 105 ms 77564 KB Output is correct
21 Correct 106 ms 77648 KB Output is correct
22 Correct 97 ms 77688 KB Output is correct
23 Correct 96 ms 77656 KB Output is correct
24 Correct 94 ms 77688 KB Output is correct
25 Correct 89 ms 77944 KB Output is correct
26 Correct 106 ms 77688 KB Output is correct
27 Correct 100 ms 77428 KB Output is correct
28 Correct 93 ms 77740 KB Output is correct
29 Correct 98 ms 77816 KB Output is correct
30 Correct 95 ms 77816 KB Output is correct
31 Correct 101 ms 77896 KB Output is correct
32 Correct 103 ms 77512 KB Output is correct
33 Correct 106 ms 77488 KB Output is correct
34 Correct 93 ms 77800 KB Output is correct
35 Correct 103 ms 77436 KB Output is correct
36 Correct 88 ms 78072 KB Output is correct
37 Correct 108 ms 77528 KB Output is correct
38 Correct 102 ms 77432 KB Output is correct
39 Correct 103 ms 77660 KB Output is correct
40 Correct 105 ms 77688 KB Output is correct
41 Correct 97 ms 77664 KB Output is correct
42 Correct 99 ms 77584 KB Output is correct
43 Correct 86 ms 77816 KB Output is correct
44 Correct 103 ms 77512 KB Output is correct
45 Correct 100 ms 77432 KB Output is correct
46 Correct 88 ms 77436 KB Output is correct
47 Correct 108 ms 77304 KB Output is correct
48 Correct 106 ms 77304 KB Output is correct
49 Correct 113 ms 77280 KB Output is correct
50 Correct 110 ms 77304 KB Output is correct
51 Correct 120 ms 77312 KB Output is correct
52 Correct 115 ms 77304 KB Output is correct
53 Correct 103 ms 77452 KB Output is correct
54 Correct 109 ms 77312 KB Output is correct
55 Correct 108 ms 77404 KB Output is correct
56 Correct 1149 ms 78184 KB Output is correct
57 Correct 1215 ms 79276 KB Output is correct
58 Correct 979 ms 79096 KB Output is correct
59 Correct 665 ms 80120 KB Output is correct
60 Correct 720 ms 79588 KB Output is correct
61 Correct 706 ms 80340 KB Output is correct
62 Correct 776 ms 80036 KB Output is correct
63 Correct 845 ms 79916 KB Output is correct
64 Correct 844 ms 79920 KB Output is correct
65 Correct 819 ms 79580 KB Output is correct
66 Correct 674 ms 79608 KB Output is correct
67 Correct 747 ms 79596 KB Output is correct
68 Correct 649 ms 80128 KB Output is correct
69 Correct 691 ms 79524 KB Output is correct
70 Correct 803 ms 79980 KB Output is correct
71 Correct 821 ms 79808 KB Output is correct
72 Correct 717 ms 80120 KB Output is correct
73 Correct 742 ms 79620 KB Output is correct
74 Correct 342 ms 80452 KB Output is correct
75 Correct 447 ms 79700 KB Output is correct
76 Correct 412 ms 79864 KB Output is correct
77 Correct 478 ms 80164 KB Output is correct
78 Correct 443 ms 80268 KB Output is correct
79 Correct 259 ms 80732 KB Output is correct
80 Correct 635 ms 79992 KB Output is correct
81 Correct 680 ms 79992 KB Output is correct
82 Correct 533 ms 79916 KB Output is correct
83 Correct 550 ms 80140 KB Output is correct
84 Correct 317 ms 80384 KB Output is correct
85 Correct 684 ms 79676 KB Output is correct
86 Correct 757 ms 79728 KB Output is correct
87 Correct 416 ms 79416 KB Output is correct
88 Correct 796 ms 79848 KB Output is correct
89 Correct 878 ms 80084 KB Output is correct
90 Correct 934 ms 79904 KB Output is correct
91 Correct 855 ms 79304 KB Output is correct
92 Correct 914 ms 79120 KB Output is correct
93 Correct 960 ms 79288 KB Output is correct
94 Correct 651 ms 79864 KB Output is correct
95 Correct 769 ms 79832 KB Output is correct
96 Correct 805 ms 79848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1241 ms 192500 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2041 ms 205516 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 76280 KB Output is correct
2 Correct 69 ms 76152 KB Output is correct
3 Correct 71 ms 76280 KB Output is correct
4 Correct 117 ms 77176 KB Output is correct
5 Correct 115 ms 77180 KB Output is correct
6 Correct 113 ms 77176 KB Output is correct
7 Correct 98 ms 77560 KB Output is correct
8 Correct 100 ms 77632 KB Output is correct
9 Correct 105 ms 77688 KB Output is correct
10 Correct 108 ms 77304 KB Output is correct
11 Correct 111 ms 77380 KB Output is correct
12 Correct 110 ms 77588 KB Output is correct
13 Correct 103 ms 77488 KB Output is correct
14 Correct 107 ms 77560 KB Output is correct
15 Correct 107 ms 77468 KB Output is correct
16 Correct 113 ms 77568 KB Output is correct
17 Correct 112 ms 77644 KB Output is correct
18 Correct 112 ms 77392 KB Output is correct
19 Correct 117 ms 77304 KB Output is correct
20 Correct 105 ms 77564 KB Output is correct
21 Correct 106 ms 77648 KB Output is correct
22 Correct 97 ms 77688 KB Output is correct
23 Correct 96 ms 77656 KB Output is correct
24 Correct 94 ms 77688 KB Output is correct
25 Correct 89 ms 77944 KB Output is correct
26 Correct 106 ms 77688 KB Output is correct
27 Correct 100 ms 77428 KB Output is correct
28 Correct 93 ms 77740 KB Output is correct
29 Correct 98 ms 77816 KB Output is correct
30 Correct 95 ms 77816 KB Output is correct
31 Correct 101 ms 77896 KB Output is correct
32 Correct 103 ms 77512 KB Output is correct
33 Correct 106 ms 77488 KB Output is correct
34 Correct 93 ms 77800 KB Output is correct
35 Correct 103 ms 77436 KB Output is correct
36 Correct 88 ms 78072 KB Output is correct
37 Correct 108 ms 77528 KB Output is correct
38 Correct 102 ms 77432 KB Output is correct
39 Correct 103 ms 77660 KB Output is correct
40 Correct 105 ms 77688 KB Output is correct
41 Correct 97 ms 77664 KB Output is correct
42 Correct 99 ms 77584 KB Output is correct
43 Correct 86 ms 77816 KB Output is correct
44 Correct 103 ms 77512 KB Output is correct
45 Correct 100 ms 77432 KB Output is correct
46 Correct 88 ms 77436 KB Output is correct
47 Correct 108 ms 77304 KB Output is correct
48 Correct 106 ms 77304 KB Output is correct
49 Correct 113 ms 77280 KB Output is correct
50 Correct 110 ms 77304 KB Output is correct
51 Correct 120 ms 77312 KB Output is correct
52 Correct 115 ms 77304 KB Output is correct
53 Correct 103 ms 77452 KB Output is correct
54 Correct 109 ms 77312 KB Output is correct
55 Correct 108 ms 77404 KB Output is correct
56 Correct 1149 ms 78184 KB Output is correct
57 Correct 1215 ms 79276 KB Output is correct
58 Correct 979 ms 79096 KB Output is correct
59 Correct 665 ms 80120 KB Output is correct
60 Correct 720 ms 79588 KB Output is correct
61 Correct 706 ms 80340 KB Output is correct
62 Correct 776 ms 80036 KB Output is correct
63 Correct 845 ms 79916 KB Output is correct
64 Correct 844 ms 79920 KB Output is correct
65 Correct 819 ms 79580 KB Output is correct
66 Correct 674 ms 79608 KB Output is correct
67 Correct 747 ms 79596 KB Output is correct
68 Correct 649 ms 80128 KB Output is correct
69 Correct 691 ms 79524 KB Output is correct
70 Correct 803 ms 79980 KB Output is correct
71 Correct 821 ms 79808 KB Output is correct
72 Correct 717 ms 80120 KB Output is correct
73 Correct 742 ms 79620 KB Output is correct
74 Correct 342 ms 80452 KB Output is correct
75 Correct 447 ms 79700 KB Output is correct
76 Correct 412 ms 79864 KB Output is correct
77 Correct 478 ms 80164 KB Output is correct
78 Correct 443 ms 80268 KB Output is correct
79 Correct 259 ms 80732 KB Output is correct
80 Correct 635 ms 79992 KB Output is correct
81 Correct 680 ms 79992 KB Output is correct
82 Correct 533 ms 79916 KB Output is correct
83 Correct 550 ms 80140 KB Output is correct
84 Correct 317 ms 80384 KB Output is correct
85 Correct 684 ms 79676 KB Output is correct
86 Correct 757 ms 79728 KB Output is correct
87 Correct 416 ms 79416 KB Output is correct
88 Correct 796 ms 79848 KB Output is correct
89 Correct 878 ms 80084 KB Output is correct
90 Correct 934 ms 79904 KB Output is correct
91 Correct 855 ms 79304 KB Output is correct
92 Correct 914 ms 79120 KB Output is correct
93 Correct 960 ms 79288 KB Output is correct
94 Correct 651 ms 79864 KB Output is correct
95 Correct 769 ms 79832 KB Output is correct
96 Correct 805 ms 79848 KB Output is correct
97 Runtime error 1241 ms 192500 KB Execution killed with signal 11 (could be triggered by violating memory limits)
98 Halted 0 ms 0 KB -