답안 #962399

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
962399 2024-04-13T12:48:31 Z GrindMachine 이상적인 도시 (IOI12_city) C++17
0 / 100
1000 ms 13324 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<int> dr = {0,1};
vector<int> dc = {1,0};

vector<pll> adj1[N];
vector<bool> vis(N);
vector<ll> tin(N), low(N);
vector<bool> bridge(2*N);
ll timer = 1;

void dfs1(ll u, ll p){
    vis[u] = 1;
    tin[u] = low[u] = timer++;
    for(auto [v,id] : adj1[u]){
        if(v == p) conts;
        if(vis[v]){
            amin(low[u],tin[v]);
        }
        else{
            dfs1(v,u);
            amin(low[u],low[v]);
            if(low[v] > low[u]){
                bridge[id] = 1;
            }
        }
    }
}

vector<ll> col(N);
vector<pll> a;
vector<ll> nodes;

void dfs2(ll u, ll p, ll c){
    vis[u] = 1;
    col[u] = c;
    nodes.pb(u);

    for(auto [v,id] : adj1[u]){
        if(vis[v] or bridge[id]) conts;
        dfs2(v,u,c);
    }
}

ll within_comp(){
    ll siz = sz(nodes);
    vector<ll> cx(siz), cy(siz);
    rep(ind,siz){
        ll i = nodes[ind];
        cx[ind] = a[i].ff, cy[ind] = a[i].ss;
    }

    sort(all(cx)), sort(all(cy));

    ll res = 0;
 
    {
        ll sum = 0;
        rep(i,siz){
            res += cx[i]*i-sum;
            res = (res%MOD+MOD)%MOD;
            sum += cx[i];
        }
    }
 
    {
        ll sum = 0;
        rep(i,siz){
            res += cy[i]*i-sum;
            res = (res%MOD+MOD)%MOD;
            sum += cy[i];
        }
    }
 
    return res%MOD;
}

ll dis(ll i, ll j){
    auto [x1,y1] = a[i];
    auto [x2,y2] = a[j];
    return abs(x1-x2)+abs(y1-y2);
}

vector<pll> adj2[N];
vector<ll> subsiz(N);
vector<ll> comp_sum(N);

void dfs3(ll u, ll p){
    for(auto [v,i] : adj2[u]){
        if(v == p) conts;
        dfs3(v,u);
        subsiz[u] += subsiz[v];
    }
}

ll dfs4(ll u, ll p){
    ll res = 0;
    vector<pll> children;
    ll par_id = -1;

    for(auto [v,i] : adj2[u]){
        if(v == p){
            par_id = i;
            conts;
        }
        res += dfs4(v,u);
        res %= MOD;
        children.pb({v,i});
    }

    if(par_id != -1){
        for(auto [v,i] : children){
            ll add = dis(i,par_id)%MOD*subsiz[v]%MOD*(subsiz[0]-subsiz[u])%MOD;
            res += add;
            res %= MOD;
        }   
    }

    for(auto [v,i] : children){
        ll add = comp_sum[i]*subsiz[v]%MOD;
        res += add;
        res %= MOD;
    }

    if(par_id != -1){
        ll add = comp_sum[par_id]*(subsiz[0]-subsiz[u])%MOD;
        res += add;
        res %= MOD;
    }

    for(auto [v1,i] : children){
        for(auto [v2,j] : children){
            if(v1 > v2) conts;
            ll add = dis(i,j)%MOD*subsiz[v1]%MOD*subsiz[v2]%MOD;
            res += add;
            res %= MOD;
        }
    }

    return res;
}

int DistanceSum(int n, int *X, int *Y) {
    rep(i,n) a.pb({X[i],Y[i]});
    sort(all(a));
    ll ptr = 0;

    rep(i,n){
        auto [r1,c1] = a[i];
        rep(dir,2){
            ll r2 = r1+dr[dir], c2 = c1+dc[dir];
            pll px = {r2,c2};
            if(!binary_search(all(a),px)) conts;
            ll j = lower_bound(all(a),px)-a.begin();
            adj1[i].pb({j,ptr}), adj1[j].pb({i,ptr});
            ptr++;
        }
    }

    dfs1(0,-1);
    
    fill(all(vis),0);
    ptr = 0;

    ll ans = 0;

    rep(i,n){
        if(vis[i]) conts;
        nodes.clear();
        dfs2(i,-1,ptr++);
        ans += within_comp();
        ans %= MOD;

        trav(u,nodes){
            trav(v,nodes){
                comp_sum[u] += dis(u,v);
                comp_sum[u] %= MOD;
            }
        }
    }

    rep(u,n){
        for(auto [v,id] : adj1[u]){
            if(col[u] != col[v]){
                adj2[col[u]].pb({col[v],u});
            }
        }
    }

    rep(u,n) subsiz[col[u]]++;
    dfs3(0,-1);

    rep(u,ptr){
        ll s = subsiz[u];
        ans += s*(n-s);
        ans %= MOD;
    }

    ans += dfs4(0,-1);
    ans %= MOD;

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9052 KB Output is correct
2 Correct 4 ms 9048 KB Output is correct
3 Correct 3 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Incorrect 4 ms 8900 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 9208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1040 ms 13324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 11464 KB Output isn't correct
2 Halted 0 ms 0 KB -