Submission #852569

# Submission time Handle Problem Language Result Execution time Memory
852569 2023-09-22T05:58:38 Z abcvuitunggio Regions (IOI09_regions) C++17
90 / 100
1706 ms 131072 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int sz=4000;
int lim,n,R,q,r1,r2,cnt[200001][200000/sz+1],c[25001][200000/sz+1],c2[25001][200000/sz+1],r[200001],p[200001],tin[200001],tout[200001],t,C[25001],pos[25001];
vector <int> ke[200001],ve[25001],V;
void dfs(int u){
    tin[u]=++t;
    ve[r[u]].push_back(t);
    if (C[r[u]]>sz)
        cnt[u][pos[r[u]]]++;
    for (int v:ke[u]){
        for (int i=1;i<=lim;i++)
            cnt[v][i]+=cnt[u][i];
        dfs(v);
    }
    for (int i=1;i<=lim;i++)
        c[r[u]][i]+=cnt[u][i];
    tout[u]=++t;
    ve[r[u]].push_back(-t);
}
void dfs2(int u){
    if (C[r[u]]>sz)
        cnt[u][pos[r[u]]]++;
    for (int v:ke[u]){
        dfs2(v);
        for (int i=1;i<=lim;i++)
            cnt[u][i]+=cnt[v][i];
    }
    for (int i=1;i<=lim;i++)
        c2[r[u]][i]+=cnt[u][i];
}
int32_t main(){
    ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
    cin >> n >> R >> q;
    for (int i=1;i<=n;i++){
        if (i>1){
            cin >> p[i];
            ke[p[i]].push_back(i);
        }
        cin >> r[i];
        C[r[i]]++;
    }
    for (int i=1;i<=R;i++)
        V.push_back(i);
    sort(V.begin(),V.end(),[](int i, int j){return C[i]>C[j];});
    for (int i=0;i<V.size();i++){
        pos[V[i]]=i+1;
        if (C[V[i]]>sz)
            lim=i+1;
    }
    dfs(1);
    memset(cnt,0,sizeof(cnt));
    dfs2(1);
    while (q--){
        cin >> r1 >> r2;
        if (ve[r1].empty()||ve[r2].empty()){
            cout << 0 << endl;
            continue;
        }
        if (C[r1]>sz){
            cout << c[r2][pos[r1]] << endl;
            continue;
        }
        if (C[r2]>sz){
            cout << c2[r1][pos[r2]] << endl;
            continue;
        }
        int j=0,cnt=0,res=0;
        for (int i:ve[r2]){
            while (j<ve[r1].size()){
                if (abs(ve[r1][j])>abs(i))
                    break;
                cnt+=(ve[r1][j]>0)-(ve[r1][j]<0);
                j++;
            }
            if (i>0)
                res+=cnt;
        }
        cout << res << endl;
    }
}

Compilation message

regions.cpp: In function 'int32_t main()':
regions.cpp:47:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for (int i=0;i<V.size();i++){
      |                  ~^~~~~~~~~
regions.cpp:71:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             while (j<ve[r1].size()){
      |                    ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 91480 KB Output is correct
2 Correct 11 ms 91480 KB Output is correct
3 Correct 12 ms 91480 KB Output is correct
4 Correct 14 ms 91632 KB Output is correct
5 Correct 15 ms 91480 KB Output is correct
6 Correct 18 ms 91676 KB Output is correct
7 Correct 25 ms 91736 KB Output is correct
8 Correct 30 ms 91736 KB Output is correct
9 Correct 35 ms 92248 KB Output is correct
10 Correct 54 ms 94040 KB Output is correct
11 Correct 81 ms 94592 KB Output is correct
12 Correct 86 ms 95212 KB Output is correct
13 Correct 103 ms 94888 KB Output is correct
14 Correct 130 ms 95480 KB Output is correct
15 Correct 165 ms 99208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 643 ms 100756 KB Output is correct
2 Correct 680 ms 99420 KB Output is correct
3 Correct 1121 ms 102180 KB Output is correct
4 Correct 169 ms 95484 KB Output is correct
5 Correct 214 ms 97756 KB Output is correct
6 Correct 501 ms 96516 KB Output is correct
7 Correct 694 ms 97740 KB Output is correct
8 Correct 644 ms 104376 KB Output is correct
9 Correct 985 ms 103836 KB Output is correct
10 Correct 1706 ms 109952 KB Output is correct
11 Correct 1675 ms 103116 KB Output is correct
12 Correct 670 ms 118172 KB Output is correct
13 Correct 938 ms 118852 KB Output is correct
14 Correct 1114 ms 121576 KB Output is correct
15 Correct 1542 ms 128480 KB Output is correct
16 Runtime error 89 ms 131072 KB Execution killed with signal 9
17 Runtime error 78 ms 131072 KB Execution killed with signal 9