Submission #960019

# Submission time Handle Problem Language Result Execution time Memory
960019 2024-04-09T13:50:20 Z Cookie Grapevine (NOI22_grapevine) C++14
100 / 100
2221 ms 243056 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 69, pr = 31;
const int mxn = 1e5 + 5, mxq = 1e5 + 5, sq = 300, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e18 + 5, neg = -69420, inf2 = 1e16;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
// <3;
struct ST{
    vt<ll>st, lz;
    void init(int siz, bool bad){
        st.resize(4 * siz + 1, 0); lz.resize(4 * siz + 1, 0);
        if(bad){
            for(int i = 1; i <= 4 * siz; i++)st[i] = inf;
        }
    }
    void push(int nd){
        ll &v = lz[nd];
        lz[nd << 1] += v; lz[nd << 1 | 1] += v; st[nd << 1] += v; st[nd << 1 | 1] += v;
        v = 0;
    }
    void change(int nd, int l, int r, int id, ll v){
        if(id > r || id < l)return;
        if(l == r){
            st[nd] = v;
            return;
        }
        int mid = (l + r) >> 1;
        push(nd);
        change(nd << 1, l, mid, id, v); change(nd << 1 | 1, mid + 1, r, id, v);
        st[nd] = min(st[nd << 1], st[nd << 1 | 1]);
    }
    void upd(int nd, int l, int r, int ql, int qr, ll v){
        if(ql > r || qr < l)return;
        if(ql <= l && qr >= r){
            st[nd] += v; lz[nd] += v;
            return;
        }   
        push(nd);
        int mid = (l + r) >> 1;
        upd(nd << 1, l, mid, ql, qr, v); upd(nd << 1 | 1, mid + 1, r, ql, qr, v);
        st[nd] = min(st[nd << 1], st[nd << 1 | 1]);
    }
    ll get(int nd, int l, int r, int id){
        if(id > r || id < l)return(inf);
        if(l == r)return(st[nd]);
        push(nd);
        int mid = (l + r) >> 1;
        return(min(get(nd << 1, l, mid, id), get(nd << 1 | 1, mid + 1, r, id)));
    }
};

ST st[mxn + 1], st2[mxn + 1];
int n, q;
vt<pii>adj[mxn + 1];
int tin[20][mxn + 1], tout[20][mxn + 1], tt[mxn + 1], siz[mxn + 1], dep[mxn + 1], pa[mxn + 1];
bool vis[mxn + 1], on[mxn + 1];
int dfs(int s, int pre){
    siz[s] = 1;
    for(auto [i, w]: adj[s]){
        if(i != pre && !vis[i]){
            siz[s] += dfs(i, s);
        }
    }
    return(siz[s]);
}
int centroid(int s, int pre, int need){
    for(auto [i, w]: adj[s]){
        if(i != pre && !vis[i] && siz[i] * 2 > need)return(centroid(i, s, need));
    }
    return(s);
}
void dfs2(int s, int pre, int depth,int root){
    tin[depth][s] = ++tt[root];
    for(auto [v, w]: adj[s]){
        if(!vis[v] && v != pre){
            dfs2(v, s, depth, root);
        }
    }
    tout[depth][s] = tt[root];
}
ll eu[mxn + 1], ev[mxn + 1], ew[mxn + 1];
void build(int s, int pre, int depth){
    int c = centroid(s, -1, dfs(s, -1)); dep[c] = depth; pa[c] = pre;
   
    vis[c] = 1; 
    st[c].init(siz[s], 0); st2[c].init(siz[s], 1);
    dfs2(c, -1 , depth,c);
    for(auto [i, w]: adj[c]){
        if(!vis[i]){
            build(i, c, depth + 1);
        }
    }
}
map<pii, int>toe;
void anneal(int u, int v, ll w){
    if(dep[u] > dep[v])swap(u, v);
    int node = u;

    for(int i = dep[u]; i >= 0; i--){
        int at = ((tin[i][u] < tin[i][v]) ? v : u);
        st[node].upd(1, 1, tt[node], tin[i][at], tout[i][at], w);
        st2[node].upd(1, 1, tt[node], tin[i][at], tout[i][at], w);
        //if(i == 0)cout << tin[i][v] << ' ' << tout[i][v] << " " << w << "\n";
        node = pa[node];
    }
}
void seek(int u){
    ll ans = inf;
    int node = u;
    for(int i = dep[u]; i >= 0; i--){
        //cout << node << " " << st2[node].st[1] << "\n";
        ans = min(ans, st[node].get(1, 1, tt[node], tin[i][u]) + st2[node].st[1]);
        node = pa[node];
    }
    cout << ((ans >= inf2) ? -1 : ans) << "\n";
}
void soak(int u){
    int node = u;
    for(int i = dep[u]; i >= 0; i--){
        if(on[u])st2[node].change(1, 1, tt[node], tin[i][u], st[node].get(1, 1, tt[node], tin[i][u]));
        else st2[node].change(1, 1, tt[node], tin[i][u], inf);
        node = pa[node];
    }
}
void solve(){
    cin >> n >> q;
    for(int i = 1; i < n; i++){
        cin >> eu[i] >> ev[i] >> ew[i];
        if(eu[i] > ev[i])swap(eu[i], ev[i]); toe[mpp(eu[i], ev[i])] = i;
        adj[eu[i]].pb(mpp(ev[i], ew[i]));  adj[ev[i]].pb(mpp(eu[i], ew[i]));
    }
    
    build(1, -1, 0);
    for(int i = 1; i < n; i++){
        anneal(eu[i], ev[i], ew[i]);
    }
    
    while(q--){
        int idq; cin >> idq;
        if(idq == 1){
            int u; cin >> u; seek(u);
        }else if(idq == 2){
            
            int u; cin >> u; on[u] = !on[u]; soak(u);
        }else{
            int a, b, w; cin >> a >> b >> w; if(a > b)swap(a, b); 
            ll crw = ew[toe[mpp(a, b)]]; 
            anneal(a, b, w - crw); ew[toe[mpp(a, b)]] = w;
        }
    }
}


signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("netw.inp", "r", stdin);
    //freopen("netw.out", "w", stdout);
    int ttt; ttt = 1;
    while(ttt--){
        solve();

    }
    return(0);
}
/*
1
5 7 2
1 1 1
3 1 2
2 2 3
4 2 4
2 4 2
1 5 5
3 5 2
5 3
2 7
3 3 1
1 1 1
1 2 2
1 3 3
3 1
*/

Compilation message

Main.cpp: In function 'int dfs(int, int)':
Main.cpp:81:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   81 |     for(auto [i, w]: adj[s]){
      |              ^
Main.cpp: In function 'int centroid(int, int, int)':
Main.cpp:89:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   89 |     for(auto [i, w]: adj[s]){
      |              ^
Main.cpp: In function 'void dfs2(int, int, int, int)':
Main.cpp:96:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   96 |     for(auto [v, w]: adj[s]){
      |              ^
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:110:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  110 |     for(auto [i, w]: adj[c]){
      |              ^
Main.cpp: In function 'void solve()':
Main.cpp:151:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  151 |         if(eu[i] > ev[i])swap(eu[i], ev[i]); toe[mpp(eu[i], ev[i])] = i;
      |         ^~
Main.cpp:151:46: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  151 |         if(eu[i] > ev[i])swap(eu[i], ev[i]); toe[mpp(eu[i], ev[i])] = i;
      |                                              ^~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 30044 KB Output is correct
2 Correct 15 ms 29908 KB Output is correct
3 Correct 14 ms 30040 KB Output is correct
4 Correct 14 ms 30300 KB Output is correct
5 Correct 14 ms 30208 KB Output is correct
6 Correct 13 ms 30372 KB Output is correct
7 Correct 7 ms 24412 KB Output is correct
8 Correct 8 ms 24412 KB Output is correct
9 Correct 7 ms 24408 KB Output is correct
10 Correct 12 ms 30300 KB Output is correct
11 Correct 13 ms 30300 KB Output is correct
12 Correct 13 ms 30140 KB Output is correct
13 Correct 7 ms 24412 KB Output is correct
14 Correct 7 ms 24368 KB Output is correct
15 Correct 8 ms 24372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1869 ms 199116 KB Output is correct
2 Correct 1764 ms 195496 KB Output is correct
3 Correct 1852 ms 195180 KB Output is correct
4 Correct 1399 ms 240704 KB Output is correct
5 Correct 1324 ms 227188 KB Output is correct
6 Correct 1364 ms 230660 KB Output is correct
7 Correct 193 ms 67784 KB Output is correct
8 Correct 190 ms 66500 KB Output is correct
9 Correct 202 ms 66588 KB Output is correct
10 Correct 1434 ms 239496 KB Output is correct
11 Correct 1391 ms 234232 KB Output is correct
12 Correct 1443 ms 243056 KB Output is correct
13 Correct 179 ms 66756 KB Output is correct
14 Correct 221 ms 66804 KB Output is correct
15 Correct 186 ms 67272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1493 ms 235052 KB Output is correct
2 Correct 1552 ms 235972 KB Output is correct
3 Correct 1600 ms 236396 KB Output is correct
4 Correct 1463 ms 231248 KB Output is correct
5 Correct 1541 ms 232228 KB Output is correct
6 Correct 1522 ms 229560 KB Output is correct
7 Correct 1453 ms 229392 KB Output is correct
8 Correct 1628 ms 233668 KB Output is correct
9 Correct 1536 ms 230340 KB Output is correct
10 Correct 1541 ms 235888 KB Output is correct
11 Correct 1474 ms 231068 KB Output is correct
12 Correct 1521 ms 228028 KB Output is correct
13 Correct 1482 ms 233716 KB Output is correct
14 Correct 1449 ms 227928 KB Output is correct
15 Correct 1536 ms 231220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1919 ms 196716 KB Output is correct
2 Correct 2033 ms 199324 KB Output is correct
3 Correct 1818 ms 192700 KB Output is correct
4 Correct 1596 ms 239432 KB Output is correct
5 Correct 1566 ms 238024 KB Output is correct
6 Correct 1586 ms 240212 KB Output is correct
7 Correct 194 ms 67044 KB Output is correct
8 Correct 203 ms 68264 KB Output is correct
9 Correct 232 ms 67212 KB Output is correct
10 Correct 1515 ms 237212 KB Output is correct
11 Correct 1422 ms 229504 KB Output is correct
12 Correct 1506 ms 237188 KB Output is correct
13 Correct 202 ms 66764 KB Output is correct
14 Correct 201 ms 68292 KB Output is correct
15 Correct 212 ms 67324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2221 ms 202392 KB Output is correct
2 Correct 2160 ms 200132 KB Output is correct
3 Correct 2096 ms 196112 KB Output is correct
4 Correct 1786 ms 226928 KB Output is correct
5 Correct 1755 ms 230880 KB Output is correct
6 Correct 1662 ms 233644 KB Output is correct
7 Correct 210 ms 66704 KB Output is correct
8 Correct 220 ms 67520 KB Output is correct
9 Correct 245 ms 67604 KB Output is correct
10 Correct 1564 ms 225256 KB Output is correct
11 Correct 1551 ms 231880 KB Output is correct
12 Correct 1604 ms 235580 KB Output is correct
13 Correct 231 ms 67460 KB Output is correct
14 Correct 214 ms 66760 KB Output is correct
15 Correct 216 ms 67124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 30044 KB Output is correct
2 Correct 15 ms 29908 KB Output is correct
3 Correct 14 ms 30040 KB Output is correct
4 Correct 14 ms 30300 KB Output is correct
5 Correct 14 ms 30208 KB Output is correct
6 Correct 13 ms 30372 KB Output is correct
7 Correct 7 ms 24412 KB Output is correct
8 Correct 8 ms 24412 KB Output is correct
9 Correct 7 ms 24408 KB Output is correct
10 Correct 12 ms 30300 KB Output is correct
11 Correct 13 ms 30300 KB Output is correct
12 Correct 13 ms 30140 KB Output is correct
13 Correct 7 ms 24412 KB Output is correct
14 Correct 7 ms 24368 KB Output is correct
15 Correct 8 ms 24372 KB Output is correct
16 Correct 1869 ms 199116 KB Output is correct
17 Correct 1764 ms 195496 KB Output is correct
18 Correct 1852 ms 195180 KB Output is correct
19 Correct 1399 ms 240704 KB Output is correct
20 Correct 1324 ms 227188 KB Output is correct
21 Correct 1364 ms 230660 KB Output is correct
22 Correct 193 ms 67784 KB Output is correct
23 Correct 190 ms 66500 KB Output is correct
24 Correct 202 ms 66588 KB Output is correct
25 Correct 1434 ms 239496 KB Output is correct
26 Correct 1391 ms 234232 KB Output is correct
27 Correct 1443 ms 243056 KB Output is correct
28 Correct 179 ms 66756 KB Output is correct
29 Correct 221 ms 66804 KB Output is correct
30 Correct 186 ms 67272 KB Output is correct
31 Correct 1493 ms 235052 KB Output is correct
32 Correct 1552 ms 235972 KB Output is correct
33 Correct 1600 ms 236396 KB Output is correct
34 Correct 1463 ms 231248 KB Output is correct
35 Correct 1541 ms 232228 KB Output is correct
36 Correct 1522 ms 229560 KB Output is correct
37 Correct 1453 ms 229392 KB Output is correct
38 Correct 1628 ms 233668 KB Output is correct
39 Correct 1536 ms 230340 KB Output is correct
40 Correct 1541 ms 235888 KB Output is correct
41 Correct 1474 ms 231068 KB Output is correct
42 Correct 1521 ms 228028 KB Output is correct
43 Correct 1482 ms 233716 KB Output is correct
44 Correct 1449 ms 227928 KB Output is correct
45 Correct 1536 ms 231220 KB Output is correct
46 Correct 1919 ms 196716 KB Output is correct
47 Correct 2033 ms 199324 KB Output is correct
48 Correct 1818 ms 192700 KB Output is correct
49 Correct 1596 ms 239432 KB Output is correct
50 Correct 1566 ms 238024 KB Output is correct
51 Correct 1586 ms 240212 KB Output is correct
52 Correct 194 ms 67044 KB Output is correct
53 Correct 203 ms 68264 KB Output is correct
54 Correct 232 ms 67212 KB Output is correct
55 Correct 1515 ms 237212 KB Output is correct
56 Correct 1422 ms 229504 KB Output is correct
57 Correct 1506 ms 237188 KB Output is correct
58 Correct 202 ms 66764 KB Output is correct
59 Correct 201 ms 68292 KB Output is correct
60 Correct 212 ms 67324 KB Output is correct
61 Correct 2221 ms 202392 KB Output is correct
62 Correct 2160 ms 200132 KB Output is correct
63 Correct 2096 ms 196112 KB Output is correct
64 Correct 1786 ms 226928 KB Output is correct
65 Correct 1755 ms 230880 KB Output is correct
66 Correct 1662 ms 233644 KB Output is correct
67 Correct 210 ms 66704 KB Output is correct
68 Correct 220 ms 67520 KB Output is correct
69 Correct 245 ms 67604 KB Output is correct
70 Correct 1564 ms 225256 KB Output is correct
71 Correct 1551 ms 231880 KB Output is correct
72 Correct 1604 ms 235580 KB Output is correct
73 Correct 231 ms 67460 KB Output is correct
74 Correct 214 ms 66760 KB Output is correct
75 Correct 216 ms 67124 KB Output is correct
76 Correct 5 ms 23388 KB Output is correct
77 Correct 4 ms 23388 KB Output is correct
78 Correct 5 ms 23384 KB Output is correct
79 Correct 1921 ms 192544 KB Output is correct
80 Correct 2021 ms 197772 KB Output is correct
81 Correct 2096 ms 199128 KB Output is correct
82 Correct 1553 ms 234160 KB Output is correct
83 Correct 1566 ms 231804 KB Output is correct
84 Correct 1644 ms 232520 KB Output is correct
85 Correct 215 ms 68428 KB Output is correct
86 Correct 202 ms 67912 KB Output is correct
87 Correct 216 ms 66756 KB Output is correct
88 Correct 1496 ms 228768 KB Output is correct
89 Correct 1526 ms 232080 KB Output is correct
90 Correct 1483 ms 229044 KB Output is correct
91 Correct 226 ms 68184 KB Output is correct
92 Correct 205 ms 67272 KB Output is correct
93 Correct 237 ms 67164 KB Output is correct