답안 #427815

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
427815 2021-06-14T23:48:25 Z Odavey Regions (IOI09_regions) C++17
50 / 100
8000 ms 131076 KB
//
// ~oisín~ C++ Template
//

#include                <bits/stdc++.h>
#define MX_N            200005
#define mp              make_pair
#define mod7            1000000007
#define modpi           314159
#define PI              3.141592653589793238
#define pb              push_back
#define FastIO          ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define All(a)          a.begin(),a.end()
#define fi              first
#define se              second
#define ll              long long int
#define ull             unsigned long long int

int kx[8]  =            {+2, +2, -2, -2, +1, +1, -1, -1};
int ky[8]  =            {+1, -1, +1, -1, +2, -2, +2, -2};
int d9x[9] =            {+1, +1, +1, +0, +0, +0, -1, -1, -1};
int d9y[9] =            {+1, +0, -1, +1, +0, -1, +1, +0, -1};
int dx4[4] =            {+0, +0, +1, -1};
int dy4[4] =            {+1, -1, +0, +0};

ll gcd(ull a, ull b){
    return (a==0)?b:gcd(b%a,a);
}

ll lcm(ull a, ull b){
    return a*(b/gcd(a,b));
}

const long long INF = 1e18;

using namespace std;

int N, R, Q;
int H[MX_N], p[MX_N];
vector<int> adj[MX_N], r[25001];
vector<pair<int, bool> > ord;
unordered_map<int, int> cache[25001];

void dfs(int at){
    ord.pb({at, 0});
    r[H[at]].pb((int)ord.size()-1);
    for(int to : adj[at]){
        if(p[at] == to){
            continue;
        }
        dfs(to);
    }
    ord.pb({at, 1});
    r[H[at]].pb((int)ord.size()-1);
}

int main(){
    cin >> N >> R >> Q;
//    int c = 10000;
    int c = sqrt(N);
//    int c = 1000000000;
    cin >> H[0];
    --H[0];
    for(int i=1;i<N;++i){
        cin >> p[i] >> H[i];
        --p[i], --H[i];
        adj[p[i]].pb(i);
    }
    dfs(0);

    for(int j=0;j<R;++j){
        if((int)r[j].size() >= c){
            for(int i=0;i<R;++i){
                if(i == j){
                    continue;
                }
                int x = 0, y = 0;
                int cur;
                int cntA = 0, cntB = 0;
                int ans = 0;
                while(true){
                    if(x == (int)r[i].size() && y == (int)r[j].size()){
                        break;
                    }else if(x == (int)r[i].size()){
                        cur = r[j][y];
                        ++y;
                    }else if(y == (int)r[j].size()){
                        cur = r[i][x];
                        ++x;
                    }else{
                        if(r[i][x] < r[j][y]){
                            cur = r[i][x];
                            ++x;
                        }else{
                            cur = r[j][y];
                            ++y;
                        }
                    }
                    auto P = ord[cur];
                    if(H[P.fi] == i){
                        if(P.se == 0){
                            ++cntA;
                        }else{
                            --cntA;
                        }
                    }else{
                        if(P.se == 0){
                            ++cntB;
                            ans += cntA;
                        }else{
                            --cntB;
                        }
                    }
                }
                cache[i][j] = ans;
            }
        }
    }

    for(int i=0;i<R;++i){
        if((int)r[i].size() >= c){
            for(int j=0;j<R;++j){
                if(i == j){
                    continue;
                }
                int x = 0, y = 0;
                int cur;
                int cntA = 0, cntB = 0;
                int ans = 0;
                while(true){
                    if(x == (int)r[i].size() && y == (int)r[j].size()){
                        break;
                    }else if(x == (int)r[i].size()){
                        cur = r[j][y];
                        ++y;
                    }else if(y == (int)r[j].size()){
                        cur = r[i][x];
                        ++x;
                    }else{
                        if(r[i][x] < r[j][y]){
                            cur = r[i][x];
                            ++x;
                        }else{
                            cur = r[j][y];
                            ++y;
                        }
                    }
                    auto P = ord[cur];
                    if(H[P.fi] == i){
                        if(P.se == 0){
                            ++cntA;
                        }else{
                            --cntA;
                        }
                    }else{
                        if(P.se == 0){
                            ++cntB;
                            ans += cntA;
                        }else{
                            --cntB;
                        }
                    }
                }
                cache[i][j] = ans;
            }
        }
    }

    while(Q--){
        int r1, r2;
        cin >> r1 >> r2;
        --r1, --r2;
        if((int)r[r1].size() >= c || (int)r[r2].size() >= c){
            cout << cache[r1][r2] << endl;
            cout.flush();
        }else{
            int x = 0, y = 0;
            int cur;
            int cntA = 0, cntB = 0;
            int ans = 0;
            while(true){
                if(x == (int)r[r1].size() && y == (int)r[r2].size()){
                    break;
                }else if(x == (int)r[r1].size()){
                    cur = r[r2][y];
                    ++y;
                }else if(y == (int)r[r2].size()){
                    cur = r[r1][x];
                    ++x;
                }else{
                    if(r[r1][x] < r[r2][y]){
                        cur = r[r1][x];
                        ++x;
                    }else{
                        cur = r[r2][y];
                        ++y;
                    }
                }
                auto P = ord[cur];
                if(H[P.fi] == r1){
                    if(P.se == 0){
                        ++cntA;
                    }else{
                        --cntA;
                    }
                }else{
                    if(P.se == 0){
                        ++cntB;
                        ans += cntA;
                    }else{
                        --cntB;
                    }
                }
            }
            cout << ans << endl;
            cout.flush();
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6856 KB Output is correct
2 Correct 5 ms 6856 KB Output is correct
3 Correct 6 ms 6856 KB Output is correct
4 Correct 10 ms 6984 KB Output is correct
5 Correct 18 ms 6984 KB Output is correct
6 Correct 34 ms 6984 KB Output is correct
7 Correct 34 ms 6984 KB Output is correct
8 Correct 44 ms 7112 KB Output is correct
9 Correct 70 ms 7760 KB Output is correct
10 Correct 63 ms 7624 KB Output is correct
11 Correct 152 ms 9424 KB Output is correct
12 Correct 113 ms 8772 KB Output is correct
13 Correct 296 ms 10260 KB Output is correct
14 Correct 472 ms 10560 KB Output is correct
15 Correct 863 ms 15124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1489 ms 14520 KB Output is correct
2 Correct 1520 ms 13140 KB Output is correct
3 Correct 2613 ms 18744 KB Output is correct
4 Correct 1491 ms 41668 KB Output is correct
5 Correct 1073 ms 34424 KB Output is correct
6 Correct 4218 ms 48684 KB Output is correct
7 Correct 6066 ms 81636 KB Output is correct
8 Execution timed out 8023 ms 89600 KB Time limit exceeded
9 Execution timed out 8029 ms 118500 KB Time limit exceeded
10 Execution timed out 8037 ms 122412 KB Time limit exceeded
11 Runtime error 7637 ms 131076 KB Execution killed with signal 9
12 Execution timed out 8032 ms 23140 KB Time limit exceeded
13 Execution timed out 8074 ms 52680 KB Time limit exceeded
14 Execution timed out 8029 ms 34608 KB Time limit exceeded
15 Execution timed out 8076 ms 30204 KB Time limit exceeded
16 Runtime error 6041 ms 131076 KB Execution killed with signal 9
17 Execution timed out 8032 ms 50836 KB Time limit exceeded