Submission #763590

# Submission time Handle Problem Language Result Execution time Memory
763590 2023-06-22T13:35:40 Z anhduc2701 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 45712 KB
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI acos(-1.0)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x, i) (1 & ((x) >> (i)))
#define MASK(x) (1LL << (x))
#define task "tnc"
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18;
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
const int mo = 998244353;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using pii = pair<pair<ll, ll>, ll>;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int tin[maxn];
int tout[maxn];
int eu[maxn];
int n, m, q;
int ti = 0;
vector<int> G[maxn];
int h[maxn];
void dfs(int u, int pa)
{
    tin[u] = ++ti;
    eu[ti] = u;
    for (auto v : G[u])
    {
        if (v == pa)
            continue;
        h[v] = h[u] + 1;
        dfs(v, u);
        eu[++ti] = u;
    }
}
pair<int, int> rmq[maxn][19];
void init()
{
    for (int i = 1; i <= ti; i++)
    {
        rmq[i][0] = {tin[eu[i]], eu[i]};
    }
    for (int j = 1; (1 << j) <= ti; j++)
    {
        for (int i = 1; i <= ti; i++)
        {
            rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
        }
    }
}
const int blk = 330;
struct que
{
    int l, r, id;
    que() {}
    que(int _l, int _r, int _id)
    {
        l = _l;
        r = _r;
        id = _id;
    }
    bool operator<(const que &x)
    {
        if (l / blk == x.l / blk)
            return r < x.r;
        return l < x.l;
    }
};
set<int> d;
int cnt[maxn];
int sum = 0;
int LCA(int u, int v)
{
    if (tin[u] > tin[v])
        swap(u, v);
    int k = 31 - __builtin_clz(tin[v] - tin[u] + 1);
    return min(rmq[tin[u]][k], rmq[tin[v] - (1 << k) + 1][k]).se;
}
int calc(int u, int v)
{
    return h[v] - h[LCA(u, v)];
}
void add(int u)
{
    cnt[u]++;
    if (cnt[u] == 1)
    {
        auto k = d.lower_bound(tin[u]);

        if (k != d.end())
        {
            sum += calc(u, eu[(*k)]);
            if (k != d.begin())
            {
                int x = (*k);
                k--;
                sum -= calc(eu[(*k)], eu[x]);
                k++;
            }
        }
        if (k != d.begin())
        {
            k--;
            sum += calc(eu[(*k)], u);
        }
        d.insert(tin[u]);
    }
}

void era(int u)
{
    cnt[u]--;
    if (cnt[u] == 0)
    {
        auto k = d.lower_bound(tin[u]);
        k++;
        int nxt = 0;
        int bef = 0;
        if (k != d.end())
        {
            nxt = (*k);
            sum -= calc(u, eu[nxt]);
        }
        k--;

        if (k != d.begin())
        {
            k--;
            bef = (*k);
            sum -= calc(eu[bef], u);
        }
        if (bef != 0 && nxt != 0)
        {
            sum += calc(eu[bef], eu[nxt]);
        }
        d.erase(tin[u]);
    }
}
vector<que> p;
int ans[maxn];
int c[maxn];
signed main()
{
    cin.tie(0), cout.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        G[u].pb(v);
        G[v].pb(u);
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> c[i];
    }
    dfs(1, -1);
    init();
    for (int i = 1; i <= q; i++)
    {
        int l, r;
        cin >> l >> r;
        p.pb(que(l, r, i));
    }
    sort(p.begin(), p.end());
    int l = 1;
    int r = 0;
    //  cout << calc(7, 4) << "\n";
    for (auto &v : p)
    {

        while (l < v.l)
            era(c[l++]);
        while (l > v.l)
            add(c[--l]);
        while (r > v.r)
            era(c[r--]);
        while (r < v.r)
            add(c[++r]);
        int st = (*d.begin());
        int en = (*d.rbegin());
        ans[v.id] = calc(eu[en], eu[st]) + sum;
    }
    for (int i = 1; i <= q; i++)
    {
        cout << ans[i] + 1 << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7396 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 6 ms 7380 KB Output is correct
7 Correct 5 ms 7496 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7504 KB Output is correct
10 Correct 6 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 6 ms 7528 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 6 ms 7544 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 7 ms 7536 KB Output is correct
20 Correct 6 ms 7456 KB Output is correct
21 Correct 6 ms 7508 KB Output is correct
22 Correct 5 ms 7508 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 6 ms 7532 KB Output is correct
26 Correct 5 ms 7508 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7508 KB Output is correct
29 Correct 4 ms 7456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7396 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 6 ms 7380 KB Output is correct
7 Correct 5 ms 7496 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7504 KB Output is correct
10 Correct 6 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 6 ms 7528 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 6 ms 7544 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 7 ms 7536 KB Output is correct
20 Correct 6 ms 7456 KB Output is correct
21 Correct 6 ms 7508 KB Output is correct
22 Correct 5 ms 7508 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 6 ms 7532 KB Output is correct
26 Correct 5 ms 7508 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7508 KB Output is correct
29 Correct 4 ms 7456 KB Output is correct
30 Correct 20 ms 7892 KB Output is correct
31 Correct 22 ms 8000 KB Output is correct
32 Correct 28 ms 8212 KB Output is correct
33 Correct 34 ms 8232 KB Output is correct
34 Correct 27 ms 8148 KB Output is correct
35 Correct 6 ms 8148 KB Output is correct
36 Correct 6 ms 8148 KB Output is correct
37 Correct 6 ms 8108 KB Output is correct
38 Correct 33 ms 8280 KB Output is correct
39 Correct 29 ms 8324 KB Output is correct
40 Correct 27 ms 8276 KB Output is correct
41 Correct 6 ms 8276 KB Output is correct
42 Correct 6 ms 8276 KB Output is correct
43 Correct 6 ms 8276 KB Output is correct
44 Correct 29 ms 8264 KB Output is correct
45 Correct 28 ms 8248 KB Output is correct
46 Correct 28 ms 8260 KB Output is correct
47 Correct 6 ms 8148 KB Output is correct
48 Correct 6 ms 8148 KB Output is correct
49 Correct 6 ms 8276 KB Output is correct
50 Correct 26 ms 8220 KB Output is correct
51 Correct 24 ms 8200 KB Output is correct
52 Correct 24 ms 8228 KB Output is correct
53 Correct 25 ms 8192 KB Output is correct
54 Correct 26 ms 8220 KB Output is correct
55 Correct 26 ms 8228 KB Output is correct
56 Correct 5 ms 7448 KB Output is correct
57 Correct 5 ms 8148 KB Output is correct
58 Correct 7 ms 8156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Execution timed out 5065 ms 34600 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 62 ms 26796 KB Output is correct
3 Correct 88 ms 28516 KB Output is correct
4 Correct 81 ms 31360 KB Output is correct
5 Correct 124 ms 45676 KB Output is correct
6 Correct 151 ms 44108 KB Output is correct
7 Correct 163 ms 43088 KB Output is correct
8 Correct 158 ms 42900 KB Output is correct
9 Correct 143 ms 42796 KB Output is correct
10 Correct 156 ms 42720 KB Output is correct
11 Correct 140 ms 42776 KB Output is correct
12 Correct 149 ms 42832 KB Output is correct
13 Correct 125 ms 43036 KB Output is correct
14 Correct 118 ms 43352 KB Output is correct
15 Correct 117 ms 44620 KB Output is correct
16 Correct 139 ms 44592 KB Output is correct
17 Correct 143 ms 44576 KB Output is correct
18 Correct 138 ms 44628 KB Output is correct
19 Correct 109 ms 45712 KB Output is correct
20 Correct 131 ms 44616 KB Output is correct
21 Correct 149 ms 43616 KB Output is correct
22 Correct 165 ms 43232 KB Output is correct
23 Correct 144 ms 42912 KB Output is correct
24 Correct 143 ms 42860 KB Output is correct
25 Correct 143 ms 42728 KB Output is correct
26 Correct 140 ms 42816 KB Output is correct
27 Correct 132 ms 42712 KB Output is correct
28 Correct 137 ms 42740 KB Output is correct
29 Correct 126 ms 42728 KB Output is correct
30 Correct 123 ms 42980 KB Output is correct
31 Correct 119 ms 43136 KB Output is correct
32 Correct 113 ms 43244 KB Output is correct
33 Correct 121 ms 43756 KB Output is correct
34 Correct 113 ms 44648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7424 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Execution timed out 5064 ms 30748 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7396 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 6 ms 7380 KB Output is correct
7 Correct 5 ms 7496 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7504 KB Output is correct
10 Correct 6 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 4 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 4 ms 7508 KB Output is correct
15 Correct 6 ms 7528 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 6 ms 7544 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 7 ms 7536 KB Output is correct
20 Correct 6 ms 7456 KB Output is correct
21 Correct 6 ms 7508 KB Output is correct
22 Correct 5 ms 7508 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 6 ms 7532 KB Output is correct
26 Correct 5 ms 7508 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7508 KB Output is correct
29 Correct 4 ms 7456 KB Output is correct
30 Correct 20 ms 7892 KB Output is correct
31 Correct 22 ms 8000 KB Output is correct
32 Correct 28 ms 8212 KB Output is correct
33 Correct 34 ms 8232 KB Output is correct
34 Correct 27 ms 8148 KB Output is correct
35 Correct 6 ms 8148 KB Output is correct
36 Correct 6 ms 8148 KB Output is correct
37 Correct 6 ms 8108 KB Output is correct
38 Correct 33 ms 8280 KB Output is correct
39 Correct 29 ms 8324 KB Output is correct
40 Correct 27 ms 8276 KB Output is correct
41 Correct 6 ms 8276 KB Output is correct
42 Correct 6 ms 8276 KB Output is correct
43 Correct 6 ms 8276 KB Output is correct
44 Correct 29 ms 8264 KB Output is correct
45 Correct 28 ms 8248 KB Output is correct
46 Correct 28 ms 8260 KB Output is correct
47 Correct 6 ms 8148 KB Output is correct
48 Correct 6 ms 8148 KB Output is correct
49 Correct 6 ms 8276 KB Output is correct
50 Correct 26 ms 8220 KB Output is correct
51 Correct 24 ms 8200 KB Output is correct
52 Correct 24 ms 8228 KB Output is correct
53 Correct 25 ms 8192 KB Output is correct
54 Correct 26 ms 8220 KB Output is correct
55 Correct 26 ms 8228 KB Output is correct
56 Correct 5 ms 7448 KB Output is correct
57 Correct 5 ms 8148 KB Output is correct
58 Correct 7 ms 8156 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 3 ms 7380 KB Output is correct
61 Correct 4 ms 7380 KB Output is correct
62 Execution timed out 5065 ms 34600 KB Time limit exceeded
63 Halted 0 ms 0 KB -