답안 #427790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
427790 2021-06-14T22:48:21 Z Odavey Regions (IOI09_regions) C++17
16 / 100
8000 ms 66948 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;
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 = 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;
                if(r[i][0] < r[j][0]){
                    cur = r[i][0];
                    ++x;
                }else{
                    cur = r[j][0];
                    ++y;
                }
                int cntA = 0, cntB = 0;
                int ans = 0;
                while(true){
                    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;
                        }
                    }
                    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;
                        }
                    }
                }
                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;
                if(r[i][0] < r[j][0]){
                    cur = r[i][0];
                    ++x;
                }else{
                    cur = r[j][0];
                    ++y;
                }
                int cntA = 0, cntB = 0;
                int ans = 0;
                while(true){
                    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;
                        }
                    }
                    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;
                        }
                    }
                }
                cache[i][j] = ans;
            }
        }
    }

    while(Q--){
        int r1, r2;
        cin >> r1 >> r2;
        --r1, --r2;
        if(r[r1].empty() || r[r2].empty()){
            cout << "000000000\n";
        }
        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;
            if(r[r1][0] < r[r2][0]){
                cur = r[r1][0];
                ++x;
            }else{
                cur = r[r2][0];
                ++y;
            }
            int cntA = 0, cntB = 0;
            int ans = 0;
            while(true){
                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;
                    }
                }
                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;
                    }
                }
            }
            cout << ans << endl;
            cout.flush();
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 11 ms 13452 KB Execution killed with signal 11
2 Runtime error 12 ms 13416 KB Execution killed with signal 11
3 Runtime error 11 ms 13412 KB Execution killed with signal 11
4 Correct 11 ms 6728 KB Output is correct
5 Correct 15 ms 6728 KB Output is correct
6 Runtime error 12 ms 13768 KB Execution killed with signal 11
7 Correct 32 ms 6856 KB Output is correct
8 Correct 30 ms 6856 KB Output is correct
9 Correct 84 ms 7496 KB Output is correct
10 Correct 73 ms 7444 KB Output is correct
11 Correct 152 ms 7752 KB Output is correct
12 Correct 106 ms 8624 KB Output is correct
13 Correct 209 ms 8004 KB Output is correct
14 Correct 306 ms 8704 KB Output is correct
15 Correct 302 ms 12992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6135 ms 13604 KB Output is correct
2 Execution timed out 8032 ms 11796 KB Time limit exceeded
3 Execution timed out 8061 ms 15924 KB Time limit exceeded
4 Runtime error 37 ms 17552 KB Execution killed with signal 11
5 Runtime error 44 ms 22524 KB Execution killed with signal 11
6 Runtime error 59 ms 20740 KB Execution killed with signal 11
7 Runtime error 212 ms 23196 KB Execution killed with signal 11
8 Runtime error 1062 ms 37784 KB Execution killed with signal 11
9 Runtime error 163 ms 36868 KB Execution killed with signal 11
10 Runtime error 190 ms 50964 KB Execution killed with signal 11
11 Runtime error 210 ms 36300 KB Execution killed with signal 11
12 Runtime error 223 ms 39212 KB Execution killed with signal 11
13 Runtime error 185 ms 40628 KB Execution killed with signal 11
14 Runtime error 234 ms 39836 KB Execution killed with signal 11
15 Execution timed out 8050 ms 25836 KB Time limit exceeded
16 Execution timed out 8048 ms 35552 KB Time limit exceeded
17 Runtime error 247 ms 66948 KB Execution killed with signal 11