답안 #283620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283620 2020-08-26T03:26:29 Z arnold518 Iqea (innopolis2018_final_C) C++14
36 / 100
1704 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const int INF = 1e9;

int N;
pii A[MAXN+10];
vector<int> X[MAXN+10], Y[MAXN+10];

struct Line
{
	int l, r, p;
	Line(int l, int r, int p) : l(l), r(r), p(p) {}
	bool operator < (const Line &q) const { return r<q.r; }
};
int Q;
vector<Line> LX[MAXN+10], LY[MAXN+10];
vector<int> adjX[MAXN+10], adjY[MAXN+10];

pii get(int y, int x)
{
	int nx=lower_bound(LX[y].begin(), LX[y].end(), Line(0, x, 0))->p;
	int ny=lower_bound(LY[x].begin(), LY[x].end(), Line(0, y, 0))->p;
	return {nx, ny};
}

pii P[MAXN+10];

struct Tree
{
	int M;
	vector<int> adj[MAXN+10];
	int sz[MAXN+10], del[MAXN+10], cpar[MAXN+10], cdist[MAXN+10][30], cdep[MAXN+10];

	Tree() : M(0) {}

	void getsz(int now, int bef)
	{
		sz[now]=1;
		for(int nxt : adj[now])
		{
			if(nxt==bef) continue;
			if(del[nxt]) continue;
			getsz(nxt, now);
			sz[now]+=sz[nxt];
		}
	}

	int getcen(int now, int bef, int S)
	{
		for(int nxt : adj[now])
		{
			if(nxt==bef) continue;
			if(del[nxt]) continue;
			if(sz[nxt]>S/2) return getcen(nxt, now, S);
		}
		return now;
	}

	void dfs(int now, int bef, int t, int dist)
	{
		cdist[now][t]=dist;
		for(int nxt : adj[now])
		{
			if(nxt==bef) continue;
			if(del[nxt]) continue;
			dfs(nxt, now, t, dist+1);
		}
	}

	void decomp(int now, int bef, int cendep)
	{
		getsz(now, now);
		now=getcen(now, now, sz[now]/2);
		cpar[now]=bef;
		cdep[now]=cendep;

		dfs(now, now, cendep, 0);
		del[now]=true;

		for(auto nxt : adj[now])
		{
			if(del[nxt]) continue;
			decomp(nxt, now, cendep+1);
		}
	}

	void init()
	{
		decomp(1, 0, 1);
	}

	vector<int> getpar(int now)
	{
		vector<int> ans;
		while(now)
		{
			ans.push_back(now);
			now=cpar[now];
		}
		return ans;
	}
}TX, TY;

vector<pii> TP1;
vector<int> TP2;

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++)
	{
		int y, x;
		scanf("%d%d", &y, &x);
		A[i]={y, x};
		X[y].push_back(x);
		Y[x].push_back(y);
	}

	for(int i=1; i<=MAXN; i++)
	{
		if(X[i].empty()) continue;
		sort(X[i].begin(), X[i].end());

		int bef=X[i][0];
		for(int j=0; j+1<X[i].size(); j++)
		{
			if(X[i][j]+1!=X[i][j+1])
			{
				LX[i].push_back({bef, X[i][j], ++TX.M});
				//printf("LX %d %d %d %d\n", i, LX[i].back().l, LX[i].back().r, LX[i].back().p);
				bef=X[i][j+1];
			}
		}
		LX[i].push_back({bef, X[i].back(), ++TX.M});
		//printf("LX %d %d %d %d\n", i, LX[i].back().l, LX[i].back().r, LX[i].back().p);
	}

	for(int i=2; i<=MAXN; i++)
	{
		int p=0;
		for(auto it : LX[i])
		{
			for(; p<LX[i-1].size() && LX[i-1][p].r<it.l; p++);
			for(; p<LX[i-1].size() && LX[i-1][p].l<=it.r; p++)
			{
				int u=LX[i-1][p].p, v=it.p;
				TX.adj[u].push_back(v);
				TX.adj[v].push_back(u);
				//printf("adjX %d %d\n", u, v);
			}
			if(p) p--;
		}
	}

	for(int i=1; i<=MAXN; i++)
	{
		if(Y[i].empty()) continue;
		sort(Y[i].begin(), Y[i].end());

		int bef=Y[i][0];
		for(int j=0; j+1<Y[i].size(); j++)
		{
			if(Y[i][j]+1!=Y[i][j+1])
			{
				LY[i].push_back({bef, Y[i][j], ++TY.M});
				//printf("LY %d %d %d %d\n", i, LY[i].back().l, LY[i].back().r, LY[i].back().p);
				bef=Y[i][j+1];
			}
		}
		LY[i].push_back({bef, Y[i].back(), ++TY.M});
		//printf("LY %d %d %d %d\n", i, LY[i].back().l, LY[i].back().r, LY[i].back().p);
	}

	for(int i=2; i<=MAXN; i++)
	{
		int p=0;
		for(auto it : LY[i])
		{
			for(; p<LY[i-1].size() && LY[i-1][p].r<it.l; p++);
			for(; p<LY[i-1].size() && LY[i-1][p].l<=it.r; p++)
			{
				int u=LY[i-1][p].p, v=it.p;
				TY.adj[u].push_back(v);
				TY.adj[v].push_back(u);
				//printf("adjY %d %d\n", u, v);
			}
			if(p) p--;
		}
	}

	TX.init(); TY.init();

	for(int i=1; i<=N; i++)
	{
		P[i]=get(A[i].first, A[i].second);
		vector<int> V1=TX.getpar(P[i].first);
		vector<int> V2=TY.getpar(P[i].second);
		for(auto it : V1) for(auto jt : V2) TP1.push_back({it, jt});
	}
	sort(TP1.begin(), TP1.end());
	TP1.erase(unique(TP1.begin(), TP1.end()), TP1.end());
	TP2=vector<int>(TP1.size(), INF);

	scanf("%d", &Q);
	while(Q--)
	{
		int tt, y, x;
		scanf("%d%d%d", &tt, &y, &x);

		pii t=get(y, x);

		vector<int> V1=TX.getpar(t.first);
		vector<int> V2=TY.getpar(t.second);

		if(tt==1)
		{
			for(auto it : V1) for(auto jt : V2)
			{
				int pt=lower_bound(TP1.begin(), TP1.end(), pii(it, jt))-TP1.begin();
				TP2[pt]=min(TP2[pt], TX.cdist[t.first][TX.cdep[it]]+TY.cdist[t.second][TY.cdep[jt]]);
			}
		}
		else
		{
			int ans=INF;
			for(auto it : V1) for(auto jt : V2)
			{
				int pt=lower_bound(TP1.begin(), TP1.end(), pii(it, jt))-TP1.begin();
				ans=min(ans, TX.cdist[t.first][TX.cdep[it]]+TY.cdist[t.second][TY.cdep[jt]]+TP2[pt]);
			}
			if(ans==INF) ans=-1;
			printf("%d\n", ans);
		}
	}
}

Compilation message

C.cpp: In function 'int main()':
C.cpp:131:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |   for(int j=0; j+1<X[i].size(); j++)
      |                ~~~^~~~~~~~~~~~
C.cpp:149:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |    for(; p<LX[i-1].size() && LX[i-1][p].r<it.l; p++);
      |          ~^~~~~~~~~~~~~~~
C.cpp:150:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |    for(; p<LX[i-1].size() && LX[i-1][p].l<=it.r; p++)
      |          ~^~~~~~~~~~~~~~~
C.cpp:167:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |   for(int j=0; j+1<Y[i].size(); j++)
      |                ~~~^~~~~~~~~~~~
C.cpp:185:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |    for(; p<LY[i-1].size() && LY[i-1][p].r<it.l; p++);
      |          ~^~~~~~~~~~~~~~~
C.cpp:186:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  186 |    for(; p<LY[i-1].size() && LY[i-1][p].l<=it.r; p++)
      |          ~^~~~~~~~~~~~~~~
C.cpp:115:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  115 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
C.cpp:119:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  119 |   scanf("%d%d", &y, &x);
      |   ~~~~~^~~~~~~~~~~~~~~~
C.cpp:210:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  210 |  scanf("%d", &Q);
      |  ~~~~~^~~~~~~~~~
C.cpp:214:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  214 |   scanf("%d%d%d", &tt, &y, &x);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19200 KB Output is correct
2 Correct 14 ms 19200 KB Output is correct
3 Correct 14 ms 19328 KB Output is correct
4 Correct 72 ms 23788 KB Output is correct
5 Correct 66 ms 23668 KB Output is correct
6 Correct 76 ms 23796 KB Output is correct
7 Correct 109 ms 24560 KB Output is correct
8 Correct 112 ms 24552 KB Output is correct
9 Correct 114 ms 24556 KB Output is correct
10 Correct 111 ms 24432 KB Output is correct
11 Correct 111 ms 24560 KB Output is correct
12 Correct 124 ms 24560 KB Output is correct
13 Correct 114 ms 24552 KB Output is correct
14 Correct 113 ms 24560 KB Output is correct
15 Correct 104 ms 24560 KB Output is correct
16 Correct 122 ms 28652 KB Output is correct
17 Correct 113 ms 24424 KB Output is correct
18 Correct 132 ms 24428 KB Output is correct
19 Correct 96 ms 24432 KB Output is correct
20 Correct 110 ms 24560 KB Output is correct
21 Correct 115 ms 24528 KB Output is correct
22 Correct 138 ms 28776 KB Output is correct
23 Correct 139 ms 28776 KB Output is correct
24 Correct 150 ms 29036 KB Output is correct
25 Correct 39 ms 22008 KB Output is correct
26 Correct 49 ms 22648 KB Output is correct
27 Correct 33 ms 22012 KB Output is correct
28 Correct 146 ms 29036 KB Output is correct
29 Correct 160 ms 29028 KB Output is correct
30 Correct 157 ms 29168 KB Output is correct
31 Correct 174 ms 29040 KB Output is correct
32 Correct 174 ms 29036 KB Output is correct
33 Correct 151 ms 28904 KB Output is correct
34 Correct 155 ms 28908 KB Output is correct
35 Correct 159 ms 29028 KB Output is correct
36 Correct 34 ms 22012 KB Output is correct
37 Correct 159 ms 29032 KB Output is correct
38 Correct 171 ms 29036 KB Output is correct
39 Correct 163 ms 29032 KB Output is correct
40 Correct 160 ms 29136 KB Output is correct
41 Correct 145 ms 28900 KB Output is correct
42 Correct 149 ms 28904 KB Output is correct
43 Correct 38 ms 22008 KB Output is correct
44 Correct 149 ms 28964 KB Output is correct
45 Correct 147 ms 28900 KB Output is correct
46 Correct 36 ms 21872 KB Output is correct
47 Correct 97 ms 24300 KB Output is correct
48 Correct 100 ms 24152 KB Output is correct
49 Correct 89 ms 24048 KB Output is correct
50 Correct 82 ms 23920 KB Output is correct
51 Correct 73 ms 23796 KB Output is correct
52 Correct 101 ms 24308 KB Output is correct
53 Correct 105 ms 24428 KB Output is correct
54 Correct 115 ms 28392 KB Output is correct
55 Correct 126 ms 28392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19200 KB Output is correct
2 Correct 14 ms 19200 KB Output is correct
3 Correct 14 ms 19328 KB Output is correct
4 Correct 72 ms 23788 KB Output is correct
5 Correct 66 ms 23668 KB Output is correct
6 Correct 76 ms 23796 KB Output is correct
7 Correct 109 ms 24560 KB Output is correct
8 Correct 112 ms 24552 KB Output is correct
9 Correct 114 ms 24556 KB Output is correct
10 Correct 111 ms 24432 KB Output is correct
11 Correct 111 ms 24560 KB Output is correct
12 Correct 124 ms 24560 KB Output is correct
13 Correct 114 ms 24552 KB Output is correct
14 Correct 113 ms 24560 KB Output is correct
15 Correct 104 ms 24560 KB Output is correct
16 Correct 122 ms 28652 KB Output is correct
17 Correct 113 ms 24424 KB Output is correct
18 Correct 132 ms 24428 KB Output is correct
19 Correct 96 ms 24432 KB Output is correct
20 Correct 110 ms 24560 KB Output is correct
21 Correct 115 ms 24528 KB Output is correct
22 Correct 138 ms 28776 KB Output is correct
23 Correct 139 ms 28776 KB Output is correct
24 Correct 150 ms 29036 KB Output is correct
25 Correct 39 ms 22008 KB Output is correct
26 Correct 49 ms 22648 KB Output is correct
27 Correct 33 ms 22012 KB Output is correct
28 Correct 146 ms 29036 KB Output is correct
29 Correct 160 ms 29028 KB Output is correct
30 Correct 157 ms 29168 KB Output is correct
31 Correct 174 ms 29040 KB Output is correct
32 Correct 174 ms 29036 KB Output is correct
33 Correct 151 ms 28904 KB Output is correct
34 Correct 155 ms 28908 KB Output is correct
35 Correct 159 ms 29028 KB Output is correct
36 Correct 34 ms 22012 KB Output is correct
37 Correct 159 ms 29032 KB Output is correct
38 Correct 171 ms 29036 KB Output is correct
39 Correct 163 ms 29032 KB Output is correct
40 Correct 160 ms 29136 KB Output is correct
41 Correct 145 ms 28900 KB Output is correct
42 Correct 149 ms 28904 KB Output is correct
43 Correct 38 ms 22008 KB Output is correct
44 Correct 149 ms 28964 KB Output is correct
45 Correct 147 ms 28900 KB Output is correct
46 Correct 36 ms 21872 KB Output is correct
47 Correct 97 ms 24300 KB Output is correct
48 Correct 100 ms 24152 KB Output is correct
49 Correct 89 ms 24048 KB Output is correct
50 Correct 82 ms 23920 KB Output is correct
51 Correct 73 ms 23796 KB Output is correct
52 Correct 101 ms 24308 KB Output is correct
53 Correct 105 ms 24428 KB Output is correct
54 Correct 115 ms 28392 KB Output is correct
55 Correct 126 ms 28392 KB Output is correct
56 Correct 701 ms 23796 KB Output is correct
57 Correct 666 ms 23796 KB Output is correct
58 Correct 655 ms 23796 KB Output is correct
59 Correct 1128 ms 24560 KB Output is correct
60 Correct 1133 ms 24676 KB Output is correct
61 Correct 1141 ms 24668 KB Output is correct
62 Correct 1127 ms 24512 KB Output is correct
63 Correct 1079 ms 24560 KB Output is correct
64 Correct 1224 ms 24880 KB Output is correct
65 Correct 1110 ms 24668 KB Output is correct
66 Correct 1198 ms 24836 KB Output is correct
67 Correct 1074 ms 24572 KB Output is correct
68 Correct 1154 ms 24624 KB Output is correct
69 Correct 1115 ms 24600 KB Output is correct
70 Correct 1208 ms 24616 KB Output is correct
71 Correct 1148 ms 24428 KB Output is correct
72 Correct 1213 ms 24924 KB Output is correct
73 Correct 1254 ms 24796 KB Output is correct
74 Correct 272 ms 22008 KB Output is correct
75 Correct 366 ms 22648 KB Output is correct
76 Correct 209 ms 22140 KB Output is correct
77 Correct 1609 ms 29028 KB Output is correct
78 Correct 1655 ms 29028 KB Output is correct
79 Correct 203 ms 22140 KB Output is correct
80 Correct 1678 ms 29032 KB Output is correct
81 Correct 1704 ms 29028 KB Output is correct
82 Correct 1557 ms 28908 KB Output is correct
83 Correct 1609 ms 28904 KB Output is correct
84 Correct 256 ms 21872 KB Output is correct
85 Correct 1504 ms 28908 KB Output is correct
86 Correct 1526 ms 28904 KB Output is correct
87 Correct 241 ms 21872 KB Output is correct
88 Correct 1054 ms 24436 KB Output is correct
89 Correct 1097 ms 24052 KB Output is correct
90 Correct 867 ms 24048 KB Output is correct
91 Correct 824 ms 23920 KB Output is correct
92 Correct 692 ms 23796 KB Output is correct
93 Correct 1084 ms 24452 KB Output is correct
94 Correct 1002 ms 24484 KB Output is correct
95 Correct 1218 ms 28392 KB Output is correct
96 Correct 1204 ms 28400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 659 ms 67952 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 507 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19200 KB Output is correct
2 Correct 14 ms 19200 KB Output is correct
3 Correct 14 ms 19328 KB Output is correct
4 Correct 72 ms 23788 KB Output is correct
5 Correct 66 ms 23668 KB Output is correct
6 Correct 76 ms 23796 KB Output is correct
7 Correct 109 ms 24560 KB Output is correct
8 Correct 112 ms 24552 KB Output is correct
9 Correct 114 ms 24556 KB Output is correct
10 Correct 111 ms 24432 KB Output is correct
11 Correct 111 ms 24560 KB Output is correct
12 Correct 124 ms 24560 KB Output is correct
13 Correct 114 ms 24552 KB Output is correct
14 Correct 113 ms 24560 KB Output is correct
15 Correct 104 ms 24560 KB Output is correct
16 Correct 122 ms 28652 KB Output is correct
17 Correct 113 ms 24424 KB Output is correct
18 Correct 132 ms 24428 KB Output is correct
19 Correct 96 ms 24432 KB Output is correct
20 Correct 110 ms 24560 KB Output is correct
21 Correct 115 ms 24528 KB Output is correct
22 Correct 138 ms 28776 KB Output is correct
23 Correct 139 ms 28776 KB Output is correct
24 Correct 150 ms 29036 KB Output is correct
25 Correct 39 ms 22008 KB Output is correct
26 Correct 49 ms 22648 KB Output is correct
27 Correct 33 ms 22012 KB Output is correct
28 Correct 146 ms 29036 KB Output is correct
29 Correct 160 ms 29028 KB Output is correct
30 Correct 157 ms 29168 KB Output is correct
31 Correct 174 ms 29040 KB Output is correct
32 Correct 174 ms 29036 KB Output is correct
33 Correct 151 ms 28904 KB Output is correct
34 Correct 155 ms 28908 KB Output is correct
35 Correct 159 ms 29028 KB Output is correct
36 Correct 34 ms 22012 KB Output is correct
37 Correct 159 ms 29032 KB Output is correct
38 Correct 171 ms 29036 KB Output is correct
39 Correct 163 ms 29032 KB Output is correct
40 Correct 160 ms 29136 KB Output is correct
41 Correct 145 ms 28900 KB Output is correct
42 Correct 149 ms 28904 KB Output is correct
43 Correct 38 ms 22008 KB Output is correct
44 Correct 149 ms 28964 KB Output is correct
45 Correct 147 ms 28900 KB Output is correct
46 Correct 36 ms 21872 KB Output is correct
47 Correct 97 ms 24300 KB Output is correct
48 Correct 100 ms 24152 KB Output is correct
49 Correct 89 ms 24048 KB Output is correct
50 Correct 82 ms 23920 KB Output is correct
51 Correct 73 ms 23796 KB Output is correct
52 Correct 101 ms 24308 KB Output is correct
53 Correct 105 ms 24428 KB Output is correct
54 Correct 115 ms 28392 KB Output is correct
55 Correct 126 ms 28392 KB Output is correct
56 Correct 701 ms 23796 KB Output is correct
57 Correct 666 ms 23796 KB Output is correct
58 Correct 655 ms 23796 KB Output is correct
59 Correct 1128 ms 24560 KB Output is correct
60 Correct 1133 ms 24676 KB Output is correct
61 Correct 1141 ms 24668 KB Output is correct
62 Correct 1127 ms 24512 KB Output is correct
63 Correct 1079 ms 24560 KB Output is correct
64 Correct 1224 ms 24880 KB Output is correct
65 Correct 1110 ms 24668 KB Output is correct
66 Correct 1198 ms 24836 KB Output is correct
67 Correct 1074 ms 24572 KB Output is correct
68 Correct 1154 ms 24624 KB Output is correct
69 Correct 1115 ms 24600 KB Output is correct
70 Correct 1208 ms 24616 KB Output is correct
71 Correct 1148 ms 24428 KB Output is correct
72 Correct 1213 ms 24924 KB Output is correct
73 Correct 1254 ms 24796 KB Output is correct
74 Correct 272 ms 22008 KB Output is correct
75 Correct 366 ms 22648 KB Output is correct
76 Correct 209 ms 22140 KB Output is correct
77 Correct 1609 ms 29028 KB Output is correct
78 Correct 1655 ms 29028 KB Output is correct
79 Correct 203 ms 22140 KB Output is correct
80 Correct 1678 ms 29032 KB Output is correct
81 Correct 1704 ms 29028 KB Output is correct
82 Correct 1557 ms 28908 KB Output is correct
83 Correct 1609 ms 28904 KB Output is correct
84 Correct 256 ms 21872 KB Output is correct
85 Correct 1504 ms 28908 KB Output is correct
86 Correct 1526 ms 28904 KB Output is correct
87 Correct 241 ms 21872 KB Output is correct
88 Correct 1054 ms 24436 KB Output is correct
89 Correct 1097 ms 24052 KB Output is correct
90 Correct 867 ms 24048 KB Output is correct
91 Correct 824 ms 23920 KB Output is correct
92 Correct 692 ms 23796 KB Output is correct
93 Correct 1084 ms 24452 KB Output is correct
94 Correct 1002 ms 24484 KB Output is correct
95 Correct 1218 ms 28392 KB Output is correct
96 Correct 1204 ms 28400 KB Output is correct
97 Incorrect 659 ms 67952 KB Output isn't correct
98 Halted 0 ms 0 KB -