답안 #893771

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
893771 2023-12-27T11:26:20 Z lolismek Tourism (JOI23_tourism) C++14
18 / 100
791 ms 1048576 KB
#include <algorithm>
#include <iostream>
#include <climits>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <random>
#include <chrono>

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using ull = unsigned long long;
using ll = long long;

//#define int __int128
//#define int ll
#define pii pair <int, int>
#define all(a) (a).begin(), (a).end()
#define fr first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound

#define vt vector
#define FOR(a, b) for(int i = (a); ((a) <= (b) ? i <= (b) : i >= (b));((a) <= (b) ? i++ : i--))
#define sz(x) (int)(x).size() // ?

#define YES cout << "YES\n"
#define NO cout << "NO\n"

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int rangerng(int l, int r){
    return uniform_int_distribution<>(l, r)(rng);
}

////////////////////////////////////////////////////////////////////////////////////

const int NMAX = 1e5;

vt <int> adj[NMAX + 1];
int c[NMAX + 1];
vt <pii> quer[NMAX + 1];

int Ans[NMAX + 1];

int sz[NMAX + 1];
int par[NMAX + 1];
int hvChild[NMAX + 1];

int lvl[NMAX + 1];

void expl(int node, int parent){
    par[node] = parent;
    sz[node] = 1;

    lvl[node] = lvl[parent] + 1;

    int maxi = 0;
    for(int child : adj[node]){
        if(child != parent){
            expl(child, node);
            sz[node] += sz[child];

            if(sz[child] >= maxi){
                maxi = sz[child];
                hvChild[node] = child;
            }
        }
    }
}

int Id = 0;
int id[NMAX + 1];
int head[NMAX + 1];
int last[NMAX + 1];

int Time = 0;
int pos[NMAX + 1];

void hvFirst(int node, int parent){
    pos[node] = ++Time;
    
    if(hvChild[parent] != node){
        id[node] = ++Id;
        head[Id] = node;
    }else{
        id[node] = id[parent];
    }

    last[id[node]] = node;

    if(hvChild[node] != 0){
        hvFirst(hvChild[node], node);
    }

    for(int child : adj[node]){
        if(child != parent && child != hvChild[node]){
            hvFirst(child, node);
        }
    }
}

namespace AIB{
    int n;
    int aib[NMAX + 2];

    void init(int _n){
        n = _n + 1;
        FOR(1, n){
            aib[i] = 0;
        }
    }

    int lsb(int x){
        return  x & -x;
    }

    void update(int pos, int val){
        pos++;
        for(int i = pos; i <= n; i += lsb(i)){
            aib[i] += val;
        }
    }

    int query(int pos){
        int ans = 0;
        pos++;
        for(int i = pos; i >= 1; i -= lsb(i)){
            ans += aib[i];
        }
        return ans;
    }
}

set <pair <pii, int>> S;

void setInterval(int l, int r, int x){
    //cout << "++ " << l << ' ' << r << ' ' << x << endl;

    vt <pair <pii, int>> toErase;
    vt <pair <pii, int>> toInsert;
    toInsert.pb({{l, r}, x});

    for(auto it = S.lb({{l, 0}, 0}); it != S.end() && (*it).fr.fr <= r; it++){
        pair <pii, int> obj = (*it);
        toErase.pb(obj);
        if(obj.fr.sc > r){
            pair <pii, int> newObj = {{r + 1, obj.fr.sc}, obj.sc};
            toInsert.pb(newObj);
        }
    }

    auto it = S.lb({{l, 0}, 0});
    if(it != S.begin()){
        it--;
        pair <pii, int> obj = (*it);
        if(obj.fr.fr < l && obj.fr.sc >= l){
            toErase.pb(obj);
            toInsert.pb({{obj.fr.fr, l - 1}, obj.sc});
            if(obj.fr.sc > r){
                toInsert.pb({{r + 1, obj.fr.sc}, obj.sc});
            }
        }
    }

    //cout << "* " << AIB::query(3) << endl;
    for(auto x : toErase){
        S.erase(x);
        AIB::update(x.sc, -(x.fr.sc - x.fr.fr + 1));
        //cout << "erase: " << x.fr.fr << ' ' << x.fr.sc << ' ' << x.sc << endl;
    }

    for(auto x : toInsert){
        S.insert(x);
        AIB::update(x.sc, +(x.fr.sc - x.fr.fr + 1));
        //cout << "insert: " << x.fr.fr << ' ' << x.fr.sc << ' ' << x.sc << endl;
    }

    //cout << "* " << AIB::query(0) << endl;
    // for(auto it : S){
    //     cout << it.fr.fr << ' ' << it.fr.sc << ' ' << it.sc << '\n';
    // }

}

void update(int a, int b, int x){
    //cout << "-- " << a << ' ' << b << ' ' << x << endl;
    while(id[a] != id[b]){
        if(lvl[head[id[a]]] > lvl[head[id[b]]]){
            setInterval(pos[head[id[a]]], pos[a], x);
            a = par[head[id[a]]];
        }else{
            setInterval(pos[head[id[b]]], pos[b], x);
            b = par[head[id[b]]];
        }
    }
    setInterval(min(pos[a], pos[b]), max(pos[a], pos[b]), x);
}

void solve(){
    int n, m, q;
    cin >> n >> m >> q;

    FOR(1, n - 1){
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    FOR(1, m){
        cin >> c[i];
    }

    FOR(1, q){
        int x, y;
        cin >> x >> y;
        quer[y].pb({x, i});
    }

    expl(1, 0);
    hvFirst(1, 0);

    AIB::init(m);

    FOR(1, Id){
        S.insert({{pos[head[i]], pos[last[i]]}, 0});
        AIB::update(0, pos[last[i]] - pos[head[i]] + 1);
    }

    //cout << "** " << AIB::query(m) << endl;

    FOR(1, m){
        //cout << ">> " << endl;
        if(i > 1){
            update(c[i - 1], c[i], i - 1);
        }
        update(c[i], c[i], i);

        for(pii x : quer[i]){
            //cout << "& " << x.fr - 1 << endl;
            Ans[x.sc] += AIB::query(m) - AIB::query(x.fr - 1);
        }
    }

    for(int i = 1; i <= q; i++){
        cout << Ans[i] << '\n';
    }
}       

signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int T;
    //cin >> T;

    T = 1;

    while(T--){
        solve();
    }

    return 0;
}

/*
7 3 1
1 2
1 3
2 4
2 5
3 6
3 7
2 3 6
1 3
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 3 ms 7212 KB Output is correct
5 Correct 2 ms 7260 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
8 Correct 2 ms 7252 KB Output is correct
9 Correct 2 ms 7260 KB Output is correct
10 Correct 2 ms 7308 KB Output is correct
11 Correct 2 ms 7260 KB Output is correct
12 Correct 3 ms 7260 KB Output is correct
13 Correct 2 ms 7260 KB Output is correct
14 Correct 2 ms 7260 KB Output is correct
15 Correct 2 ms 7256 KB Output is correct
16 Correct 2 ms 7260 KB Output is correct
17 Correct 2 ms 7260 KB Output is correct
18 Correct 2 ms 7260 KB Output is correct
19 Correct 2 ms 7260 KB Output is correct
20 Correct 2 ms 7304 KB Output is correct
21 Correct 2 ms 7260 KB Output is correct
22 Correct 2 ms 7260 KB Output is correct
23 Correct 2 ms 7260 KB Output is correct
24 Correct 2 ms 7320 KB Output is correct
25 Correct 3 ms 7256 KB Output is correct
26 Correct 3 ms 7260 KB Output is correct
27 Runtime error 714 ms 1048576 KB Execution killed with signal 9
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 3 ms 7212 KB Output is correct
5 Correct 2 ms 7260 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
8 Correct 2 ms 7252 KB Output is correct
9 Correct 2 ms 7260 KB Output is correct
10 Correct 2 ms 7308 KB Output is correct
11 Correct 2 ms 7260 KB Output is correct
12 Correct 3 ms 7260 KB Output is correct
13 Correct 2 ms 7260 KB Output is correct
14 Correct 2 ms 7260 KB Output is correct
15 Correct 2 ms 7256 KB Output is correct
16 Correct 2 ms 7260 KB Output is correct
17 Correct 2 ms 7260 KB Output is correct
18 Correct 2 ms 7260 KB Output is correct
19 Correct 2 ms 7260 KB Output is correct
20 Correct 2 ms 7304 KB Output is correct
21 Correct 2 ms 7260 KB Output is correct
22 Correct 2 ms 7260 KB Output is correct
23 Correct 2 ms 7260 KB Output is correct
24 Correct 2 ms 7320 KB Output is correct
25 Correct 3 ms 7256 KB Output is correct
26 Correct 3 ms 7260 KB Output is correct
27 Runtime error 714 ms 1048576 KB Execution killed with signal 9
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7256 KB Output is correct
2 Runtime error 592 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 353 ms 13416 KB Output is correct
3 Correct 534 ms 14812 KB Output is correct
4 Correct 411 ms 14996 KB Output is correct
5 Correct 697 ms 18584 KB Output is correct
6 Correct 669 ms 18672 KB Output is correct
7 Correct 720 ms 18572 KB Output is correct
8 Correct 739 ms 18772 KB Output is correct
9 Correct 699 ms 18512 KB Output is correct
10 Correct 691 ms 18512 KB Output is correct
11 Correct 683 ms 18568 KB Output is correct
12 Correct 740 ms 18560 KB Output is correct
13 Correct 716 ms 18912 KB Output is correct
14 Correct 703 ms 19988 KB Output is correct
15 Correct 791 ms 22884 KB Output is correct
16 Correct 696 ms 19408 KB Output is correct
17 Correct 685 ms 18840 KB Output is correct
18 Correct 681 ms 18932 KB Output is correct
19 Correct 415 ms 14096 KB Output is correct
20 Correct 404 ms 13964 KB Output is correct
21 Correct 429 ms 14164 KB Output is correct
22 Correct 455 ms 14000 KB Output is correct
23 Correct 413 ms 13776 KB Output is correct
24 Correct 417 ms 14176 KB Output is correct
25 Correct 455 ms 13908 KB Output is correct
26 Correct 457 ms 14160 KB Output is correct
27 Correct 409 ms 13908 KB Output is correct
28 Correct 420 ms 13776 KB Output is correct
29 Correct 422 ms 14228 KB Output is correct
30 Correct 428 ms 14116 KB Output is correct
31 Correct 425 ms 14420 KB Output is correct
32 Correct 421 ms 14860 KB Output is correct
33 Correct 469 ms 16228 KB Output is correct
34 Correct 441 ms 18536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Runtime error 585 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 3 ms 7212 KB Output is correct
5 Correct 2 ms 7260 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
8 Correct 2 ms 7252 KB Output is correct
9 Correct 2 ms 7260 KB Output is correct
10 Correct 2 ms 7308 KB Output is correct
11 Correct 2 ms 7260 KB Output is correct
12 Correct 3 ms 7260 KB Output is correct
13 Correct 2 ms 7260 KB Output is correct
14 Correct 2 ms 7260 KB Output is correct
15 Correct 2 ms 7256 KB Output is correct
16 Correct 2 ms 7260 KB Output is correct
17 Correct 2 ms 7260 KB Output is correct
18 Correct 2 ms 7260 KB Output is correct
19 Correct 2 ms 7260 KB Output is correct
20 Correct 2 ms 7304 KB Output is correct
21 Correct 2 ms 7260 KB Output is correct
22 Correct 2 ms 7260 KB Output is correct
23 Correct 2 ms 7260 KB Output is correct
24 Correct 2 ms 7320 KB Output is correct
25 Correct 3 ms 7256 KB Output is correct
26 Correct 3 ms 7260 KB Output is correct
27 Runtime error 714 ms 1048576 KB Execution killed with signal 9
28 Halted 0 ms 0 KB -