Submission #901575

# Submission time Handle Problem Language Result Execution time Memory
901575 2024-01-09T15:28:39 Z JakobZorz Ideal city (IOI12_city) C++17
100 / 100
223 ms 27560 KB
#include<iostream>
#include<vector>
#include<limits.h>
#include<unordered_set>
#include<map>
using namespace std;
typedef long long ll;
const int MOD=1e9;
const int BIG=INT_MAX;

int w,h;
vector<unordered_set<int>>cols;
map<pair<int,int>,bool>vis;

struct Tree{
    int val=0;
    vector<Tree*>ch;
    pair<int,int>sum;
    bool sumt=false;
    
    int get(){
        vector<pair<int,int>>vals;
        for(auto c:ch){
            auto r=c->get_sum();
            vals.emplace_back(r.first+r.second,r.first);
        }
        
        int res=0;
        
        for(int i=0;i<(int)vals.size();i++){
            for(int j=i-1;j>=0;j--){
                res=(res+(ll)vals[i].second*vals[j].first+(ll)vals[i].first*vals[j].second)%MOD;
            }
        }
        
        for(auto i:vals)
            res=(res+(ll)i.first*val)%MOD;
        
        for(auto c:ch){
            res+=c->get();
            res%=MOD;
        }
        
        return res;
    }
    
    pair<int,int>get_sum(){ // (num_nodes,sum_of_paths)
        if(sumt)
            return sum;
        
        sum={val,0};
        for(auto c:ch){
            auto r=c->get_sum();
            sum.first+=r.first;
            sum.second+=(r.second+r.first)%MOD;
            sum.second%=MOD;
        }
        sumt=true;
        return sum;
    }
};

Tree*get_tree(int x,int y){
    if(vis[{x,y}])
        return nullptr;
    if(x<0||x>=w)
        return nullptr;
    if(cols[x].find(y)==cols[x].end())
        return nullptr;
    vis[{x,y}]=true;
    
    Tree*res=new Tree;
    res->val=1;
    int cy=y-1;
    while(cols[x].find(cy)!=cols[x].end()){
        vis[{x,cy}]=true;
        cy--;
        res->val++;
    }
    
    cy=y+1;
    while(cols[x].find(cy)!=cols[x].end()){
        vis[{x,cy}]=true;
        cy++;
        res->val++;
    }
    
    {
        auto r=get_tree(x+1,y);
        if(r)
            res->ch.push_back(r);
    }
    {
        auto r=get_tree(x-1,y);
        if(r)
            res->ch.push_back(r);
    }
    
    cy=y-1;
    while(cols[x].find(cy)!=cols[x].end()){
        {
            auto r=get_tree(x+1,cy);
            if(r)
                res->ch.push_back(r);
        }
        {
            auto r=get_tree(x-1,cy);
            if(r)
                res->ch.push_back(r);
        }
        cy--;
    }
    
    cy=y+1;
    while(cols[x].find(cy)!=cols[x].end()){
        {
            auto r=get_tree(x+1,cy);
            if(r)
                res->ch.push_back(r);
        }
        {
            auto r=get_tree(x-1,cy);
            if(r)
                res->ch.push_back(r);
        }
        cy++;
    }
    return res;
}

int get_dist(vector<pair<int,int>>vec){
    w=0;
    h=0;
    for(auto i:vec){
        w=max(w,i.first+1);
        h=max(h,i.second+1);
    }
    cols.clear();
    cols.resize(w);
    vis.clear();
    for(auto i:vec)
        cols[i.first].insert(i.second);
    
    Tree*tree=get_tree(0,*cols[0].begin());
    int r=tree->get();
    return r;
}

int DistanceSum(int N,int *X,int *Y){
    int mx=BIG,my=BIG;
    for(int i=0;i<N;i++){
        mx=min(mx,X[i]);
        my=min(my,Y[i]);
    }
    vector<pair<int,int>>vec1,vec2;
    for(int i=0;i<N;i++){
        vec1.emplace_back(X[i]-mx,Y[i]-my);
        vec2.emplace_back(Y[i]-my,X[i]-mx);
    }
    return(get_dist(vec1)+get_dist(vec2))%MOD;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 440 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 860 KB Output is correct
4 Correct 2 ms 752 KB Output is correct
5 Correct 3 ms 860 KB Output is correct
6 Correct 2 ms 708 KB Output is correct
7 Correct 3 ms 856 KB Output is correct
8 Correct 2 ms 856 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 3464 KB Output is correct
2 Correct 19 ms 3720 KB Output is correct
3 Correct 39 ms 7908 KB Output is correct
4 Correct 41 ms 8396 KB Output is correct
5 Correct 102 ms 15864 KB Output is correct
6 Correct 85 ms 16688 KB Output is correct
7 Correct 106 ms 15816 KB Output is correct
8 Correct 84 ms 15308 KB Output is correct
9 Correct 96 ms 16408 KB Output is correct
10 Correct 117 ms 27560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 5500 KB Output is correct
2 Correct 24 ms 4988 KB Output is correct
3 Correct 78 ms 13664 KB Output is correct
4 Correct 65 ms 11716 KB Output is correct
5 Correct 223 ms 26944 KB Output is correct
6 Correct 132 ms 20360 KB Output is correct
7 Correct 200 ms 27324 KB Output is correct
8 Correct 137 ms 20804 KB Output is correct
9 Correct 115 ms 19132 KB Output is correct
10 Correct 133 ms 18928 KB Output is correct