답안 #53491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53491 2018-06-30T06:15:42 Z 시제연(#2016) Regions (IOI09_regions) C++11
100 / 100
6899 ms 39800 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200005, K = 25005;
using ll = long long;

int n, k, q, a[N], l[N], r[N];
vector<int> c[N], s[K], v[K], up[K], um[K];
map<pair<int, int>, ll> mp;

void f(int x){
    static int y = 0;
    l[x] = ++y;
    for(int i : c[x]) f(i);
    r[x] = y;
}

int g(vector<int> &v, int l, int r){
    return int(upper_bound(v.begin(), v.end(), r) -
               lower_bound(v.begin(), v.end(), l));
}

int main(){
    scanf("%d%d%d%d", &n, &k, &q, a + 1);
    s[a[1]].push_back(1);
    for(int i = 2, x; i <= n; i++){
        scanf("%d%d", &x, a + i);
        s[a[i]].push_back(i);
        c[x].push_back(i);
    }
    f(1);
    for(int i = 1; i <= n; i++){
        v[a[i]].push_back(l[i]);
        up[a[i]].push_back(l[i]);
        um[a[i]].push_back(r[i] + 1);
    }
    for(int i = 1; i < K; i++){
        sort(v[i].begin(), v[i].end());
        sort(up[i].begin(), up[i].end());
        sort(um[i].begin(), um[i].end());
    }
    for(int x, y; q--; ){
        scanf("%d%d", &x, &y);
        if(mp.find({x, y}) != mp.end()){
            printf("%lld\n", mp[{x, y}]); fflush(stdout);
            continue;
        }
        ll t = 0;
        if(s[x].size() > s[y].size())
            for(int i : s[y]) t += g(up[x], 1, l[i]) - g(um[x], 1, l[i]);
        else
            for(int i : s[x]) t += g(v[y], l[i], r[i]);
        mp[{x, y}] = t;
        printf("%lld\n", t); fflush(stdout);
    }
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d%d", &n, &k, &q, a + 1);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, a + i);
         ~~~~~^~~~~~~~~~~~~~~~~~~
regions.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7288 KB Output is correct
2 Correct 8 ms 7352 KB Output is correct
3 Correct 10 ms 7528 KB Output is correct
4 Correct 13 ms 7528 KB Output is correct
5 Correct 13 ms 7664 KB Output is correct
6 Correct 15 ms 7740 KB Output is correct
7 Correct 33 ms 7808 KB Output is correct
8 Correct 38 ms 7808 KB Output is correct
9 Correct 46 ms 8336 KB Output is correct
10 Correct 93 ms 8644 KB Output is correct
11 Correct 96 ms 9036 KB Output is correct
12 Correct 177 ms 9788 KB Output is correct
13 Correct 222 ms 9788 KB Output is correct
14 Correct 389 ms 10308 KB Output is correct
15 Correct 427 ms 12584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1592 ms 14560 KB Output is correct
2 Correct 1778 ms 14560 KB Output is correct
3 Correct 2629 ms 18412 KB Output is correct
4 Correct 292 ms 18412 KB Output is correct
5 Correct 360 ms 18412 KB Output is correct
6 Correct 538 ms 18412 KB Output is correct
7 Correct 788 ms 18412 KB Output is correct
8 Correct 1518 ms 21016 KB Output is correct
9 Correct 2871 ms 27400 KB Output is correct
10 Correct 5638 ms 33476 KB Output is correct
11 Correct 6899 ms 33476 KB Output is correct
12 Correct 2416 ms 33476 KB Output is correct
13 Correct 2903 ms 33476 KB Output is correct
14 Correct 3608 ms 33476 KB Output is correct
15 Correct 5444 ms 36088 KB Output is correct
16 Correct 5377 ms 39800 KB Output is correct
17 Correct 4846 ms 39800 KB Output is correct