Submission #959323

# Submission time Handle Problem Language Result Execution time Memory
959323 2024-04-08T01:22:53 Z steveonalex Valley (BOI19_valley) C++17
23 / 100
225 ms 34724 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}

ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return 63 - __builtin_clzll(mask);}

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
// mt19937_64 rng(69);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}

template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }

template <class T>
    void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }

template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int INF = 1e18 + 69;

struct SegmentTree{
    int n;
    vector<ll> a, lazy;

    SegmentTree(int _n){
        n = _n;
        a.resize(n * 4 + 4);
        lazy.resize(n * 4 + 4);
    }

    void down(int id){
        #define ligma(i) {a[i] += lazy[id]; lazy[i] += lazy[id];}
        ligma(id * 2);
        ligma(id * 2 + 1);
        lazy[id] = 0;
    }

    void update(int u, int v, ll val, int l, int r, int id){
        if (u <= l && r <= v){
            a[id] += val;
            lazy[id] += val;
            return;
        }
        if (lazy[id]) down(id);
        int mid = (l + r) >> 1;
        if (u <= mid) update(u, v, val, l, mid, id * 2);
        if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
        a[id] = min(a[id * 2], a[id * 2 + 1]);
    }

    void update(int u, int v, ll val){
        if (u > v) return;
        update(u, v, val, 1, n, 1);
    }

    ll get(int u, int v, int l, int r, int id){
        if (u <= l && r <= v) return a[id];
        if (lazy[id]) down(id);
        int mid = (l + r) >> 1;
        if (v <= mid) return get(u, v, l, mid, id * 2);
        if (u > mid) return get(u, v, mid + 1, r, id * 2 + 1);
        return min(get(u, v, l, mid, id * 2), get(u, v, mid + 1, r, id * 2 + 1));
    }

    ll get(int u, int v){
        if (u > v) return INF;
        return get(u, v, 1, n, 1);
    }
};

const int N = 1e5 + 69;

int n, s, q, e;
vector<pair<int, int>> graph[N];
pair<int, int> edge[N];
int h[N], l[N], r[N];
ll sum[N];
bitset<N> shop;
int dfs_cnt = 0;

void dfs(int u, int p){
    l[u] = r[u] = ++dfs_cnt;
    for(pair<int, int> v: graph[u]) if (v.first != p){
        h[v.first] = h[u] + 1;
        sum[v.first] = sum[u] + v.second;
        dfs(v.first, u);
        maximize(r[u], r[v.first]);
    }
}

vector<pair<int, int>> queries[N];
ll ans[N];

void go(int u, int p, SegmentTree &st){
    for(pair<int, int> i: queries[u]){
        int x = edge[i.first].first, y = edge[i.first].second;
        if (h[x] < h[y]) swap(x, y);
        if (l[x] <= l[u] && r[u] <= r[x]){ // x is ancestor of u
            if (l[x] <= l[e] && r[e] <= r[x]){
                ans[i.second] = -2;
            }
            else if (st.get(l[x], r[x]) < INF){
                ans[i.second] = st.get(l[x], r[x]);
            }
            else ans[i.second] = -1;
        }
        else{ // x is not ancestor of u
            if (l[x] > l[e] || r[e] > r[x]){
                ans[i.second] = -2;
            }
            else if (min(st.get(1, l[x] - 1), st.get(r[x] + 1, n)) < INF){
                ans[i.second] = min(st.get(1, l[x] - 1), st.get(r[x] + 1, n));
            }
            else ans[i.second] = -1;
        }
    }
    for(pair<int, int> v: graph[u]) if (v.first != p){
        #define bede(l, r, k) st.update(l, r, k);
        bede(1, n, v.second);
        bede(l[v.first], r[v.first], -2 * v.second);
        go(v.first, u, st);
        bede(1, n, -v.second);
        bede(l[v.first], r[v.first], 2 * v.second);
    }
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> s >> q >> e;
    for(int i = 1; i<n; ++i){
        int u, v, w; cin >> u >> v >> w;
        graph[u].push_back({v, w});
        graph[v].push_back({u, w});
        edge[i] = {u, v};
    }

    dfs(1, 1);

    for(int i = 0; i<s; ++i){
        int x; cin >> x;
        shop[x] = true;
    } 

    for(int i = 0; i<q; ++i){
        int j, r; cin >> j >> r;
        queries[r].push_back({j, i});
    }

    SegmentTree st(n);
    for(int i = 1; i<=n; ++i){
        st.update(l[i], l[i], sum[i]);
        if (!shop[i]) st.update(l[i], l[i], INF);
    }

    go(1, 1, st);

    for(int i = 0; i<q; ++i){
        if (ans[i] == -2) cout << "escaped\n";
        else if (ans[i] == -1) cout << "oo\n";
        else cout << ans[i] << "\n";
    }


    return 0;
}

Compilation message

valley.cpp:47:22: warning: overflow in conversion from 'double' to 'int' changes value from '1.0000000000000001e+18' to '2147483647' [-Woverflow]
   47 | const int INF = 1e18 + 69;
      |                 ~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8792 KB Output is correct
2 Incorrect 4 ms 8796 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8792 KB Output is correct
2 Incorrect 4 ms 8796 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 155 ms 25428 KB Output is correct
2 Correct 158 ms 24972 KB Output is correct
3 Correct 163 ms 24948 KB Output is correct
4 Correct 180 ms 28420 KB Output is correct
5 Correct 149 ms 27220 KB Output is correct
6 Correct 225 ms 34724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8792 KB Output is correct
2 Incorrect 4 ms 8796 KB Output isn't correct
3 Halted 0 ms 0 KB -