Submission #1051111

# Submission time Handle Problem Language Result Execution time Memory
1051111 2024-08-09T20:02:39 Z Ludissey Regions (IOI09_regions) C++17
0 / 100
2021 ms 131072 KB
#include <bits/stdc++.h>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
 
using namespace std;

vector<vector<int>> child;
vector<int> parent;
vector<vector<int>> up;
vector<vector<int>> down;
unordered_map<int,int> conv;
vector<int> rg;
vector<vector<int>> pr;
int need;
int sq;
vector<int> tin;
vector<int> out;
int timer=0;


vector<int> setup_down(int x){
    vector<int> cv(sq);
    for (auto u : child[x]) {
        vector<int> v=setup_down(u);
        for (int i=0; i<sq; i++)
        {
            down[conv[rg[x]]][i]+=v[i];
            cv[i]+=v[i];
        }
    }
    if(conv[rg[x]]<sq){
        down[conv[rg[x]]][conv[rg[x]]]++;
        cv[conv[rg[x]]]++;
    }
    return cv;
}

void setup_up(int x, vector<int> rm){
    for (int i=0; i<sz(rm); i++)
    {
        up[conv[rg[x]]][i]+=rm[i];
    }
    if(conv[rg[x]]<sq){
        up[conv[rg[x]]][conv[rg[x]]]++;
        rm[conv[rg[x]]]++;
    }
    for (auto u : child[x]) setup_up(u,rm);
    return;
}

void setup_euler(int x){
    tin[x]=timer++;
    for (auto u : child[x]) setup_euler(u);
    out[x]=timer++;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int N,R,Q; cin >> N >> R >> Q;
    sq=500;
    child.resize(N);
    parent.resize(N);
    up.resize(R,vector<int>(sq));
    down.resize(R,vector<int>(sq));
    rg.resize(N);
    tin.resize(N);
    out.resize(N);
    pr.resize(R);
    cin >> rg[0]; rg[0]--;
    vector<pair<int,int>> cnt(R);
    pr[rg[0]].push_back(0);
    cnt[rg[0]].first++;
    for (int i = 1; i < N; i++)
    {
        cin >> parent[i] >> rg[i]; parent[i]--; rg[i]--;
        child[parent[i]].push_back(i);
        pr[rg[i]].push_back(i);
        cnt[rg[i]].first++;
    }
    for (int i = 0; i < R; i++) cnt[i].second=i;
    
    sort(all(cnt),[&](auto &aa, auto &bb){return aa>bb; });
    for (int i = 0; i < sz(cnt); i++)
    {
        conv[cnt[i].second]=i;
    }
    vector<int> ep(sq);
    timer=0;
    setup_euler(0);
    setup_up(0,ep);
    setup_down(0);
    for (int i = 0; i < Q; i++)
    {
        int sm=0;
        int e1,e2; cin >> e1>>e2; e1--; e2--;
        vector<pair<int,pair<int,int>>> tos;
        vector<int> forCONV;
        unordered_map<int,int> conv2;
        for (int j = 0; j < sz(pr[e1]); j++)
        {
            int in1=tin[pr[e1][j]], out1=out[pr[e1][j]];
            forCONV.push_back(in1);
            forCONV.push_back(out1);
        }   
        for (int j = 0; j < sz(pr[e2]); j++)
        {
            int in1=tin[pr[e2][j]], out1=out[pr[e2][j]];
            forCONV.push_back(in1);
            forCONV.push_back(out1);
        }
        sort(all(forCONV));
        for (int j = 0; j < sz(forCONV); j++)
        {
            conv2[forCONV[j]]=j;
        }
        
        for (int j = 0; j < sz(pr[e1]); j++)
        {
            int in1=tin[pr[e1][j]], out1=out[pr[e1][j]];
            tos.push_back({conv2[in1],{conv2[out1],1}});
        }   
        for (int j = 0; j < sz(pr[e2]); j++)
        {
            int in1=tin[pr[e2][j]], out1=out[pr[e2][j]];
            tos.push_back({conv2[in1],{conv2[out1],2}});
        }
        sort(all(tos));
        vector<int> t;
        t.resize(sz(forCONV)*2+1,0);
        int ones=0;
        for (int j = 0; j < sz(tos); j++)
        {
            if(tos[j].second.second==1) {
                ones++;
                int p=tos[j].second.first;
                int value=1;
                for (t[p += sz(forCONV)] = value; p > 1; p >>= 1) t[p>>1] = t[p] + t[p^1];
            }
            else{
                int csum=0;
                int l=0;
                int r=tos[j].second.first+1;
                for (l += sz(forCONV), r += sz(forCONV); l < r; l >>= 1, r >>= 1) {
                    if (l&1) csum += t[l++];
                    if (r&1) csum += t[--r];
                }
                sm+=ones-csum;
            }
        }
        cout << sm << "\n";
    }
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 0 ms 344 KB Time limit exceeded (wall clock)
2 Execution timed out 1 ms 344 KB Time limit exceeded (wall clock)
3 Execution timed out 1 ms 344 KB Time limit exceeded (wall clock)
4 Execution timed out 2 ms 600 KB Time limit exceeded (wall clock)
5 Execution timed out 5 ms 856 KB Time limit exceeded (wall clock)
6 Execution timed out 10 ms 3160 KB Time limit exceeded (wall clock)
7 Execution timed out 13 ms 1620 KB Time limit exceeded (wall clock)
8 Execution timed out 18 ms 1980 KB Time limit exceeded (wall clock)
9 Execution timed out 58 ms 12444 KB Time limit exceeded (wall clock)
10 Execution timed out 84 ms 3264 KB Time limit exceeded (wall clock)
11 Execution timed out 131 ms 3976 KB Time limit exceeded (wall clock)
12 Execution timed out 226 ms 12988 KB Time limit exceeded (wall clock)
13 Execution timed out 211 ms 3680 KB Time limit exceeded (wall clock)
14 Execution timed out 248 ms 3880 KB Time limit exceeded (wall clock)
15 Execution timed out 486 ms 88212 KB Time limit exceeded (wall clock)
# Verdict Execution time Memory Grader output
1 Execution timed out 846 ms 37808 KB Time limit exceeded (wall clock)
2 Execution timed out 674 ms 10624 KB Time limit exceeded (wall clock)
3 Execution timed out 1025 ms 81668 KB Time limit exceeded (wall clock)
4 Execution timed out 285 ms 22276 KB Time limit exceeded (wall clock)
5 Execution timed out 440 ms 76652 KB Time limit exceeded (wall clock)
6 Execution timed out 501 ms 44124 KB Time limit exceeded (wall clock)
7 Execution timed out 674 ms 56144 KB Time limit exceeded (wall clock)
8 Runtime error 318 ms 131072 KB Execution killed with signal 9
9 Execution timed out 1618 ms 111736 KB Time limit exceeded (wall clock)
10 Runtime error 111 ms 131072 KB Execution killed with signal 9
11 Execution timed out 1717 ms 121136 KB Time limit exceeded (wall clock)
12 Execution timed out 2021 ms 87072 KB Time limit exceeded (wall clock)
13 Execution timed out 1939 ms 119304 KB Time limit exceeded (wall clock)
14 Execution timed out 1949 ms 112164 KB Time limit exceeded (wall clock)
15 Runtime error 125 ms 131072 KB Execution killed with signal 9
16 Runtime error 91 ms 131072 KB Execution killed with signal 9
17 Runtime error 179 ms 131072 KB Execution killed with signal 9