Submission #411788

# Submission time Handle Problem Language Result Execution time Memory
411788 2021-05-26T01:20:05 Z couplefire ČVENK (COI15_cvenk) C++17
60 / 100
3000 ms 319836 KB
#include <bits/stdc++.h>
using namespace std;
#define point pair<int, int>
#define x first
#define y second

const int N = 100005;

int lowbit(int x){return x&(-x);}

point fa(point v){
    if(v.y == 0 || (v.x != 0 && lowbit(v.x) < lowbit(v.y))) return {v.x^lowbit(v.x), v.y};
    return {v.x, v.y^lowbit(v.y)};
}

bool isPar(point u, point v){
    if(u.x > v.x || u.y > v.y) return 0;
    int pos = 30;
    while(pos >= 0 && ((u.x>>pos)&1) == ((v.x>>pos)&1) && ((u.y>>pos)&1) == ((v.y>>pos)&1)) pos--;
    if(pos<0) return 1;
    if((v.x>>pos)&1) return !(u.y&((1<<pos)-1));
    return !(u.x&((1<<pos)-1));
}

int n;
map<int, vector<int>> hori, vert;
point points[N], lol[30*N];
map<point, int> mp;
vector<int> adj[30*N];
int siz[30*N], curid;

int dist(point a, point b){
    return abs(a.x-b.x)+abs(a.y-b.y);
}

int add(point a){
    if(mp.count(a)) return mp[a];
    lol[curid] = a;
    return mp[a] = curid++;
}

void ae(int u, int v){
    adj[u].push_back(v);
    adj[v].push_back(u);
}

void dfs(int v, int p){
    for(auto u : adj[v]){
        if(u == p) continue;
        dfs(u, v);
        siz[v] += siz[u];
    }
}

long long dfs2(int v, int p){
    long long ans = 0;
    for(auto u : adj[v]){
        if(u == p) continue;
        ans += dfs2(u, v)+1ll*siz[u]*dist(lol[u], lol[v]);
    }
    return ans;
}

int centroid(int v){
    dfs(v, -1);
    int num = n, p = -1;
    do{
        int nxt = -1;
        for(auto u : adj[v])
            if(u != p && siz[u]*2 > num)
                nxt = u;
        p = v, v = nxt;
    } while(~v);
    return p;
}

int main(){
    // freopen("a.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 0; i<n; i++){
        int a, b; cin >> a >> b;
        points[i] = {a, b};
    }
    hori[0].push_back(0);
    vert[0].push_back(0);
    for(int i = 0; i<n; i++){
        point v = points[i];
        while(v != point{0, 0}){
            hori[v.y].push_back(v.x);
            vert[v.x].push_back(v.y);
            v = fa(v);
        }
    }
    int _c = 0;
    for(auto &l : hori){
        sort(l.second.begin(), l.second.end());
        l.second.erase(unique(l.second.begin(), l.second.end()), l.second.end());
        for(int i = 1; i<(int)l.second.size(); i++){
            point u = point{l.second[i-1], l.first}, v = point{l.second[i], l.first};
            if(!isPar(u, v)) continue;
            ae(add(u), add(v));
        }
    }
    for(auto &l : vert){
        sort(l.second.begin(), l.second.end());
        l.second.erase(unique(l.second.begin(), l.second.end()), l.second.end());
        for(int i = 1; i<(int)l.second.size(); i++){
            point u = point{l.first, l.second[i-1]}, v = point{l.first, l.second[i]};
            if(!isPar(u, v)) continue;
            ae(add(u), add(v));
        }
    }
    for(int i = 0; i<n; i++)
        siz[add(points[i])]++;
    int cent = centroid(0);
    memset(siz, 0, sizeof siz);
    for(int i = 0; i<n; i++)
        siz[add(points[i])]++;
    
    dfs(cent, -1);
    cout << dfs2(cent, -1) << endl;
}

Compilation message

cvenk.cpp: In function 'int main()':
cvenk.cpp:96:9: warning: unused variable '_c' [-Wunused-variable]
   96 |     int _c = 0;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 42 ms 82500 KB Output is correct
2 Correct 43 ms 82380 KB Output is correct
3 Correct 42 ms 82460 KB Output is correct
4 Correct 42 ms 82508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 82760 KB Output is correct
2 Correct 45 ms 82824 KB Output is correct
3 Correct 44 ms 82784 KB Output is correct
4 Correct 44 ms 82812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 88884 KB Output is correct
2 Correct 198 ms 88848 KB Output is correct
3 Correct 111 ms 86640 KB Output is correct
4 Correct 105 ms 86180 KB Output is correct
5 Correct 129 ms 86692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3078 ms 319836 KB Time limit exceeded
2 Halted 0 ms 0 KB -