Submission #761796

# Submission time Handle Problem Language Result Execution time Memory
761796 2023-06-20T09:43:35 Z fatemetmhr Ideal city (IOI12_city) C++17
100 / 100
167 ms 35404 KB
//  ~ Be Name Khoda ~  //

#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  1e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  2e3   + 10;
const int mod   =  2e9;
const ll  inf   =  1e18;

ll ans = 0;
vector <int> avx, av[maxn5];
map <int, int> inner[maxn5];
int ptx[maxn5], par[maxn5];

inline void fix(ll &a){
    if(a >= mod)
        a -= mod;
    if(a < 0)
        a += mod;
}

struct __node{

    vector <int> adj;
    int n, inin;
    ll dissum, dissumup, nup;

    inline void clear(){
        n = inin = 0;
        dissum = dissumup = nup = 0;
        adj.clear();
    }

    inline void rebuild_adj(){
        sort(all(adj));
        adj.resize(unique(all(adj)) - adj.begin());
        adj.shrink_to_fit();
    }

    inline void sumup(int ty){
        //cout << "here " << ty << ' ' << dissum << ' ' << n << ' ' << dissumup << ' ' << nup << endl;
        if(ty != -1){
            fix(dissum += dissumup);
            n += nup;
        }
        fix(ans += dissum * inin % mod);
        //cout << "then " << dissum << ' ' << n << ' ' << inin << ' ' << ans << endl;
    }

} node[maxn5], up[maxn5], tmp;

void dfs_up(int v){
    //cout << "aha " << v << ' ' << par[v] << ' ' << node[v].n << endl;
    for(auto u : node[v].adj) if(u != par[v]){
        par[u] = v;
        dfs_up(u);
        fix(node[v].dissum += node[u].dissum);
        fix(node[v].dissum += node[u].n);
        node[v].n += node[u].n;
    }
}

void dfs_dw(int v){
    //cout << "summing up " << v << endl;
    node[v].sumup(par[v]);
    for(auto u : node[v].adj) if(u != par[v]){
        fix(node[u].dissumup = node[v].dissum + node[v].n);
        fix(node[u].dissumup -= node[u].dissum);
        fix(node[u].dissumup -= node[u].n);
        fix(node[u].dissumup -= node[u].n);
        node[u].nup = node[v].n - node[u].n;
        dfs_dw(u);
    }
}

inline int solve(int n, int *x, int *y){
    ans = 0;
    avx.clear();
    for(int i = 0; i < n; i++)
        avx.pb(x[i]);
    sort(all(avx));
    avx.resize(unique(all(avx)) - avx.begin());
    for(int i = 0; i < avx.size(); i++){
        av[i].clear();
        inner[i].clear();
    }
    for(int i = 0; i < n; i++){
        ptx[i] = lower_bound(all(avx), x[i]) - avx.begin();
        av[ptx[i]].pb(y[i]);
    }
    int newid = 0;
    for(int i = 0; i < avx.size(); i++){
        sort(all(av[i]));
        int last = 0;
        for(int j = 1; j < av[i].size(); j++){
            if(av[i][j] != av[i][j - 1] + 1){
                node[newid].clear();
                node[newid].n = node[newid].inin = j - last;
                for(int k = last; k < j; k++)
                    inner[i][av[i][k]] = newid;
                newid++;
                last = j;
            }
        }
        node[newid].clear();
        node[newid].n = node[newid].inin = int(av[i].size()) - last;
        for(int k = last; k < av[i].size(); k++)
            inner[i][av[i][k]] = newid;
        newid++;
    }

    for(int i = 0; i < n; i++){
        int k = inner[ptx[i]][y[i]];
        if(ptx[i] && inner[ptx[i] - 1].find(y[i]) != inner[ptx[i] - 1].end())
            node[k].adj.pb(inner[ptx[i] - 1][y[i]]);
        if(ptx[i] + 1 < avx.size() && inner[ptx[i] + 1].find(y[i]) != inner[ptx[i] + 1].end())
            node[k].adj.pb(inner[ptx[i] + 1][y[i]]);
    }

    for(int i = 0; i < newid; i++)
        node[i].rebuild_adj();

    par[0] = -1;
    dfs_up(0);
    dfs_dw(0);
    return ans;
}

int DistanceSum(int n, int *x, int *y){

    ll ans = solve(n, x, y);
    //cout << "here " << ans << endl;
    ans = (ans + solve(n, y, x));
    //cout << "now " << ans << endl;
    return (ans % mod) / 2;
}

Compilation message

city.cpp: In function 'int solve(int, int*, int*)':
city.cpp:98:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |     for(int i = 0; i < avx.size(); i++){
      |                    ~~^~~~~~~~~~~~
city.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for(int i = 0; i < avx.size(); i++){
      |                    ~~^~~~~~~~~~~~
city.cpp:110:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |         for(int j = 1; j < av[i].size(); j++){
      |                        ~~^~~~~~~~~~~~~~
city.cpp:122:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |         for(int k = last; k < av[i].size(); k++)
      |                           ~~^~~~~~~~~~~~~~
city.cpp:131:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         if(ptx[i] + 1 < avx.size() && inner[ptx[i] + 1].find(y[i]) != inner[ptx[i] + 1].end())
      |            ~~~~~~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 18260 KB Output is correct
2 Correct 8 ms 18316 KB Output is correct
3 Correct 7 ms 18260 KB Output is correct
4 Correct 7 ms 18260 KB Output is correct
5 Correct 7 ms 18260 KB Output is correct
6 Correct 7 ms 18260 KB Output is correct
7 Correct 8 ms 18256 KB Output is correct
8 Correct 8 ms 18344 KB Output is correct
9 Correct 8 ms 18260 KB Output is correct
10 Correct 7 ms 18260 KB Output is correct
11 Correct 8 ms 18228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18416 KB Output is correct
2 Correct 8 ms 18388 KB Output is correct
3 Correct 9 ms 18368 KB Output is correct
4 Correct 8 ms 18388 KB Output is correct
5 Correct 9 ms 18516 KB Output is correct
6 Correct 9 ms 18516 KB Output is correct
7 Correct 9 ms 18520 KB Output is correct
8 Correct 9 ms 18452 KB Output is correct
9 Correct 9 ms 18388 KB Output is correct
10 Correct 9 ms 18388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 20044 KB Output is correct
2 Correct 31 ms 21072 KB Output is correct
3 Correct 65 ms 22460 KB Output is correct
4 Correct 67 ms 25020 KB Output is correct
5 Correct 154 ms 26820 KB Output is correct
6 Correct 139 ms 31920 KB Output is correct
7 Correct 131 ms 26924 KB Output is correct
8 Correct 129 ms 26800 KB Output is correct
9 Correct 130 ms 31632 KB Output is correct
10 Correct 138 ms 35404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 20108 KB Output is correct
2 Correct 29 ms 20200 KB Output is correct
3 Correct 70 ms 22892 KB Output is correct
4 Correct 78 ms 22864 KB Output is correct
5 Correct 142 ms 28832 KB Output is correct
6 Correct 137 ms 27104 KB Output is correct
7 Correct 167 ms 27492 KB Output is correct
8 Correct 145 ms 27852 KB Output is correct
9 Correct 148 ms 26768 KB Output is correct
10 Correct 137 ms 27756 KB Output is correct