# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
218056 |
2020-03-31T18:59:06 Z |
AQT |
Ideal city (IOI12_city) |
C++14 |
|
1000 ms |
20208 KB |
#include <bits/stdc++.h>
using namespace std;
int N, C;
vector<int> graph[100005];
int sz[100005];
set<pair<int, int>> st;
map<pair<int, int>, pair<int, int>> par;
vector<pair<int, int>> coords;
map<pair<int, int>, int> who;
pair<int, int> getrt(pair<int, int> n){
return par[n] = (par[n] == n ? n : getrt(par[n]));
}
void dfs(int n, int p){
for(int e : graph[n]){
if(e != p){
dfs(e, n);
sz[n] += sz[e];
}
}
}
void build(){
for(auto p : coords){
par[p] = p;
if(st.count({p.first-1, p.second})){
par[getrt({p.first-1, p.second})] = p;
}
if(st.count({p.first+1, p.second})){
par[getrt({p.first+1, p.second})] = p;
}
st.insert({p.first, p.second});
}
C = 0;
for(int i = 0; i<N; i++){
if(par[coords[i]] == coords[i]){
who[coords[i]] = ++C;
sz[C] = 0;
}
}
for(auto p : coords){
sz[who[getrt(p)]]++;
}
for(auto p : coords){
auto n = who[getrt(p)];
if(st.count({p.first, p.second-1})){
auto a = who[getrt({p.first, p.second-1})];
graph[a].push_back(n);
}
if(st.count({p.first, p.second+1})){
auto a = who[getrt({p.first, p.second+1})];
graph[a].push_back(n);
}
}
for(int i = 1; i<=C; i++){
sort(graph[i].begin(), graph[i].end());
graph[i].erase(unique(graph[i].begin(), graph[i].end()), graph[i].end());
}
}
long long calc(int *X, int *Y){
for(int i = 0; i<N; i++){
coords.push_back({X[i], Y[i]});
}
build();
dfs(1, 0);
long long ans = 0;
for(int i = 1; i<=C; i++){
ans += 1LL*sz[i]*(N-sz[i]);
graph[i].clear();
}
coords.clear();
par.clear();
st.clear();
who.clear();
return ans;
}
int DistanceSum(int NN, int *X, int *Y){
N = NN;
long long ret = calc(X, Y);
ret += calc(Y, X);
return ret%1000000000;
}
/*
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int NNN;
cin >> NNN;
int X[NNN], Y[NNN];
for(int i = 0; i<NNN; i++){
cin >> X[i] >> Y[i];
}
cout << DistanceSum(NNN, X, Y) << endl;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
7 ms |
2688 KB |
Output is correct |
7 |
Correct |
7 ms |
2688 KB |
Output is correct |
8 |
Correct |
7 ms |
2688 KB |
Output is correct |
9 |
Correct |
7 ms |
2688 KB |
Output is correct |
10 |
Correct |
7 ms |
2688 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2816 KB |
Output is correct |
2 |
Correct |
13 ms |
2816 KB |
Output is correct |
3 |
Correct |
15 ms |
2944 KB |
Output is correct |
4 |
Correct |
16 ms |
2944 KB |
Output is correct |
5 |
Correct |
19 ms |
3072 KB |
Output is correct |
6 |
Correct |
21 ms |
3072 KB |
Output is correct |
7 |
Correct |
19 ms |
3072 KB |
Output is correct |
8 |
Correct |
21 ms |
3072 KB |
Output is correct |
9 |
Correct |
21 ms |
2944 KB |
Output is correct |
10 |
Correct |
21 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
5580 KB |
Output is correct |
2 |
Correct |
240 ms |
6004 KB |
Output is correct |
3 |
Correct |
804 ms |
10228 KB |
Output is correct |
4 |
Correct |
739 ms |
10484 KB |
Output is correct |
5 |
Execution timed out |
1098 ms |
17520 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
196 ms |
6132 KB |
Output is correct |
2 |
Correct |
224 ms |
6136 KB |
Output is correct |
3 |
Correct |
671 ms |
11640 KB |
Output is correct |
4 |
Correct |
729 ms |
11236 KB |
Output is correct |
5 |
Execution timed out |
1092 ms |
20208 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |