답안 #198708

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198708 2020-01-27T12:39:05 Z SamAnd Regions (IOI09_regions) C++11
100 / 100
6686 ms 35068 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
const int N = 200005, R1 = 502;

int n, r, qq;
int p[N], u[N];
vector<int> a[N];

int ans[R1][R1];

int q[R1];
void dfs(int x)
{
    for (int i = 1; i <= r; ++i)
    {
        ans[i][u[x]] += q[i];
    }
    ++q[u[x]];
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        dfs(h);
    }
    --q[u[x]];
}

void solv1()
{
    dfs(1);
    while (qq--)
    {
        int e1, e2;
        scanf("%d%d", &e1, &e2);
        printf("%d\n", ans[e1][e2]);
        fflush(stdout);
    }
}

int tin[N], tout[N], ti;
void dfs1(int x)
{
    tin[x] = ++ti;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        dfs1(h);
    }
    tout[x] = ++ti;
}

vector<pair<int, bool> > v[N];
vector<int> vv[N];

void solv2()
{
    dfs1(1);
    for (int i = 1; i <= n; ++i)
    {
        v[u[i]].push_back(m_p(tin[i], true));
        v[u[i]].push_back(m_p(tout[i], false));
        vv[u[i]].push_back(tin[i]);
    }
    for (int i = 1; i <= r; ++i)
    {
        sort(v[i].begin(), v[i].end());
        sort(vv[i].begin(), vv[i].end());
    }
    while (qq--)
    {
        int e1, e2;
        scanf("%d%d", &e1, &e2);
        int ans = 0;
        int q = 0;
        int i = 0, j = 0;
        while (1)
        {
            if (i == v[e1].size() || j == vv[e2].size())
                break;
            if (v[e1][i].first < vv[e2][j])
            {
                if (v[e1][i].second)
                    ++q;
                else
                    --q;
                ++i;
            }
            else
            {
                ans += q;
                ++j;
            }
        }
        printf("%d\n", ans);
        fflush(stdout);
    }
}

int main()
{
    scanf("%d%d%d", &n, &r, &qq);
    scanf("%d", &u[1]);
    for (int i = 2; i <= n; ++i)
    {
        scanf("%d%d", &p[i], &u[i]);
        a[p[i]].push_back(i);
    }
    if (r <= 500)
        solv1();
    else
        solv2();
    return 0;
}

Compilation message

regions.cpp: In function 'void dfs(int)':
regions.cpp:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
regions.cpp: In function 'void dfs1(int)':
regions.cpp:44:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
regions.cpp: In function 'void solv2()':
regions.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (i == v[e1].size() || j == vv[e2].size())
                 ~~^~~~~~~~~~~~~~~
regions.cpp:78:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (i == v[e1].size() || j == vv[e2].size())
                                      ~~^~~~~~~~~~~~~~~~
regions.cpp: In function 'void solv1()':
regions.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &e1, &e2);
         ~~~~~^~~~~~~~~~~~~~~~~~
regions.cpp: In function 'void solv2()':
regions.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &e1, &e2);
         ~~~~~^~~~~~~~~~~~~~~~~~
regions.cpp: In function 'int main()':
regions.cpp:101:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &n, &r, &qq);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &u[1]);
     ~~~~~^~~~~~~~~~~~~
regions.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &p[i], &u[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14328 KB Output is correct
2 Correct 15 ms 14328 KB Output is correct
3 Correct 15 ms 14456 KB Output is correct
4 Correct 21 ms 14456 KB Output is correct
5 Correct 21 ms 14584 KB Output is correct
6 Correct 30 ms 14968 KB Output is correct
7 Correct 33 ms 14840 KB Output is correct
8 Correct 43 ms 14892 KB Output is correct
9 Correct 72 ms 15352 KB Output is correct
10 Correct 96 ms 15480 KB Output is correct
11 Correct 123 ms 15480 KB Output is correct
12 Correct 140 ms 16120 KB Output is correct
13 Correct 122 ms 15608 KB Output is correct
14 Correct 195 ms 15864 KB Output is correct
15 Correct 266 ms 17716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 758 ms 18092 KB Output is correct
2 Correct 927 ms 17016 KB Output is correct
3 Correct 1352 ms 19576 KB Output is correct
4 Correct 414 ms 16760 KB Output is correct
5 Correct 526 ms 18040 KB Output is correct
6 Correct 924 ms 18168 KB Output is correct
7 Correct 1271 ms 19616 KB Output is correct
8 Correct 1208 ms 24056 KB Output is correct
9 Correct 1981 ms 27128 KB Output is correct
10 Correct 2749 ms 30200 KB Output is correct
11 Correct 2734 ms 27768 KB Output is correct
12 Correct 3332 ms 28728 KB Output is correct
13 Correct 4918 ms 28580 KB Output is correct
14 Correct 6686 ms 28652 KB Output is correct
15 Correct 5070 ms 32072 KB Output is correct
16 Correct 5164 ms 35068 KB Output is correct
17 Correct 5415 ms 34740 KB Output is correct