답안 #1071090

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1071090 2024-08-23T04:22:28 Z vjudge1 Tourism (JOI23_tourism) C++17
100 / 100
4590 ms 36036 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

//#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX                     100005
#define BLOCK_SIZE              400
#define LOG                     20
#define MIN_HIGH(u, v)          (depth[u] < depth[v] ? (u) : (v))


int numNode, numQuery, numCity;
vector<int> G[MAX];
int A[MAX];

struct Queries{
    int l, r, id;
    Queries(){}
    Queries(int _l, int _r, int _id): l(_l), r(_r), id(_id){}

    bool operator < (const Queries& oth) const{
        if (l / BLOCK_SIZE != oth.l / BLOCK_SIZE) return l < oth.l;
        return r < oth.r;
    }
} Q[MAX];

int ans[MAX];

int tin[MAX], st[MAX << 1][LOG], depth[MAX], rev[MAX], pos[MAX << 1];
int timeDfs = 0, cnt = 0;

int lg[MAX << 1];
void pre_dfs(int u, int p = -1){
    st[++cnt][0] = u; pos[u] = cnt;

    tin[u] = ++timeDfs; rev[tin[u]] = u;

    for (int &v : G[u]) if(v ^ p){
        depth[v] = depth[u] + 1;
        pre_dfs(v, u);
        st[++cnt][0] = u;
    }
}

void build(void){
    pre_dfs(1);
    for (int k = 1; MASK(k) <= cnt; ++k){
        for (int i = 1; i + MASK(k) - 1 <= cnt; ++i){
            st[i][k] = MIN_HIGH(st[i][k - 1], st[i + MASK(k - 1)][k - 1]);
        }
    }
    for (int i = 2; i <= cnt; ++i) lg[i] = lg[i >> 1] + 1;
}
int lca(int u, int v){
    int l = pos[u], r = pos[v];
    if(l > r) swap(l, r);
    int k = lg[r - l + 1];
    return MIN_HIGH(st[l][k], st[r - MASK(k) + 1][k]);
}
int dis(int a, int b){
    return depth[a] + depth[b] - 2 * depth[lca(a, b)];
}

namespace DistanceOnTree{
    int sum = 0;
    int F[MAX * 2];
    int low_bit(int p){
        return p & (-p);
    }

    void upd(int p, int v){
        for (; p <= cnt; p += low_bit(p)){
            F[p] += v;
        }
    }

    int query(int p){
        int res = 0;
        for (; p > 0; p -= low_bit(p)) res += F[p];
        return res;
    }
    int lower_bound(int val){
        int res = 0;
        for (int i = lg[cnt]; i >= 0; --i){
            if ((res | MASK(i)) <= cnt && val > F[res | MASK(i)]){
                val -= F[res | MASK(i)];
                res |= MASK(i);
            }
        }
        return res + 1;
    }
    int size = 0;
    int get_order(int x){
        return query(x);
    }
    int find_last(void){
        return lower_bound(size);
    }
    int find_first(void){
        return lower_bound(1);
    }
    int find_by_order(int x){
        return lower_bound(x);
    }

    int next[MAX], prev[MAX];

    int res(void){
        return sum / 2 + 1;
    }
    void ins(int x){
        x = tin[x];
        if(size){
            int i = get_order(x);
            int prv = (i > 0 ? find_by_order(i) : find_last());
            int nxt = next[prv];

            sum -= dis(rev[prv], rev[nxt]);
            sum += dis(rev[prv], rev[x]);
            sum += dis(rev[nxt], rev[x]);
            next[x] = nxt; prev[x] = prv;
            next[prv] = x; prev[nxt] = x;
        }

        upd(x, 1);
        if(!size){
            prev[x] = next[x] = x;
        }
        ++size;
    }
    void eras(int x){
        x = tin[x];
        upd(x, -1);
        --size;
        if(size){
            sum += dis(rev[prev[x]], rev[next[x]]);
            sum -= dis(rev[prev[x]], rev[x]);
            sum -= dis(rev[next[x]], rev[x]);

            next[prev[x]] = next[x];
            prev[next[x]] = prev[x];
        }
    }
}
#define T       DistanceOnTree
int in[MAX];

void sub(int x){
    in[A[x]]--;
    if(!in[A[x]])
        T :: eras(A[x]);
}
void add(int x){
    if(!in[A[x]])
        T :: ins(A[x]);
    in[A[x]]++;
}
void process(void){
    cin >> numNode >> numCity >> numQuery;
    for (int i = 1; i < numNode; ++i){
        int u, v; cin >> u >> v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    for (int i = 1; i <= numCity; ++i) cin >> A[i];

    for (int i = 1; i <= numQuery; ++i){
        cin >> Q[i].l >> Q[i].r;
        Q[i].id = i;
    }
    sort(Q + 1, Q + numQuery + 1);
    build();
    int L = 1, R = 0;
    for (int i = 1; i <= numQuery; ++i){
        while (L < Q[i].l) sub(L++);
        while (L > Q[i].l) add(--L);
        while (R < Q[i].r) add(++R);
        while (R > Q[i].r) sub(R--);

        ans[Q[i].id] = T :: res();
    }

    for (int i = 1; i <= numQuery; ++i){
        cout << ans[i] << '\n';
    }
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8520 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 3 ms 8516 KB Output is correct
5 Correct 2 ms 8432 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 3 ms 8540 KB Output is correct
10 Correct 2 ms 8520 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 3 ms 8532 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 3 ms 8544 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 3 ms 8540 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 3 ms 8556 KB Output is correct
24 Correct 3 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8784 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8520 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 3 ms 8516 KB Output is correct
5 Correct 2 ms 8432 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 3 ms 8540 KB Output is correct
10 Correct 2 ms 8520 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 3 ms 8532 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 3 ms 8544 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 3 ms 8540 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 3 ms 8556 KB Output is correct
24 Correct 3 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8784 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 8 ms 8540 KB Output is correct
31 Correct 10 ms 8536 KB Output is correct
32 Correct 18 ms 8540 KB Output is correct
33 Correct 13 ms 8540 KB Output is correct
34 Correct 12 ms 8508 KB Output is correct
35 Correct 4 ms 8536 KB Output is correct
36 Correct 3 ms 8540 KB Output is correct
37 Correct 3 ms 8640 KB Output is correct
38 Correct 18 ms 8792 KB Output is correct
39 Correct 10 ms 8796 KB Output is correct
40 Correct 11 ms 8792 KB Output is correct
41 Correct 4 ms 8796 KB Output is correct
42 Correct 3 ms 8840 KB Output is correct
43 Correct 4 ms 8796 KB Output is correct
44 Correct 11 ms 8780 KB Output is correct
45 Correct 17 ms 8540 KB Output is correct
46 Correct 17 ms 8792 KB Output is correct
47 Correct 3 ms 8540 KB Output is correct
48 Correct 3 ms 8540 KB Output is correct
49 Correct 4 ms 8540 KB Output is correct
50 Correct 11 ms 8756 KB Output is correct
51 Correct 11 ms 8744 KB Output is correct
52 Correct 10 ms 8748 KB Output is correct
53 Correct 11 ms 8744 KB Output is correct
54 Correct 11 ms 8540 KB Output is correct
55 Correct 11 ms 8540 KB Output is correct
56 Correct 3 ms 8536 KB Output is correct
57 Correct 3 ms 8536 KB Output is correct
58 Correct 3 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2500 ms 25432 KB Output is correct
5 Correct 1802 ms 32848 KB Output is correct
6 Correct 2741 ms 34264 KB Output is correct
7 Correct 4278 ms 36032 KB Output is correct
8 Correct 4250 ms 35952 KB Output is correct
9 Correct 4374 ms 36032 KB Output is correct
10 Correct 4340 ms 36032 KB Output is correct
11 Correct 4298 ms 36036 KB Output is correct
12 Correct 237 ms 35856 KB Output is correct
13 Correct 272 ms 35636 KB Output is correct
14 Correct 271 ms 35784 KB Output is correct
15 Correct 56 ms 33364 KB Output is correct
16 Correct 67 ms 35916 KB Output is correct
17 Correct 59 ms 9560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 36 ms 21048 KB Output is correct
3 Correct 69 ms 21524 KB Output is correct
4 Correct 46 ms 23964 KB Output is correct
5 Correct 58 ms 28496 KB Output is correct
6 Correct 92 ms 28516 KB Output is correct
7 Correct 73 ms 28608 KB Output is correct
8 Correct 81 ms 28496 KB Output is correct
9 Correct 73 ms 28496 KB Output is correct
10 Correct 77 ms 28496 KB Output is correct
11 Correct 101 ms 28480 KB Output is correct
12 Correct 74 ms 28612 KB Output is correct
13 Correct 78 ms 28840 KB Output is correct
14 Correct 93 ms 28760 KB Output is correct
15 Correct 93 ms 29332 KB Output is correct
16 Correct 92 ms 28664 KB Output is correct
17 Correct 85 ms 28756 KB Output is correct
18 Correct 72 ms 28752 KB Output is correct
19 Correct 55 ms 28428 KB Output is correct
20 Correct 64 ms 28500 KB Output is correct
21 Correct 71 ms 28496 KB Output is correct
22 Correct 78 ms 28500 KB Output is correct
23 Correct 92 ms 28396 KB Output is correct
24 Correct 73 ms 28576 KB Output is correct
25 Correct 75 ms 28496 KB Output is correct
26 Correct 82 ms 28568 KB Output is correct
27 Correct 97 ms 28496 KB Output is correct
28 Correct 72 ms 28496 KB Output is correct
29 Correct 67 ms 28504 KB Output is correct
30 Correct 111 ms 28756 KB Output is correct
31 Correct 75 ms 28740 KB Output is correct
32 Correct 73 ms 28804 KB Output is correct
33 Correct 67 ms 29012 KB Output is correct
34 Correct 65 ms 29520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2850 ms 22264 KB Output is correct
5 Correct 2962 ms 22404 KB Output is correct
6 Correct 3938 ms 28548 KB Output is correct
7 Correct 4338 ms 29768 KB Output is correct
8 Correct 4319 ms 29776 KB Output is correct
9 Correct 4315 ms 29772 KB Output is correct
10 Correct 4418 ms 29776 KB Output is correct
11 Correct 4375 ms 29760 KB Output is correct
12 Correct 4393 ms 29780 KB Output is correct
13 Correct 4391 ms 29768 KB Output is correct
14 Correct 54 ms 9540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8520 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 3 ms 8516 KB Output is correct
5 Correct 2 ms 8432 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 3 ms 8540 KB Output is correct
10 Correct 2 ms 8520 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 3 ms 8532 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 3 ms 8544 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 3 ms 8540 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 3 ms 8556 KB Output is correct
24 Correct 3 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8784 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 8 ms 8540 KB Output is correct
31 Correct 10 ms 8536 KB Output is correct
32 Correct 18 ms 8540 KB Output is correct
33 Correct 13 ms 8540 KB Output is correct
34 Correct 12 ms 8508 KB Output is correct
35 Correct 4 ms 8536 KB Output is correct
36 Correct 3 ms 8540 KB Output is correct
37 Correct 3 ms 8640 KB Output is correct
38 Correct 18 ms 8792 KB Output is correct
39 Correct 10 ms 8796 KB Output is correct
40 Correct 11 ms 8792 KB Output is correct
41 Correct 4 ms 8796 KB Output is correct
42 Correct 3 ms 8840 KB Output is correct
43 Correct 4 ms 8796 KB Output is correct
44 Correct 11 ms 8780 KB Output is correct
45 Correct 17 ms 8540 KB Output is correct
46 Correct 17 ms 8792 KB Output is correct
47 Correct 3 ms 8540 KB Output is correct
48 Correct 3 ms 8540 KB Output is correct
49 Correct 4 ms 8540 KB Output is correct
50 Correct 11 ms 8756 KB Output is correct
51 Correct 11 ms 8744 KB Output is correct
52 Correct 10 ms 8748 KB Output is correct
53 Correct 11 ms 8744 KB Output is correct
54 Correct 11 ms 8540 KB Output is correct
55 Correct 11 ms 8540 KB Output is correct
56 Correct 3 ms 8536 KB Output is correct
57 Correct 3 ms 8536 KB Output is correct
58 Correct 3 ms 8540 KB Output is correct
59 Correct 2 ms 8536 KB Output is correct
60 Correct 2 ms 8536 KB Output is correct
61 Correct 3 ms 8540 KB Output is correct
62 Correct 2500 ms 25432 KB Output is correct
63 Correct 1802 ms 32848 KB Output is correct
64 Correct 2741 ms 34264 KB Output is correct
65 Correct 4278 ms 36032 KB Output is correct
66 Correct 4250 ms 35952 KB Output is correct
67 Correct 4374 ms 36032 KB Output is correct
68 Correct 4340 ms 36032 KB Output is correct
69 Correct 4298 ms 36036 KB Output is correct
70 Correct 237 ms 35856 KB Output is correct
71 Correct 272 ms 35636 KB Output is correct
72 Correct 271 ms 35784 KB Output is correct
73 Correct 56 ms 33364 KB Output is correct
74 Correct 67 ms 35916 KB Output is correct
75 Correct 59 ms 9560 KB Output is correct
76 Correct 2 ms 8540 KB Output is correct
77 Correct 36 ms 21048 KB Output is correct
78 Correct 69 ms 21524 KB Output is correct
79 Correct 46 ms 23964 KB Output is correct
80 Correct 58 ms 28496 KB Output is correct
81 Correct 92 ms 28516 KB Output is correct
82 Correct 73 ms 28608 KB Output is correct
83 Correct 81 ms 28496 KB Output is correct
84 Correct 73 ms 28496 KB Output is correct
85 Correct 77 ms 28496 KB Output is correct
86 Correct 101 ms 28480 KB Output is correct
87 Correct 74 ms 28612 KB Output is correct
88 Correct 78 ms 28840 KB Output is correct
89 Correct 93 ms 28760 KB Output is correct
90 Correct 93 ms 29332 KB Output is correct
91 Correct 92 ms 28664 KB Output is correct
92 Correct 85 ms 28756 KB Output is correct
93 Correct 72 ms 28752 KB Output is correct
94 Correct 55 ms 28428 KB Output is correct
95 Correct 64 ms 28500 KB Output is correct
96 Correct 71 ms 28496 KB Output is correct
97 Correct 78 ms 28500 KB Output is correct
98 Correct 92 ms 28396 KB Output is correct
99 Correct 73 ms 28576 KB Output is correct
100 Correct 75 ms 28496 KB Output is correct
101 Correct 82 ms 28568 KB Output is correct
102 Correct 97 ms 28496 KB Output is correct
103 Correct 72 ms 28496 KB Output is correct
104 Correct 67 ms 28504 KB Output is correct
105 Correct 111 ms 28756 KB Output is correct
106 Correct 75 ms 28740 KB Output is correct
107 Correct 73 ms 28804 KB Output is correct
108 Correct 67 ms 29012 KB Output is correct
109 Correct 65 ms 29520 KB Output is correct
110 Correct 2 ms 8540 KB Output is correct
111 Correct 2 ms 8540 KB Output is correct
112 Correct 3 ms 8540 KB Output is correct
113 Correct 2850 ms 22264 KB Output is correct
114 Correct 2962 ms 22404 KB Output is correct
115 Correct 3938 ms 28548 KB Output is correct
116 Correct 4338 ms 29768 KB Output is correct
117 Correct 4319 ms 29776 KB Output is correct
118 Correct 4315 ms 29772 KB Output is correct
119 Correct 4418 ms 29776 KB Output is correct
120 Correct 4375 ms 29760 KB Output is correct
121 Correct 4393 ms 29780 KB Output is correct
122 Correct 4391 ms 29768 KB Output is correct
123 Correct 54 ms 9540 KB Output is correct
124 Correct 3901 ms 29620 KB Output is correct
125 Correct 2313 ms 28952 KB Output is correct
126 Correct 4578 ms 29836 KB Output is correct
127 Correct 4590 ms 29888 KB Output is correct
128 Correct 4530 ms 29888 KB Output is correct
129 Correct 4316 ms 29892 KB Output is correct
130 Correct 4425 ms 29888 KB Output is correct
131 Correct 4410 ms 35092 KB Output is correct
132 Correct 4409 ms 35944 KB Output is correct
133 Correct 4424 ms 32916 KB Output is correct
134 Correct 4377 ms 29936 KB Output is correct
135 Correct 4344 ms 29896 KB Output is correct
136 Correct 4375 ms 29888 KB Output is correct
137 Correct 4253 ms 30180 KB Output is correct
138 Correct 4202 ms 28412 KB Output is correct
139 Correct 4347 ms 28388 KB Output is correct
140 Correct 4288 ms 28364 KB Output is correct
141 Correct 4201 ms 28392 KB Output is correct
142 Correct 4262 ms 28360 KB Output is correct
143 Correct 46 ms 26196 KB Output is correct
144 Correct 66 ms 27728 KB Output is correct