답안 #1082378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082378 2024-08-31T08:51:06 Z vjudge1 Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
48 ms 26208 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 MIN_HIGH(u, v)          (depth[u] < depth[v] ? (u) : (v))
#define LOG                     20
#define MAX                     50005
int numNode, numQuery;
struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}

    int other(int x){
        return (x ^ u ^ v);
    }
} edge[MAX];
vector<int> G[MAX];

namespace DistanceOnTree{
    int tin[MAX], timeDfs = 0, rev[MAX];

    int cnt = 0, st[MAX << 1][20], pos[MAX << 1], d[MAX], depth[MAX];
    void pre_dfs(int u, int p = -1){
        tin[u] = ++timeDfs; rev[timeDfs] = u;

        st[++cnt][0] = u; pos[u] = cnt;

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

    void build(void){
        pre_dfs(1);
        for (int i = 1; MASK(i) <= cnt; ++i){
            for (int u = 1; u + MASK(i) - 1 <= cnt; ++u){
                st[u][i] = MIN_HIGH(st[u][i - 1], st[u + MASK(i - 1)][i - 1]);
            }
        }
    }

    int lca(int u, int v){
        int l = pos[u], r = pos[v];
        if(l > r) swap(l, r);
        int k = 31 - __builtin_clz(r - l + 1);
        return MIN_HIGH(st[l][k], st[r - MASK(k) + 1][k]);
    }


    set<int> mySet;
    int sum = 0;
    int dis(int u, int v){
        return d[u] + d[v] - 2 * d[lca(u, v)];
    }

    void ins(int x){
        x = tin[x];
        if(mySet.empty()){
            mySet.insert(x);
            return;
        }
        if(mySet.size() == 1){
            sum += 2 * dis(rev[*mySet.begin()], rev[x]);
            mySet.insert(x);
            return;
        }
        auto it = mySet.lower_bound(x);
        int l = -1, r = -1;

        if(it == mySet.begin() || it == mySet.end()){
            l = rev[*mySet.begin()];
            r = rev[*prev(mySet.end())];
        }
        else{
            l = rev[*prev(it)];
            r = rev[*it];
        }
        assert(l != -1); assert(r != -1);
        mySet.insert(x);
        sum += dis(l, rev[x]);
        sum += dis(r, rev[x]);
        sum -= dis(l, r);
    }

    void eras(int x){
        x = tin[x];
        mySet.erase(x);
        if(mySet.size() == 0){
            sum = 0;
            return;
        }
        if(mySet.size() == 1){
            sum -= 2 * dis(rev[x], rev[*mySet.begin()]);
            return;
        }

        int l = -1, r = -1;

        auto it = mySet.lower_bound(x);
        if(it == mySet.begin() || it == mySet.end()){
            l = rev[*mySet.begin()];
            r = rev[*prev(mySet.end())];
        }
        else{
            l = rev[*prev(it)];
            r = rev[*it];
        }
        sum += dis(l, r);
        sum -= dis(l, rev[x]);
        sum -= dis(r, rev[x]);
    }
    int ans(void){
        return sum / 2;
    }
}
#define T           DistanceOnTree

void process(void){
    cin >> numNode;
    for (int i = 1; i < numNode; ++i){
        cin >> edge[i].u >> edge[i].v >> edge[i].w;
        ++edge[i].u, ++edge[i].v;
        G[edge[i].u].emplace_back(i);
        G[edge[i].v].emplace_back(i);
    }
    T :: build();


    cin >> numQuery;
    for (int i = 1; i <= numQuery; ++i){
        int node[5];
        REP(j, 5) {
            cin >> node[j];
            ++node[j];
            T :: ins(node[j]);
        }
        cout << T :: ans() << '\n';
        REP(j, 5){
            T :: eras(node[j]);
        }
    }
}
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 1 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 24648 KB Output is correct
2 Correct 44 ms 26196 KB Output is correct
3 Correct 43 ms 26192 KB Output is correct
4 Correct 42 ms 26208 KB Output is correct
5 Correct 41 ms 26196 KB Output is correct
6 Correct 43 ms 26192 KB Output is correct
7 Correct 42 ms 26132 KB Output is correct
8 Correct 43 ms 26196 KB Output is correct
9 Correct 46 ms 26136 KB Output is correct
10 Correct 46 ms 26196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 22864 KB Output is correct
2 Correct 32 ms 25176 KB Output is correct
3 Correct 31 ms 25180 KB Output is correct
4 Correct 30 ms 25172 KB Output is correct
5 Correct 32 ms 25180 KB Output is correct
6 Correct 30 ms 25172 KB Output is correct
7 Correct 30 ms 25412 KB Output is correct
8 Correct 30 ms 25176 KB Output is correct
9 Correct 31 ms 25180 KB Output is correct
10 Correct 30 ms 25332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 37 ms 24648 KB Output is correct
3 Correct 44 ms 26196 KB Output is correct
4 Correct 43 ms 26192 KB Output is correct
5 Correct 42 ms 26208 KB Output is correct
6 Correct 41 ms 26196 KB Output is correct
7 Correct 43 ms 26192 KB Output is correct
8 Correct 42 ms 26132 KB Output is correct
9 Correct 43 ms 26196 KB Output is correct
10 Correct 46 ms 26136 KB Output is correct
11 Correct 46 ms 26196 KB Output is correct
12 Correct 26 ms 22864 KB Output is correct
13 Correct 32 ms 25176 KB Output is correct
14 Correct 31 ms 25180 KB Output is correct
15 Correct 30 ms 25172 KB Output is correct
16 Correct 32 ms 25180 KB Output is correct
17 Correct 30 ms 25172 KB Output is correct
18 Correct 30 ms 25412 KB Output is correct
19 Correct 30 ms 25176 KB Output is correct
20 Correct 31 ms 25180 KB Output is correct
21 Correct 30 ms 25332 KB Output is correct
22 Correct 37 ms 24656 KB Output is correct
23 Correct 39 ms 23332 KB Output is correct
24 Correct 48 ms 25596 KB Output is correct
25 Correct 42 ms 25632 KB Output is correct
26 Correct 40 ms 25688 KB Output is correct
27 Correct 46 ms 25684 KB Output is correct
28 Correct 47 ms 25680 KB Output is correct
29 Correct 45 ms 25576 KB Output is correct
30 Correct 46 ms 25596 KB Output is correct
31 Correct 46 ms 25680 KB Output is correct