Submission #1091172

# Submission time Handle Problem Language Result Execution time Memory
1091172 2024-09-20T02:26:10 Z underwaterkillerwhale Tourism (JOI23_tourism) C++17
36 / 100
284 ms 27796 KB
#include <bits/stdc++.h>
#define ll              long long
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)
#define iter(id, v)     for(auto id : v)
#define fs              first
#define se              second
#define MP              make_pair
#define pb              push_back
#define bit(msk, i)     ((msk >> i) & 1)
#define SZ(v)           (ll)v.size()
#define ALL(v)          v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 1e5 + 7;
const int Mod = 1e9 + 2022; ///loonf mod sai
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 320;

struct Data {
    int L, R, val;
    bool operator < (const Data &other) const {
        return L < other.L || (L == other.L && R < other.R) || (L == other.L && R == other.R && val < other.val);
    }
};

int n, m, Q;
int C[N];
vector<int> ke[N];
vector<pii> qr[N];

set<Data> S[N];

int high[N], pa[N], szC[N];
int Head[N], ein[N];

int ptrL = 1, ptrR = 0;

struct fenwick_Tree {
    int m;
    int fen[N];

    void init (int n) {
        m = n;
    }

    void update (int pos, int val) {
        for (; pos <= m; pos += pos & -pos) fen[pos] += val;
    }
    int get (int pos) {
        int res = 0;
        for (; pos > 0; pos -= pos & -pos) res += fen[pos];
        return res;
    }
}BIT;

void pdfs (int u, int p) {
    high[u] = high[p] + 1;
    szC[u] = 1;
    pa[u] = p;
    iter (&v, ke[u]) {
        if (v != p) {
            pdfs(v, u);
            szC[u] += szC[v];
        }
    }
}

void hld (int u, int p) {
//    cout << u<<" "<<p <<"\n";
    static int time_dfs = 0;
    Head[u] = p;
    ein[u] = ++time_dfs;
    int mxV = -1;
    iter (&v, ke[u]) if (v != pa[u] && (mxV == -1 || szC[v] > szC[mxV])) mxV = v;
    if (mxV != -1) hld(mxV, p);
    iter (&v, ke[u]) {
        if (v == pa[u] || v == mxV) continue;
        hld(v, v);
    }
}

void update (int u, int v, int val) {
    int uu = u, vv = v;
    for (; Head[u] != Head[v]; v = pa[Head[v]]) {
        if (Head[u] > Head[v]) swap(u, v);
        set<Data> &cur = S[Head[v]];
        while (1) {
            auto it = cur.begin();
            if (it == cur.end() || it->R > high[v]) break;
            BIT.update (it->val, -(it->R - it->L + 1));
            cur.erase(it);
        }
        auto it = cur.begin();
        if (it != cur.end()) {
            Data curV = {high[v] + 1, it->R, it->val};
            BIT.update(curV.val, it->L - curV.L);
            cur.erase(it);
            cur.insert(curV);
        }
        cur.insert({high[Head[v]], high[v], val});
        BIT.update(val, high[v] - high[Head[v]] + 1);
    }
    if (high[u] > high[v]) swap(u, v);
    int L = high[u], R = high[v];
    set<Data> &cur = S[Head[u]];
    auto itL = cur.upper_bound({L, INF, INF});
    if (itL != cur.begin()) {
        --itL;
        if (itL->L < L && itL->R >= L) {
            Data curV = {itL->L, L - 1, itL->val}, curVR = {-1, -1, -1};
            BIT.update(itL->val, L - 1 - itL->R);
            if (itL->R > R) {
                curVR = {R + 1, itL->R, itL->val};
            }
            cur.erase(itL);
            cur.insert(curV);
            if (curVR.L != -1) {
                BIT.update(curVR.val, curVR.R - curVR.L + 1);
                cur.insert(curVR);
            }
        }
    }
    auto itR = cur.upper_bound({R, INF, INF});
    if (itR != cur.begin()) {
//        if (uu == 5 && vv == 7) {
//            cout <<R<<":"<<itR->L<<","<<itR->R<<" hih\n";
//            cout << curV.L<<","<<curV.R<<","<<curV.val<<" jhih\n";
//        }
        --itR;
        if (itR->R > R && itR->L <= R) {
            Data curV = {R + 1, itR->R, itR->val};
            BIT.update(itR->val, itR->L - (R + 1));
            cur.erase(itR);
            cur.insert(curV);
        }
    }
    while (1) {
        auto it = cur.lower_bound({L, -1, -1});
        if (it == cur.end() || it->L > R) break;
        BIT.update(it->val, -(it->R - it->L + 1));
        cur.erase(it);
    }
    BIT.update(val, R - L + 1);
    cur.insert({L, R, val});
//    cout << uu<<"->"<<vv<<"\n";
//    for (int id : {1, 3, 5, 7}) {
//        cout <<id<<": ";
//        iter (&id2, S[id]) cout <<id2.L<<","<<id2.R<<","<<id2.val<<" ";
//        cout<<"\n";
//    }
}

void solution() {
    cin >> n >> m >> Q;
    rep (i, 1, n - 1) {
        int u, v;
        cin >> u >> v;
        ke[u].pb(v);
        ke[v].pb(u);
    }
    rep (i, 1, m) cin >> C[i];
    pdfs(1, 0);
    hld(1, 1);
    rep (i, 1, Q) {
        int L, R;
        cin >> L >> R;
        qr[R].pb({L, i});
    }
    BIT.init(m);
    vector<int> Ans(Q + 1, 0);
    rep (R, 1, m) {
        if (R > 1) update (C[R - 1], C[R], R - 1);
        iter (&id, qr[R]) {
            if (id.fs == R) Ans[id.se] = 1;
            else Ans[id.se] = BIT.get(m) - BIT.get(id.fs - 1);
        }
    }
    rep (i, 1, Q) cout << Ans[i] <<"\n";

}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug +8
chu y break hay return se lam hong logic
xet transition cua i va i + 1
construct ket qua
chu y truong hop : KHONG CO GI

ko làm được

hướng 1: đổi hướng làm
hướng 2: đưa ra nhận xét

tim mo hinh bai toan sau khi doc de

trung ten bien trong ham gan nhat co the dan den sai

10 7 3
6 5
3 6
9 3
8 3
7 8
7 1
2 5
7 10
8 4
9 4 10 1 10 7 6
4 4
1 3
6 7



*/

Compilation message

tourism.cpp: In function 'void update(int, int, int)':
tourism.cpp:91:9: warning: unused variable 'uu' [-Wunused-variable]
   91 |     int uu = u, vv = v;
      |         ^~
tourism.cpp:91:17: warning: unused variable 'vv' [-Wunused-variable]
   91 |     int uu = u, vv = v;
      |                 ^~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9708 KB Output is correct
4 Correct 5 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 5 ms 9868 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 4 ms 9680 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 5 ms 9864 KB Output is correct
17 Correct 4 ms 9824 KB Output is correct
18 Correct 4 ms 9824 KB Output is correct
19 Correct 4 ms 9828 KB Output is correct
20 Correct 4 ms 9820 KB Output is correct
21 Correct 5 ms 10008 KB Output is correct
22 Correct 5 ms 9824 KB Output is correct
23 Correct 5 ms 9828 KB Output is correct
24 Correct 5 ms 9824 KB Output is correct
25 Correct 4 ms 9868 KB Output is correct
26 Correct 5 ms 9884 KB Output is correct
27 Correct 4 ms 9820 KB Output is correct
28 Correct 5 ms 9820 KB Output is correct
29 Correct 7 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9708 KB Output is correct
4 Correct 5 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 5 ms 9868 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 4 ms 9680 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 5 ms 9864 KB Output is correct
17 Correct 4 ms 9824 KB Output is correct
18 Correct 4 ms 9824 KB Output is correct
19 Correct 4 ms 9828 KB Output is correct
20 Correct 4 ms 9820 KB Output is correct
21 Correct 5 ms 10008 KB Output is correct
22 Correct 5 ms 9824 KB Output is correct
23 Correct 5 ms 9828 KB Output is correct
24 Correct 5 ms 9824 KB Output is correct
25 Correct 4 ms 9868 KB Output is correct
26 Correct 5 ms 9884 KB Output is correct
27 Correct 4 ms 9820 KB Output is correct
28 Correct 5 ms 9820 KB Output is correct
29 Correct 7 ms 9820 KB Output is correct
30 Correct 7 ms 10076 KB Output is correct
31 Correct 6 ms 9972 KB Output is correct
32 Correct 7 ms 10124 KB Output is correct
33 Correct 6 ms 10072 KB Output is correct
34 Correct 7 ms 10096 KB Output is correct
35 Correct 7 ms 10076 KB Output is correct
36 Correct 7 ms 10076 KB Output is correct
37 Correct 8 ms 10132 KB Output is correct
38 Correct 6 ms 10076 KB Output is correct
39 Correct 8 ms 10188 KB Output is correct
40 Correct 6 ms 9964 KB Output is correct
41 Correct 6 ms 10076 KB Output is correct
42 Correct 5 ms 10076 KB Output is correct
43 Correct 6 ms 10328 KB Output is correct
44 Correct 6 ms 10076 KB Output is correct
45 Incorrect 7 ms 10076 KB Output isn't correct
46 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Correct 4 ms 9732 KB Output is correct
3 Correct 5 ms 9820 KB Output is correct
4 Correct 69 ms 21148 KB Output is correct
5 Correct 78 ms 23124 KB Output is correct
6 Correct 73 ms 24660 KB Output is correct
7 Correct 89 ms 27752 KB Output is correct
8 Correct 108 ms 27728 KB Output is correct
9 Correct 89 ms 27764 KB Output is correct
10 Correct 88 ms 27796 KB Output is correct
11 Correct 103 ms 27784 KB Output is correct
12 Correct 90 ms 27268 KB Output is correct
13 Correct 97 ms 27224 KB Output is correct
14 Correct 85 ms 27216 KB Output is correct
15 Correct 52 ms 23904 KB Output is correct
16 Correct 93 ms 27476 KB Output is correct
17 Correct 36 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 89 ms 15940 KB Output is correct
3 Correct 147 ms 16980 KB Output is correct
4 Correct 120 ms 17236 KB Output is correct
5 Correct 223 ms 20644 KB Output is correct
6 Correct 183 ms 20816 KB Output is correct
7 Correct 183 ms 20820 KB Output is correct
8 Correct 178 ms 20820 KB Output is correct
9 Correct 181 ms 20764 KB Output is correct
10 Correct 201 ms 20820 KB Output is correct
11 Correct 174 ms 20820 KB Output is correct
12 Correct 184 ms 20920 KB Output is correct
13 Incorrect 183 ms 21180 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 6 ms 9820 KB Output is correct
4 Correct 223 ms 20788 KB Output is correct
5 Correct 234 ms 20816 KB Output is correct
6 Correct 252 ms 23632 KB Output is correct
7 Correct 284 ms 25456 KB Output is correct
8 Correct 258 ms 25264 KB Output is correct
9 Correct 255 ms 25164 KB Output is correct
10 Correct 267 ms 25364 KB Output is correct
11 Correct 272 ms 25168 KB Output is correct
12 Correct 273 ms 25168 KB Output is correct
13 Correct 282 ms 25168 KB Output is correct
14 Correct 41 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9708 KB Output is correct
4 Correct 5 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 5 ms 9868 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 4 ms 9680 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 5 ms 9864 KB Output is correct
17 Correct 4 ms 9824 KB Output is correct
18 Correct 4 ms 9824 KB Output is correct
19 Correct 4 ms 9828 KB Output is correct
20 Correct 4 ms 9820 KB Output is correct
21 Correct 5 ms 10008 KB Output is correct
22 Correct 5 ms 9824 KB Output is correct
23 Correct 5 ms 9828 KB Output is correct
24 Correct 5 ms 9824 KB Output is correct
25 Correct 4 ms 9868 KB Output is correct
26 Correct 5 ms 9884 KB Output is correct
27 Correct 4 ms 9820 KB Output is correct
28 Correct 5 ms 9820 KB Output is correct
29 Correct 7 ms 9820 KB Output is correct
30 Correct 7 ms 10076 KB Output is correct
31 Correct 6 ms 9972 KB Output is correct
32 Correct 7 ms 10124 KB Output is correct
33 Correct 6 ms 10072 KB Output is correct
34 Correct 7 ms 10096 KB Output is correct
35 Correct 7 ms 10076 KB Output is correct
36 Correct 7 ms 10076 KB Output is correct
37 Correct 8 ms 10132 KB Output is correct
38 Correct 6 ms 10076 KB Output is correct
39 Correct 8 ms 10188 KB Output is correct
40 Correct 6 ms 9964 KB Output is correct
41 Correct 6 ms 10076 KB Output is correct
42 Correct 5 ms 10076 KB Output is correct
43 Correct 6 ms 10328 KB Output is correct
44 Correct 6 ms 10076 KB Output is correct
45 Incorrect 7 ms 10076 KB Output isn't correct
46 Halted 0 ms 0 KB -