답안 #523545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
523545 2022-02-07T18:19:28 Z Vladth11 Regions (IOI09_regions) C++14
0 / 100
10 ms 14408 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 <ll, ll> pii;
typedef pair <long double, pii> muchie;

const ll NMAX = 200001;
const ll VMAX = 21;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 447;
const ll base = 31;
const ll nr_of_bits = 21;
const ll MMAX = NMAX / BLOCK + 2;

vector <ll> v[NMAX], vertices[NMAX];
vector <pii> lista[NMAX];
ll stamp;
ll n, R, Q;
ll sol[MMAX][25001];
ll precalc[25001][MMAX];
ll region[NMAX];
ll bigR[NMAX];
ll smallR[NMAX];
ll bigIdx[NMAX];
ll smallIdx[NMAX];
ll cati[NMAX];
ll stiva[NMAX];
ll vf;
ll countAbove[MMAX];
ll timestamp;
pii timp[NMAX];
ll preorder[NMAX + 1];

void DFS(ll node, ll p) {
    stamp++;
    timp[node].first = ++timestamp;
    lista[region[node]].push_back({node, stamp});
    if(bigIdx[region[node]] != 0) {
        countAbove[bigIdx[region[node]]]++;
    }
    for(ll 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() {
    ifstream cin(".in");
    ofstream cout(".out");
    ll i;
    cin >> n >> R >> Q;
    cin >> region[1];
    vertices[region[1]].push_back(1);
    cati[region[1]]++;
    for(i = 2; i <= n; i++) {
        ll p;
        cin >> p >> region[i];
        v[p].push_back(i);
        v[i].push_back(p);
        vertices[region[i]].push_back(i);
        cati[region[i]]++;
    }
    ll 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(ll i = 1; i <= R; i++) {
        if(bigIdx[i] == 0) continue;
        for(auto x : vertices[i]) {
            preorder[timp[x].first]++; /// BRUH
        }
        for(ll j = 1; j <= n; j++) {
            preorder[j] += preorder[j - 1];
        }
        for(ll 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(ll j = 1; j <= n; j++)
            preorder[j] = 0;
    }
    while(Q--) {
        ll a, b;
        cin >> a >> b;
        if(bigIdx[a] != 0) { /// Aia de sus mare
            cout << sol[bigIdx[a]][b];
        } else if(bigIdx[b] != 0) { /// Aia de jos mare si aia de sus mica
            cout << precalc[smallIdx[a]][bigIdx[b]];
        } else { /// Ambele mici
            ll i = 0;
            ll j = 0;
            ll deschise = 0;
            vf = 0;
            ll 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(vf && stiva[vf] == x.first) {
                        vf--;
                        deschise--;
                    } else {
                        stiva[++vf] = x.first;
                        deschise++;
                    }
                    i++;
                } else {
                    if(vf && stiva[vf] == y.first) {
                        vf--;
                    } else {
                        rez += deschise;
                        stiva[++vf] = y.first;
                    }
                    j++;
                }
            }
            cout << rez;
        }
        cout << endl;
    }
    return 0;
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:116:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |             while(i < lista[a].size() && j < lista[b].size()) {
      |                   ~~^~~~~~~~~~~~~~~~~
regions.cpp:116:44: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |             while(i < lista[a].size() && j < lista[b].size()) {
      |                                          ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
2 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
3 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
4 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
5 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
6 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
7 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
8 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
9 Incorrect 7 ms 14388 KB Unexpected end of file - int32 expected
10 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
11 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
12 Incorrect 9 ms 14408 KB Unexpected end of file - int32 expected
13 Incorrect 8 ms 14288 KB Unexpected end of file - int32 expected
14 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
15 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
2 Incorrect 10 ms 14408 KB Unexpected end of file - int32 expected
3 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
4 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
5 Incorrect 8 ms 14364 KB Unexpected end of file - int32 expected
6 Incorrect 8 ms 14308 KB Unexpected end of file - int32 expected
7 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
8 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
9 Incorrect 7 ms 14392 KB Unexpected end of file - int32 expected
10 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
11 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
12 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
13 Incorrect 8 ms 14408 KB Unexpected end of file - int32 expected
14 Incorrect 8 ms 14324 KB Unexpected end of file - int32 expected
15 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected
16 Incorrect 9 ms 14408 KB Unexpected end of file - int32 expected
17 Incorrect 7 ms 14408 KB Unexpected end of file - int32 expected