Submission #596649

# Submission time Handle Problem Language Result Execution time Memory
596649 2022-07-14T23:30:16 Z Bench0310 Ideal city (IOI12_city) C++17
100 / 100
488 ms 56516 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int mod=1000000000;
int add(int a,int b){return a+b-(a+b>=mod?mod:0);}
int mul(int a,int b){return (ll(a)*b)%mod;}

int res=0;
const int N=100000;
int n;
vector<array<int,2>> tree[4*N];
vector<vector<array<int,2>>> upd(4*N);
int p[N];
int sz[N];
int events=0;

int find_set(int a)
{
    if(a==p[a]) return a;
    else return find_set(p[a]);
}

//~ void dsu_state()
//~ {
    //~ cout << "DSU state: ";
    //~ map<int,vector<int>> m;
    //~ for(int i=0;i<n;i++) m[find_set(i)].push_back(i);
    //~ cout << "groups(" << m.size() << "):" << endl;
    //~ for(auto [u,v]:m)
    //~ {
        //~ for(int x:v) cout << x << " ";
        //~ cout << "actual_size=" << v.size() << ", sz=" << sz[u] << endl;
    //~ }
    //~ cout << endl;
//~ }

void merge_sets(int a,int b,vector<array<int,2>> &u)
{
    a=find_set(a);
    b=find_set(b);
    if(a==b) return;
    if(sz[a]<sz[b]) swap(a,b);
    //~ cout << "about to merge [" << a << "," << b << "], before:";
    //~ dsu_state();
    u.push_back({b,p[b]});
    u.push_back({a,sz[a]});
    p[b]=a;
    sz[a]+=sz[b];
    //~ cout << "after:";
    //~ dsu_state();
}

void apply(int idx,int l,int r,int ql,int qr,int a,int b)
{
    if(ql>qr) return;
    if(l==ql&&r==qr) tree[idx].push_back({a,b});
    else
    {
        int m=(l+r)/2;
        apply(2*idx,l,m,ql,min(qr,m),a,b);
        apply(2*idx+1,m+1,r,max(ql,m+1),qr,a,b);
    }
}

void process(int idx,int l,int r)
{
    //~ cout << "idx merges[" << idx << "]: l=" << l << ", r=" << r << ": ";
    for(auto [a,b]:tree[idx])
    {
        merge_sets(a,b,upd[idx]);
        //~ cout << "[" << a << "," << b << "] ";
    }
    //~ cout << endl;
    //~ if(l==r)
    //~ {
        //~ cout << "after entering and processing tree[" << l << "," << r << "]: ";
        //~ dsu_state();
    //~ }
    if(l<r)
    {
        int m=(l+r)/2;
        process(2*idx,l,m);
        process(2*idx+1,m+1,r);
    }
    else
    {
        //~ cout << "event #" << l << ": " << sz[find_set(0)] << endl;
        res=add(res,mul(sz[find_set(0)],n-sz[find_set(0)]));
    }
    reverse(upd[idx].begin(),upd[idx].end());
    for(int i=0;i<(int)upd[idx].size();i++) ((i&1)?p:sz)[upd[idx][i][0]]=upd[idx][i][1];
}

void go(int *X,int *Y)
{
    vector<array<int,2>> blocks(n);
    for(int i=0;i<n;i++) blocks[i]={X[i],Y[i]};
    for(int i=0;i<n;i++)
    {
        p[i]=i;
        sz[i]=1;
    }
    events=0;
    map<array<int,2>,int> m;
    for(int i=0;i<n;i++) m[{X[i],Y[i]}]=i;
    auto id=[&](int x,int y)
    {
        if(m.find({x,y})!=m.end()) return m[{x,y}];
        else return -1;
    };
    vector<array<int,4>> v;
    for(int i=0;i<n;i++)
    {
        int t=id(X[i]+1,Y[i]);
        if(t!=-1) v.push_back({X[i],Y[i],i,t});
        int j=id(X[i],Y[i]+1);
        if(j!=-1) merge_sets(i,j,upd[0]);
    }
    sort(v.begin(),v.end());
    int len=v.size();
    vector<array<int,2>> groups;
    int l=0;
    while(l<len)
    {
        int r=l;
        while(r+1<len&&v[r+1][0]==v[l][0]&&v[r][1]+1==v[r+1][1]) r++;
        groups.push_back({l,r});
        events++;
        l=r+1;
    }
    for(int i=1;i<=events;i++)
    {
        //~ cout << "group " << i << ": ";
        auto [one,two]=groups[i-1];
        for(int j=one;j<=two;j++)
        {
            //~ cout << "[" << v[j][2] << "," << v[j][3] << "] ";
            if(i-1>=1) apply(1,1,events,1,i-1,v[j][2],v[j][3]);
            if(i+1<=events) apply(1,1,events,i+1,events,v[j][2],v[j][3]);
        }
        //~ cout << endl;
    }
    process(1,1,events);
    //spring cleaning
    for(int i=0;i<4*N;i++)
    {
        tree[i].clear();
        upd[i].clear();
    }
}

int DistanceSum(int _n,int *x,int *y)
{
    n=_n;
    go(x,y);
    go(y,x);
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 13 ms 19000 KB Output is correct
3 Correct 13 ms 19028 KB Output is correct
4 Correct 14 ms 19096 KB Output is correct
5 Correct 12 ms 19028 KB Output is correct
6 Correct 13 ms 19124 KB Output is correct
7 Correct 13 ms 19112 KB Output is correct
8 Correct 15 ms 19112 KB Output is correct
9 Correct 12 ms 19028 KB Output is correct
10 Correct 13 ms 19156 KB Output is correct
11 Correct 13 ms 19028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 19284 KB Output is correct
2 Correct 16 ms 19352 KB Output is correct
3 Correct 16 ms 19540 KB Output is correct
4 Correct 18 ms 19412 KB Output is correct
5 Correct 17 ms 19628 KB Output is correct
6 Correct 17 ms 19604 KB Output is correct
7 Correct 16 ms 19640 KB Output is correct
8 Correct 17 ms 19668 KB Output is correct
9 Correct 17 ms 19608 KB Output is correct
10 Correct 16 ms 19576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 23860 KB Output is correct
2 Correct 72 ms 24172 KB Output is correct
3 Correct 171 ms 30964 KB Output is correct
4 Correct 173 ms 31776 KB Output is correct
5 Correct 392 ms 43468 KB Output is correct
6 Correct 400 ms 44896 KB Output is correct
7 Correct 379 ms 50380 KB Output is correct
8 Correct 417 ms 41692 KB Output is correct
9 Correct 384 ms 45232 KB Output is correct
10 Correct 372 ms 54880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 25320 KB Output is correct
2 Correct 78 ms 25248 KB Output is correct
3 Correct 191 ms 37228 KB Output is correct
4 Correct 187 ms 35732 KB Output is correct
5 Correct 426 ms 56480 KB Output is correct
6 Correct 484 ms 50020 KB Output is correct
7 Correct 436 ms 56516 KB Output is correct
8 Correct 416 ms 49940 KB Output is correct
9 Correct 488 ms 49876 KB Output is correct
10 Correct 432 ms 49424 KB Output is correct