답안 #523525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
523525 2022-02-07T18:04:41 Z Vladth11 Regions (IOI09_regions) C++14
30 / 100
1755 ms 95032 KB
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
 
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <long double, pii> muchie;
 
const int NMAX = 500001;
const int VMAX = 21;
const int INF = (1LL << 60);
const int MOD = 1000000007;
const int BLOCK = 447;
const int base = 31;
const int nr_of_bits = 21;
const int MMAX = 500;
 
vector <int> v[NMAX], vertices[NMAX];
vector <pii> lista[NMAX];
int stamp;
int n, R, Q;
ll sol[MMAX][30001];
ll precalc[30001][MMAX];
int region[NMAX];
int bigR[NMAX];
int smallR[NMAX];
int bigIdx[NMAX];
int smallIdx[NMAX];
int cati[NMAX];
int stiva[NMAX];
int vf;
int countAbove[MMAX];
int timestamp;
pii timp[NMAX];
int preorder[NMAX + 1];
 
void DFS(int node, int p) {
    stamp++;
    timp[node].first = ++timestamp;
    lista[region[node]].push_back({node, stamp});
    if(bigIdx[region[node]] != 0) {
        countAbove[bigIdx[region[node]]]++;
    }
    for(int i = 1; i <= R; i++) {
        sol[i][region[node]] += 1LL * countAbove[i];
    }
    for(auto x : v[node]) {
        if(x == p)
            continue;
        DFS(x, node);
    }
    if(bigIdx[region[node]] != 0) {
        countAbove[bigIdx[region[node]]]--;
    }
    stamp++;
    timp[node].second = timestamp;
    lista[region[node]].push_back({node, stamp});
}
 
int main() {
    int i;
    cin >> n >> R >> Q;
    cin >> region[1];
    vertices[region[1]].push_back(1);
    cati[region[1]]++;
    for(i = 2; i <= n; i++) {
        int p;
        cin >> p >> region[i];
        v[p].push_back(i);
        v[i].push_back(p);
        vertices[region[i]].push_back(i);
        cati[region[i]]++;
    }
    int cc = 0, sc = 0;
    for(i = 1; i <= R; i++) {
        if(cati[i] >= BLOCK) {
            bigR[++cc] = i;
            bigIdx[i] = cc;
        } else {
            smallR[++sc] = i;
            smallIdx[i] = sc;
        }
    }
    DFS(1, 0);
    for(int i = 1; i <= R; i++) {
        if(bigIdx[i] == 0) continue;
        for(auto x : vertices[i]) {
            preorder[timp[x].first]++; /// BRUH
        }
        for(int j = 1; j <= n; j++) {
            preorder[j] += preorder[j - 1];
        }
        for(int j = 1; j <= sc; j++) {
            for(auto x : vertices[smallR[j]])
                precalc[j][bigIdx[i]] += 1LL * (preorder[timp[x].second] - preorder[timp[x].first - 1]);
        }
        for(int j = 1; j <= n; j++)
            preorder[j] = 0;
    }
    while(Q--) {
        int a, b;
        cin >> a >> b;
        if(bigIdx[a] != 0) { /// Aia de sus mare
            cout << sol[bigIdx[a]][b] << "\n";
        } else if(bigIdx[b] != 0) { /// Aia de jos mare si aia de sus mica
            cout << precalc[smallIdx[a]][bigIdx[b]] << "\n";
        } else { /// Ambele mici
            int i = 0;
            int j = 0;
            int deschise = 0;
            vf = 0;
            int rez = 0;
            while(i < lista[a].size() && j < lista[b].size()) {
                pii x = lista[a][i];
                pii y = lista[b][j];
                if(x.second < y.second) {
                    if(stiva[vf] == x.first) {
                        vf--;
                        deschise--;
                    } else {
                        stiva[++vf] = x.first;
                        deschise++;
                    }
                    i++;
                } else {
                    if(stiva[vf] == y.first) {
                        vf--;
                    } else {
                        rez += deschise;
                        stiva[++vf] = y.first;
                    }
                    j++;
                }
            }
            cout << rez;
        }
        cout << endl;
    }
    return 0;
}

Compilation message

regions.cpp:12:22: warning: overflow in conversion from 'long long int' to 'int' changes value from '1152921504606846976' to '0' [-Woverflow]
   12 | const int INF = (1LL << 60);
      |                 ~~~~~^~~~~~
regions.cpp: In function 'int main()':
regions.cpp:114:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |             while(i < lista[a].size() && j < lista[b].size()) {
      |                   ~~^~~~~~~~~~~~~~~~~
regions.cpp:114:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |             while(i < lista[a].size() && j < lista[b].size()) {
      |                                          ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 35596 KB Output is correct
2 Correct 24 ms 35528 KB Output is correct
3 Correct 21 ms 35656 KB Output is correct
4 Correct 23 ms 35676 KB Output is correct
5 Correct 28 ms 35732 KB Output is correct
6 Correct 39 ms 37196 KB Output is correct
7 Correct 46 ms 36600 KB Output is correct
8 Correct 49 ms 36904 KB Output is correct
9 Correct 84 ms 38388 KB Output is correct
10 Correct 98 ms 39484 KB Output is correct
11 Correct 105 ms 38472 KB Output is correct
12 Correct 175 ms 41008 KB Output is correct
13 Correct 183 ms 39888 KB Output is correct
14 Correct 163 ms 39188 KB Output is correct
15 Correct 268 ms 43332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 917 ms 44164 KB Output is correct
2 Correct 1202 ms 43932 KB Output is correct
3 Correct 1755 ms 47472 KB Output is correct
4 Runtime error 78 ms 79136 KB Execution killed with signal 11
5 Runtime error 82 ms 79688 KB Execution killed with signal 11
6 Runtime error 125 ms 81128 KB Execution killed with signal 11
7 Runtime error 104 ms 82828 KB Execution killed with signal 11
8 Runtime error 125 ms 85420 KB Execution killed with signal 11
9 Runtime error 176 ms 90020 KB Execution killed with signal 11
10 Runtime error 179 ms 92312 KB Execution killed with signal 11
11 Runtime error 253 ms 95032 KB Execution killed with signal 11
12 Runtime error 185 ms 92832 KB Execution killed with signal 11
13 Runtime error 185 ms 92864 KB Execution killed with signal 11
14 Runtime error 198 ms 94204 KB Execution killed with signal 11
15 Runtime error 210 ms 94424 KB Execution killed with signal 11
16 Runtime error 219 ms 94172 KB Execution killed with signal 11
17 Runtime error 223 ms 94172 KB Execution killed with signal 11