제출 #1091172

#제출 시각아이디문제언어결과실행 시간메모리
1091172underwaterkillerwhaleTourism (JOI23_tourism)C++17
36 / 100
284 ms27796 KiB
#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



*/

컴파일 시 표준 에러 (stderr) 메시지

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...