답안 #1082383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082383 2024-08-31T09:02:26 Z vjudge1 Roadside Advertisements (NOI17_roadsideadverts) C++17
47 / 100
62 ms 26972 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                     50005
#define LOG                     20
#define MIN_HIGH(u, v)          (depth[u] < depth[v] ? (u) : (v))
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], st[MAX << 1][LOG], depth[MAX], rev[MAX], pos[MAX << 1];
    int d[MAX];
    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 &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 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 d[a] + d[b] - 2 * d[lca(a, b)];
    }
    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;
    }
    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
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 :: res() << '\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 48 ms 25688 KB Output is correct
2 Correct 49 ms 26964 KB Output is correct
3 Correct 52 ms 26960 KB Output is correct
4 Correct 62 ms 26972 KB Output is correct
5 Incorrect 53 ms 26964 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 24172 KB Output is correct
2 Correct 33 ms 26440 KB Output is correct
3 Correct 39 ms 26452 KB Output is correct
4 Correct 31 ms 26448 KB Output is correct
5 Correct 34 ms 26376 KB Output is correct
6 Correct 45 ms 26348 KB Output is correct
7 Correct 38 ms 26460 KB Output is correct
8 Correct 32 ms 26536 KB Output is correct
9 Correct 32 ms 26460 KB Output is correct
10 Correct 34 ms 26436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 48 ms 25688 KB Output is correct
3 Correct 49 ms 26964 KB Output is correct
4 Correct 52 ms 26960 KB Output is correct
5 Correct 62 ms 26972 KB Output is correct
6 Incorrect 53 ms 26964 KB Output isn't correct
7 Halted 0 ms 0 KB -