제출 #1350063

#제출 시각아이디문제언어결과실행 시간메모리
1350063vahagngValley (BOI19_valley)C++20
32 / 100
271 ms49068 KiB
//----------vahagng----------//
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;

// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

void print(long long t) { cerr << t; }
void print(int t) { cerr << t; }
void print(string t) { cerr << t; }
void print(char t) { cerr << t; }
void print(double t) { cerr << t; }
void print(long double t) { cerr << t; }
void print(unsigned long long t) { cerr << t; }

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[], V n) { cerr << "["; for (int i = 0; i < n; i++) { cerr << v[i] << " "; } cerr << "]"; }
template <class T, class V> void print(pair <T, V> p) { cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}"; }
template <class T> void print(vector <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(unordered_set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(multiset <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void print(map <T, V> v) { cerr << "[ "; for (auto i : v) { print(i); cerr << " "; } cerr << "]"; }

#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ld long double
#define sz(v) v.size()
#define endl '\n'
#define stress_test freopen("out.txt", "r", stdin); freopen("in.txt", "w", stdout);

const ll inf = 8e18, mod = 1e9 + 7, mod2 = 998244353;

void SetIO(string str = "") {
    if (str != "") {
        freopen((str + ".in").c_str(), "r", stdin);
        freopen((str + ".out").c_str(), "w", stdout);
    }
    else {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }
}

void FastIO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());

const int N = 1e5 + 10, sq = 500, M = 2e5 + 10, Q = pow(3, 7);

int n, s, q, e, tin[N], tout[N], timer, par[N];
ll dist[N], id[N], ans[N], answ[N];
vector<pair<ll, ll>> adj[N], edges;
bool marked[N];

// answ[i] = 1 -> escaped
// answ[i] = 0 -> oo
// answ[i] = -1 -> xanut

void dfs(int node, int parent){
    tin[node] = ++timer;
    id[tin[node]] = node;
    par[node] = parent;
    for(auto [u, w] : adj[node]){
        if(u == parent) continue;
        dist[u] = dist[node] + w;
        dfs(u, node);
    }
    tout[node] = timer;
}

bool herna(int u, int v){
    return tin[v] >= tin[u] && tout[v] <= tout[u];
}

struct segtree{
    struct Node{
        __int128_t mn, lazy;
    };

    int size = 1;
    vector<Node> stree;

    void init(){
        while(size <= n) size <<= 1;
        stree.assign(4*size, {(__int128_t)50 * inf, 0});
        build(0, 0, size);
    }

    void build(int x, int lx, int rx){
        if(rx - lx == 1){
            if(1 <= lx && lx <= n && marked[lx]){
                stree[x].mn = dist[id[lx]];
            }
            return;
        }
        int m =(lx + rx) / 2;
        build(2*x+1, lx, m);
        build(2*x+2, m, rx);
        stree[x].mn = min(stree[2*x+1].mn, stree[2*x+2].mn);
    }

    void push(int x, int lx, int rx){
        if(rx - lx == 1) return;
        stree[2*x+1].lazy += stree[x].lazy;
        stree[2*x+2].lazy += stree[x].lazy;
        stree[2*x+1].mn += stree[x].lazy;
        stree[2*x+2].mn += stree[x].lazy;
        stree[x].lazy = 0;
    }

    void upd(int l, int r, ll v, int x, int lx, int rx){
        push(x, lx, rx);
        if(lx >= r || rx <= l) return;
        if(lx >= l && rx <= r){
            stree[x].mn += v;
            stree[x].lazy += v;
            return;
        }
        int m = (lx + rx) / 2;
        upd(l, r, v, 2*x+1, lx, m);
        upd(l, r, v, 2*x+2, m, rx);
        stree[x].mn = min(stree[2*x+1].mn, stree[2*x+2].mn);
    }

    __int128_t qry(int l, int r, int x, int lx, int rx){
        push(x, lx, rx);
        if(lx >= r || rx <= l) return (__int128_t)50 * inf;
        if(lx >= l && rx <= r) return stree[x].mn;
        int m = (lx + rx) / 2;
        return min(qry(l, r, 2*x+1, lx, m), qry(l, r, 2*x+2, m, rx));
    }

    void upd(int l, int r, ll v){
        upd(l, r, v, 0, 0, size);
    }

    __int128_t qry(int l, int r){
        return qry(l, r, 0, 0, size);
    }
}st;

struct QRY{
    int R, ind;
};

vector<QRY> qrs[N];

void reroot(int node, int parent){
    for(auto [R, ind] : qrs[node]){
        auto [u, v] = edges[R];
        if(par[v] == u) swap(u, v);
        if(herna(u, node)){
            if(herna(u, e)){
                answ[ind] = 1;
            }else{
                // xanut u-i subum
                __int128_t qr = st.qry(tin[u], tout[u] + 1);
                if(qr >= (__int128_t)40 * inf){
                    answ[ind] = 0;
                }else{
                    answ[ind] = -1;
                    ans[ind] = qr;
                }
            }
        }else{
            if(herna(u, e)){
                __int128_t qr = min(st.qry(1, tin[u]), st.qry(tout[u] + 1, n + 1));
                if(qr >= (__int128_t)40 * inf){
                    answ[ind] = 0;
                }else{
                    answ[ind] = -1;
                    ans[ind] = qr;
                }
            }else{
                answ[ind] = 1;
            }
        }
    }
    for(auto [u, w] : adj[node]){
        if(u == parent) continue;
        st.upd(tin[u], tout[u] + 1, -w);
        st.upd(1, tin[u], w);
        st.upd(tout[u] + 1, n + 1, w);
        reroot(u, node);
        st.upd(tout[u] + 1, n + 1, -w);
        st.upd(1, tin[u], -w);
        st.upd(tin[u], tout[u] + 1, w);
    }
}

void solve(int tc) {
    cin >> n >> s >> q >> e;
    for(int i = 1; i < n; i++){
        int u, v;
        ll w;
        cin >> u >> v >> w;
        edges.push_back({u, v});
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    for(int i = 0; i < s; i++){
        int u;
        cin >> u;
        marked[u] = 1;
    }
    dfs(1, 0);
    for(int i = 0; i < q; i++){
        int u, R;
        cin >> R >> u;
        R--;
        qrs[u].push_back({R, i});
    }
    st.init();
    reroot(1, 0);
    for(int i = 0; i < q; i++){
        if(answ[i] == 1){
            cout << "escaped\n";
        }else if(answ[i] == 0){
            cout << "oo\n";
        }else{
            cout << ans[i] << endl;
        }
    }
}


void precalc() {
    
}

int main() {
    // SetIO("in");
    // stress_test;
    FastIO();
    int test_case = 1;
    // cin >> test_case;
    auto start = chrono::steady_clock::now();
    precalc();
    int cnt = 1;
    while (test_case--) {
        solve(cnt++);
    }
    auto end = chrono::steady_clock::now();
    std::chrono::duration<double> elapsed_seconds = end - start;
    cerr << "Execution: " << elapsed_seconds.count() << endl;
    return 0;
}

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

valley.cpp: In function 'void SetIO(std::string)':
valley.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen((str + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen((str + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...