제출 #765320

#제출 시각아이디문제언어결과실행 시간메모리
765320keta_tsimakuridzeRegions (IOI09_regions)C++14
55 / 100
1239 ms131072 KiB
#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, M = 25e3 + 5, mod = 1e9 + 7, B = 2e3; // !
int t, a[N], id[M], cur, cnt[M], CN2[N][105], CN1[M][105],timer;
vector<pair<int,int>> reg[M];
vector<int> V[N];
void dfs(int u) {
    ++timer;
    reg[a[u]].push_back({timer, 1});
    for(int i = 0; i < V[u].size(); i++) {
        int v = V[u][i];
        dfs(v);
        for(int j = 1; j <= cur; j++) {
            CN2[u][j] += CN2[v][j];
        }
    }
    ++CN2[u][id[a[u]]];
    for(int j = 1; j <= cur; j++) {
        CN1[a[u]][j] += CN2[u][j];
    }
    reg[a[u]].push_back({timer + 1, 0});
}
void dfs2(int u) {
    ++cnt[id[a[u]]];
    for(int j = 1; j <= cur; j++) {
        CN2[a[u]][j] += cnt[j];
    }
    for(int i = 0; i < V[u].size(); i++) dfs2(V[u][i]);
    --cnt[id[a[u]]];
}
main(){
    int n, r, q;
    cin >> n >> r >> q;
    for(int i = 1; i <= n; i++) {
        if(i >= 2) {
            int k; cin >> k;
            V[k].push_back(i);
        }
        cin >> a[i];
        ++cnt[a[i]];
    }
    cur = 0;
    for(int i = 1; i <= r; i++) {
        if(cnt[i] >= B) {
            id[i] = ++cur;
        }
        cnt[i] = 0;
    }
    dfs(1);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= cur; j++) CN2[i][j] = 0;
    dfs2(1);
    while(q--) {
        int u, v;
        cin >> u >> v;
        if(id[u]) {
            cout << CN2[v][id[u]] << endl;
        } else if(id[v]) {
            cout << CN1[u][id[v]] << endl;
        } else {
            int l = -1;
            int cn = 0, ans = 0;
            for(int i = 0; i < reg[u].size(); i++) {
                while(l + 1 < (int)reg[v].size() && reg[v][l + 1].f < reg[u][i].f) {
                    ++l;
                    cn += reg[v][l].s;
                }
                ans += cn * (reg[u][i].s ? -1 : 1);
            }
            cout << ans << endl;
        }
    }
 }

컴파일 시 표준 에러 (stderr) 메시지

regions.cpp: In function 'void dfs(long long int)':
regions.cpp:14:22: 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]
   14 |     for(int i = 0; i < V[u].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
regions.cpp: In function 'void dfs2(long long int)':
regions.cpp:32:22: 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]
   32 |     for(int i = 0; i < V[u].size(); i++) dfs2(V[u][i]);
      |                    ~~^~~~~~~~~~~~~
regions.cpp: At global scope:
regions.cpp:35:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   35 | main(){
      | ^~~~
regions.cpp: In function 'int main()':
regions.cpp:66:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |             for(int i = 0; i < reg[u].size(); i++) {
      |                            ~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...