Submission #298952

# Submission time Handle Problem Language Result Execution time Memory
298952 2020-09-14T11:11:51 Z jovan_b Regions (IOI09_regions) C++17
26 / 100
8000 ms 131076 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int LIM = 0;
int in[200005];
int out[200005];
int region[200005];
int parent[200005];
int cnt[200005];
vector <int> graf[200005];

ll gore[25005][505];
ll dole[25005][505];

int sada[25005];
int rev[25005];
int compress[25005];
int ccomp;

int r;

int tajm;

vector <pair <int, int>> svi[25005];
vector <int> ins[25005];

void dfs(int v){

    in[v] = ++tajm;
    sada[region[v]]++;
    if(compress[region[v]]){
        for(int i=1; i<=r; i++){
            dole[i][compress[region[v]]] += sada[region[v]];
        }
        dole[region[v]][compress[region[v]]]--;
        gore[region[v]][compress[region[v]]]--;
    }
    for(int i=1; i<=ccomp; i++){
        gore[region[v]][i] += sada[rev[i]];
    }
    for(auto c : graf[v]){
        dfs(c);
    }
    sada[region[v]]--;
    out[v] = tajm;
    svi[region[v]].push_back({in[v], 1});
    svi[region[v]].push_back({out[v]+1, -1});
    ins[region[v]].push_back(in[v]);
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);

    int n, q;
    cin >> n >> r >> q;
    cin >> region[1];
    cnt[region[1]]++;
    for(int i=2; i<=n; i++){
        cin >> parent[i] >> region[i];
        cnt[region[i]]++;
        graf[parent[i]].push_back(i);
    }
    for(int i=1; i<=r; i++){
        if(cnt[i] <= LIM) continue;
        compress[i] = ++ccomp;
        rev[ccomp] = i;
    }
    dfs(1);
    for(int i=1; i<=r; i++) sort(svi[i].begin(), svi[i].end());
    for(int i=1; i<=r; i++) sort(ins[i].begin(), ins[i].end());
    while(q--){
        int a, b;
        cin >> a >> b;
        if(cnt[a] > LIM){
            cout << gore[b][compress[a]] << endl;
            continue;
        }
        if(cnt[b] > LIM){
            cout << dole[a][compress[b]] << endl;
            continue;
        }
        int k = svi[a].size();
        int j = -1;
        int tren = 0;
        ll res = 0;
        for(auto c : ins[b]){
            while(j < k-1 && c >= svi[a][j+1].first){
                j++;
                tren += svi[a][j].second;
            }
            res += tren;
        }
        cout << res << endl;
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 6272 KB Output isn't correct
2 Incorrect 5 ms 6272 KB Output isn't correct
3 Incorrect 6 ms 6400 KB Output isn't correct
4 Correct 8 ms 6528 KB Output is correct
5 Correct 10 ms 6656 KB Output is correct
6 Incorrect 27 ms 8320 KB Output isn't correct
7 Correct 20 ms 7552 KB Output is correct
8 Correct 39 ms 8064 KB Output is correct
9 Correct 61 ms 9344 KB Output is correct
10 Correct 89 ms 10368 KB Output is correct
11 Correct 74 ms 9620 KB Output is correct
12 Correct 137 ms 11700 KB Output is correct
13 Correct 174 ms 10676 KB Output is correct
14 Correct 140 ms 10328 KB Output is correct
15 Correct 210 ms 13432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 751 ms 13988 KB Output is correct
2 Correct 882 ms 13168 KB Output is correct
3 Correct 1197 ms 16544 KB Output is correct
4 Incorrect 2009 ms 40368 KB Output isn't correct
5 Incorrect 3003 ms 49896 KB Output isn't correct
6 Incorrect 5940 ms 65680 KB Output isn't correct
7 Execution timed out 8093 ms 90404 KB Time limit exceeded
8 Execution timed out 8057 ms 91968 KB Time limit exceeded
9 Runtime error 2495 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
10 Runtime error 153 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
11 Runtime error 174 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
12 Runtime error 660 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
13 Runtime error 693 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
14 Runtime error 299 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
15 Runtime error 162 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
16 Runtime error 169 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
17 Runtime error 286 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)