# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
333083 |
2020-12-04T12:30:50 Z |
dolphingarlic |
Grad (COI14_grad) |
C++14 |
|
406 ms |
120888 KB |
#include <bits/stdc++.h>
using namespace std;
struct Point { int x, y; } p[101010];
struct SP;
struct Node { // Node in tree decomposition
int idx[2], depth;
double dist;
Node(int _u, int _v) : dist(hypot(p[_u].x - p[_v].x, p[_u].y - p[_v].y)) {
idx[0] = _u;
idx[1] = _v;
}
Node *complement;
vector<Node *> anc;
vector<SP> anc_sp;
} *has_city[101010];
struct SP { // Shortest path between two nodes in tree decomposition
int idx[2][2];
double dist[2][2];
SP(Node *u) {
idx[0][0] = idx[1][0] = u->idx[0];
idx[0][1] = idx[1][1] = u->idx[1];
dist[0][0] = dist[1][1] = 0;
dist[0][1] = dist[1][0] = u->dist;
}
SP(Node *u, Node *v) {
assert(u->anc[0] == v);
idx[0][0] = u->idx[0], idx[0][1] = u->idx[1];
idx[1][0] = v->idx[0], idx[1][1] = v->idx[1];
if (u->idx[0] == v->idx[0]) {
dist[0][0] = 0;
dist[0][1] = v->dist;
dist[1][0] = u->dist;
dist[1][1] = u->complement->dist;
} else {
dist[0][0] = v->dist;
dist[0][1] = 0;
dist[1][0] = u->complement->dist;
dist[1][1] = u->dist;
}
}
SP(SP a, SP b) {
assert(a.idx[1][0] == b.idx[0][0] && a.idx[1][1] == b.idx[0][1]);
idx[0][0] = a.idx[0][0], idx[0][1] = a.idx[0][1];
idx[1][0] = b.idx[1][0], idx[1][1] = b.idx[1][1];
for (int i : {0, 1}) for (int j : {0, 1})
dist[i][j] = min(a.dist[i][0] + b.dist[0][j], a.dist[i][1] + b.dist[1][j]);
}
};
map<pair<int, int>, Node*> mp;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> p[1].x >> p[1].y >> p[2].x >> p[2].y >> n;
has_city[1] = has_city[2] = mp[{1, 2}] = new Node(1, 2);
for (int cnt = 2; n; n--) {
char c;
cin >> c;
if (c == 'd') {
cnt++;
int a, b;
cin >> p[cnt].x >> p[cnt].y >> a >> b;
if (a > b) swap(a, b);
Node *par = mp[{a, b}];
Node *u = new Node(a, cnt), *v = new Node(b, cnt);
u->complement = v, v->complement = u;
u->depth = v->depth = par->depth + 1;
has_city[cnt] = u;
u->anc.push_back(par);
u->anc_sp.push_back(SP(u, par));
for (int i = 0; i < u->anc.size() && i < u->anc[i]->anc.size(); i++) {
u->anc.push_back(u->anc[i]->anc[i]);
u->anc_sp.push_back(SP(u->anc_sp[i], u->anc[i]->anc_sp[i]));
}
mp[{a, cnt}] = u;
v->anc.push_back(par);
v->anc_sp.push_back(SP(v, par));
for (int i = 0; i < v->anc.size() && i < v->anc[i]->anc.size(); i++) {
v->anc.push_back(v->anc[i]->anc[i]);
v->anc_sp.push_back(SP(v->anc_sp[i], v->anc[i]->anc_sp[i]));
}
mp[{b, cnt}] = v;
has_city[cnt] = u;
} else {
int a, b;
cin >> a >> b;
if (has_city[a]->depth < has_city[b]->depth) swap(a, b);
double ans = 1e18;
Node *anode = has_city[a], *bnode = has_city[b];
SP asp = SP(anode), bsp = SP(bnode);
for (int k = 0, diff = anode->depth - bnode->depth; diff; k++, diff >>= 1) {
if (diff & 1) {
asp = SP(asp, anode->anc_sp[k]);
anode = anode->anc[k];
}
}
for (int k = anode->anc.size() - 1; ~k; k--) {
if (k < anode->anc.size() && anode->anc[k] != bnode->anc[k]) {
asp = SP(asp, anode->anc_sp[k]);
anode = anode->anc[k];
bsp = SP(bsp, bnode->anc_sp[k]);
bnode = bnode->anc[k];
}
}
for (int k : {0, 1}) for (int l : {0, 1}) if (asp.idx[0][k] == a && bsp.idx[0][l] == b)
for (int m : {0, 1}) for (int o : {0, 1}) if (asp.idx[1][m] == bsp.idx[1][o])
ans = min(ans, asp.dist[k][m] + bsp.dist[l][o]);
if (anode != bnode) {
asp = SP(asp, anode->anc_sp[0]), bsp = SP(bsp, bnode->anc_sp[0]);
for (int k : {0, 1}) for (int l : {0, 1}) if (asp.idx[0][k] == a && bsp.idx[0][l] == b)
for (int m : {0, 1}) for (int o : {0, 1}) if (asp.idx[1][m] == bsp.idx[1][o])
ans = min(ans, asp.dist[k][m] + bsp.dist[l][o]);
}
cout << fixed << setprecision(6) << ans << '\n';
}
}
return 0;
}
Compilation message
grad.cpp: In function 'int main()':
grad.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < u->anc.size() && i < u->anc[i]->anc.size(); i++) {
| ~~^~~~~~~~~~~~~~~
grad.cpp:84:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < u->anc.size() && i < u->anc[i]->anc.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
grad.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < v->anc.size() && i < v->anc[i]->anc.size(); i++) {
| ~~^~~~~~~~~~~~~~~
grad.cpp:92:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < v->anc.size() && i < v->anc[i]->anc.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
grad.cpp:114:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | if (k < anode->anc.size() && anode->anc[k] != bnode->anc[k]) {
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
41 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
300 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
500 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
75372 KB |
15000 numbers |
2 |
Correct |
138 ms |
44012 KB |
15000 numbers |
3 |
Correct |
191 ms |
64876 KB |
30000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
111360 KB |
28333 numbers |
2 |
Correct |
260 ms |
96236 KB |
28333 numbers |
3 |
Correct |
280 ms |
86620 KB |
40000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
319 ms |
108140 KB |
50000 numbers |
2 |
Correct |
294 ms |
56300 KB |
50000 numbers |
3 |
Correct |
326 ms |
108140 KB |
50000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
97692 KB |
55000 numbers |
2 |
Correct |
277 ms |
85484 KB |
55000 numbers |
3 |
Correct |
344 ms |
108160 KB |
50000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
357 ms |
108012 KB |
50000 numbers |
2 |
Correct |
331 ms |
107432 KB |
50000 numbers |
3 |
Correct |
309 ms |
44012 KB |
50000 numbers |
4 |
Correct |
367 ms |
108140 KB |
50000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
120812 KB |
44000 numbers |
2 |
Correct |
345 ms |
118168 KB |
44000 numbers |
3 |
Correct |
312 ms |
67692 KB |
44000 numbers |
4 |
Correct |
391 ms |
120888 KB |
44000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
108140 KB |
50000 numbers |
2 |
Correct |
365 ms |
108140 KB |
50000 numbers |
3 |
Correct |
310 ms |
49004 KB |
50000 numbers |
4 |
Correct |
388 ms |
108268 KB |
50000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
380 ms |
117228 KB |
45713 numbers |
2 |
Correct |
406 ms |
98796 KB |
54285 numbers |
3 |
Correct |
285 ms |
44652 KB |
58571 numbers |
4 |
Correct |
362 ms |
93420 KB |
57000 numbers |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
361 ms |
109548 KB |
49285 numbers |
2 |
Correct |
367 ms |
109676 KB |
49285 numbers |
3 |
Correct |
314 ms |
48748 KB |
49285 numbers |
4 |
Correct |
389 ms |
119020 KB |
45000 numbers |