답안 #870895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
870895 2023-11-09T12:17:34 Z aykhn Regions (IOI09_regions) C++17
30 / 100
3297 ms 131072 KB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define pii pair<int, int>
#define mpr make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()

const int MXN = 2e5 + 5;
const int B = 1;

int n, r, q;
int reg[MXN], p[MXN];
vector<int> idx[MXN];
vector<pii> v1[MXN];
vector<int> v2[MXN];
vector<int> adj[MXN];
int dp[MXN];
int in[MXN], out[MXN];
int tim = -1;
int cur = -1;
map<pii, int> mp;

void dfs(int a, int p)
{
    in[a] = ++tim;
    for (int v : adj[a])
    {
        if (v == p) continue;
        dfs(v, a);
    }
    out[a] = ++tim;
}

void dfs1(int a, int p, int seen)
{
    if (reg[a] != cur) mp[mpr(cur, reg[a])] += seen;
    for (int v : adj[a])
    {
        if (v == p) continue;
        dfs1(v, a, seen + (reg[a] == cur));
        dp[a] += dp[v];
    }
    dp[a] += (reg[a] == cur);
    if (cur != reg[a] && idx[reg[a]].size() < B) mp[mpr(reg[a], cur)] += dp[a];
}

int main()
{
    cin >> n >> r >> q;
    cin >> reg[1];
    idx[reg[1]].pb(1);
    for (int i = 2; i <= n; i++)
    {
        cin >> p[i] >> reg[i];
        adj[p[i]].pb(i);
        idx[reg[i]].pb(i);
    }
    dfs(1, 1);
    for (int i = 1; i <= r; i++)
    {
        for (int x : idx[i])
        {
            v1[i].pb(mpr(in[x], 1));
            v1[i].pb(mpr(out[x] + 1, -1));
            v2[i].pb(in[x]);
        }
        sort(all(v1[i]));
        sort(all(v2[i]));
    }
    for (int i = 1; i <= r; i++)
    {
        if (idx[i].size() < B) continue; 
        cur = i;
        dfs1(1, 1, 0);
    }
    while (q--)
    {
        int u, v;
        cin >> u >> v;
        if (mp.find(mpr(u, v)) != mp.end())
        {
            cout << mp[mpr(u, v)] << endl;
            continue;
        }
        int i = 0;
        int j = 0;
        int ans = 0;
        int rn = 0;
        while (i < v1[u].size() && j < v2[v].size())
        {
            if (v1[u][i].fi <= v2[v][j]) rn += v1[u][i++].se;
            else 
            {
                ans += rn;
                j++;
            }
        }
        mp[mpr(u, v)] = ans;
        cout << ans << endl;
    }
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:93:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         while (i < v1[u].size() && j < v2[v].size())
      |                ~~^~~~~~~~~~~~~~
regions.cpp:93:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         while (i < v1[u].size() && j < v2[v].size())
      |                                    ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21336 KB Output is correct
2 Correct 5 ms 21336 KB Output is correct
3 Correct 5 ms 21440 KB Output is correct
4 Correct 7 ms 21336 KB Output is correct
5 Correct 10 ms 21336 KB Output is correct
6 Correct 30 ms 25172 KB Output is correct
7 Correct 42 ms 22872 KB Output is correct
8 Correct 58 ms 23984 KB Output is correct
9 Correct 158 ms 28156 KB Output is correct
10 Correct 437 ms 32924 KB Output is correct
11 Correct 370 ms 26964 KB Output is correct
12 Correct 837 ms 36136 KB Output is correct
13 Correct 799 ms 30108 KB Output is correct
14 Correct 530 ms 26192 KB Output is correct
15 Correct 809 ms 32500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1360 ms 30708 KB Output is correct
2 Correct 1656 ms 29552 KB Output is correct
3 Correct 2240 ms 35652 KB Output is correct
4 Runtime error 1688 ms 131072 KB Execution killed with signal 9
5 Runtime error 2369 ms 131072 KB Execution killed with signal 9
6 Runtime error 1700 ms 131072 KB Execution killed with signal 9
7 Runtime error 1693 ms 131072 KB Execution killed with signal 9
8 Runtime error 1910 ms 131072 KB Execution killed with signal 9
9 Runtime error 2650 ms 131072 KB Execution killed with signal 9
10 Runtime error 1554 ms 131072 KB Execution killed with signal 9
11 Runtime error 2394 ms 131072 KB Execution killed with signal 9
12 Runtime error 3297 ms 131072 KB Execution killed with signal 9
13 Runtime error 2998 ms 131072 KB Execution killed with signal 9
14 Runtime error 2878 ms 131072 KB Execution killed with signal 9
15 Runtime error 1835 ms 131072 KB Execution killed with signal 9
16 Runtime error 1635 ms 131072 KB Execution killed with signal 9
17 Runtime error 2054 ms 131072 KB Execution killed with signal 9