Submission #763589

# Submission time Handle Problem Language Result Execution time Memory
763589 2023-06-22T13:33:08 Z anhduc2701 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 45728 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 = 350;
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 7340 KB Output is correct
2 Correct 4 ms 7400 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7448 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7508 KB Output is correct
10 Correct 6 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 3 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 7508 KB Output is correct
16 Correct 6 ms 7508 KB Output is correct
17 Correct 7 ms 7544 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 6 ms 7508 KB Output is correct
20 Correct 6 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7532 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 6 ms 7508 KB Output is correct
25 Correct 5 ms 7508 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 4 ms 7332 KB Output is correct
28 Correct 3 ms 7508 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7340 KB Output is correct
2 Correct 4 ms 7400 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7448 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7508 KB Output is correct
10 Correct 6 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 3 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 7508 KB Output is correct
16 Correct 6 ms 7508 KB Output is correct
17 Correct 7 ms 7544 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 6 ms 7508 KB Output is correct
20 Correct 6 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7532 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 6 ms 7508 KB Output is correct
25 Correct 5 ms 7508 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 4 ms 7332 KB Output is correct
28 Correct 3 ms 7508 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 20 ms 7992 KB Output is correct
31 Correct 24 ms 7980 KB Output is correct
32 Correct 29 ms 8212 KB Output is correct
33 Correct 29 ms 8208 KB Output is correct
34 Correct 28 ms 8212 KB Output is correct
35 Correct 6 ms 8148 KB Output is correct
36 Correct 6 ms 8212 KB Output is correct
37 Correct 6 ms 8148 KB Output is correct
38 Correct 33 ms 8312 KB Output is correct
39 Correct 28 ms 8324 KB Output is correct
40 Correct 28 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 30 ms 8236 KB Output is correct
45 Correct 32 ms 8248 KB Output is correct
46 Correct 29 ms 8280 KB Output is correct
47 Correct 5 ms 8148 KB Output is correct
48 Correct 6 ms 8232 KB Output is correct
49 Correct 6 ms 8148 KB Output is correct
50 Correct 26 ms 8148 KB Output is correct
51 Correct 25 ms 8220 KB Output is correct
52 Correct 25 ms 8220 KB Output is correct
53 Correct 26 ms 8228 KB Output is correct
54 Correct 32 ms 8240 KB Output is correct
55 Correct 26 ms 8192 KB Output is correct
56 Correct 4 ms 7380 KB Output is correct
57 Correct 4 ms 8148 KB Output is correct
58 Correct 5 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Execution timed out 5071 ms 34592 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 69 ms 26776 KB Output is correct
3 Correct 93 ms 28604 KB Output is correct
4 Correct 83 ms 31272 KB Output is correct
5 Correct 121 ms 45728 KB Output is correct
6 Correct 179 ms 44080 KB Output is correct
7 Correct 201 ms 43196 KB Output is correct
8 Correct 158 ms 42904 KB Output is correct
9 Correct 155 ms 42708 KB Output is correct
10 Correct 141 ms 42712 KB Output is correct
11 Correct 146 ms 42828 KB Output is correct
12 Correct 131 ms 42824 KB Output is correct
13 Correct 128 ms 43040 KB Output is correct
14 Correct 141 ms 43336 KB Output is correct
15 Correct 121 ms 44644 KB Output is correct
16 Correct 145 ms 44576 KB Output is correct
17 Correct 146 ms 44612 KB Output is correct
18 Correct 139 ms 44576 KB Output is correct
19 Correct 110 ms 45720 KB Output is correct
20 Correct 136 ms 44612 KB Output is correct
21 Correct 145 ms 43596 KB Output is correct
22 Correct 142 ms 43220 KB Output is correct
23 Correct 143 ms 42916 KB Output is correct
24 Correct 141 ms 42840 KB Output is correct
25 Correct 139 ms 42756 KB Output is correct
26 Correct 178 ms 42848 KB Output is correct
27 Correct 139 ms 42768 KB Output is correct
28 Correct 136 ms 42768 KB Output is correct
29 Correct 128 ms 42828 KB Output is correct
30 Correct 124 ms 42972 KB Output is correct
31 Correct 117 ms 43080 KB Output is correct
32 Correct 118 ms 43316 KB Output is correct
33 Correct 112 ms 43724 KB Output is correct
34 Correct 121 ms 44636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Execution timed out 5081 ms 30772 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7340 KB Output is correct
2 Correct 4 ms 7400 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7448 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7508 KB Output is correct
10 Correct 6 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 3 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 7508 KB Output is correct
16 Correct 6 ms 7508 KB Output is correct
17 Correct 7 ms 7544 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 6 ms 7508 KB Output is correct
20 Correct 6 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 5 ms 7532 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 6 ms 7508 KB Output is correct
25 Correct 5 ms 7508 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 4 ms 7332 KB Output is correct
28 Correct 3 ms 7508 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 20 ms 7992 KB Output is correct
31 Correct 24 ms 7980 KB Output is correct
32 Correct 29 ms 8212 KB Output is correct
33 Correct 29 ms 8208 KB Output is correct
34 Correct 28 ms 8212 KB Output is correct
35 Correct 6 ms 8148 KB Output is correct
36 Correct 6 ms 8212 KB Output is correct
37 Correct 6 ms 8148 KB Output is correct
38 Correct 33 ms 8312 KB Output is correct
39 Correct 28 ms 8324 KB Output is correct
40 Correct 28 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 30 ms 8236 KB Output is correct
45 Correct 32 ms 8248 KB Output is correct
46 Correct 29 ms 8280 KB Output is correct
47 Correct 5 ms 8148 KB Output is correct
48 Correct 6 ms 8232 KB Output is correct
49 Correct 6 ms 8148 KB Output is correct
50 Correct 26 ms 8148 KB Output is correct
51 Correct 25 ms 8220 KB Output is correct
52 Correct 25 ms 8220 KB Output is correct
53 Correct 26 ms 8228 KB Output is correct
54 Correct 32 ms 8240 KB Output is correct
55 Correct 26 ms 8192 KB Output is correct
56 Correct 4 ms 7380 KB Output is correct
57 Correct 4 ms 8148 KB Output is correct
58 Correct 5 ms 8148 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 4 ms 7380 KB Output is correct
61 Correct 5 ms 7380 KB Output is correct
62 Execution timed out 5071 ms 34592 KB Time limit exceeded
63 Halted 0 ms 0 KB -