Submission #736714

# Submission time Handle Problem Language Result Execution time Memory
736714 2023-05-06T06:55:22 Z Cookie Two Currencies (JOI23_currencies) C++14
100 / 100
4942 ms 92644 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");

#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>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 2e5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const ll inf = 1e9;
const ld pre = 1e-6;
struct Qu{
    ll s, t, x, y, id;
};

vt<pll>edge, weight;

vt<Qu>qq;
int n, m, q;
vt<pii>adj[mxn + 1];
int ans[mxn + 1], tin[mxn + 1], tout[mxn + 1], dep[mxn + 1], up[mxn + 1][17], tt = 0;
ll dep2[mxn + 1], cnt2[mxn + 1], w[mxn + 1];

struct ST{
    ll st[4 * mxn + 1], lz[4 * mxn + 1];
    void push(int nd, int l, int mid, int r){
        st[nd << 1] += 1LL * (mid - l + 1) * lz[nd]; lz[nd << 1] += lz[nd];
        st[nd << 1 | 1] += 1LL * (r - mid) * lz[nd]; lz[nd << 1 | 1] += lz[nd];
        lz[nd] = 0;
    }
    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] += 1LL * (r - l + 1) * v; lz[nd] += v;
            return;
        }
        int mid = (l + r) >> 1;
        push(nd, l, mid, r);
        upd(nd << 1, l, mid, ql, qr, v); upd(nd << 1 | 1, mid + 1, r, ql, qr, v);
        st[nd] = st[nd << 1] + st[nd << 1 | 1];
    }
    ll get(int nd, int l, int r, int ql, int qr){
        if(ql > r || qr < l)return(0);
        
        if(ql <= l && qr >= r){
            
            return(st[nd]);
        }
        int mid = (l + r) >> 1;
        push(nd, l, mid, r);
        return(get(nd << 1, l, mid, ql, qr) + get(nd << 1 | 1, mid + 1, r, ql, qr));
    }
    ll single(int x){
        
        return(get(1, 1, n, x, x));
    }
    void update(int u, ll v){
        upd(1, 1, n, tin[u], tout[u], v);
    }
};
ST cnt, sm;
int lca(int a, int b){
    if(dep[a] < dep[b])swap(a, b);
    int k = dep[a] - dep[b];
    forr(i, 0, 17){
        if(k & (1 << i)){
            a = up[a][i];
        }
    }
    if(a == b)return(a);
    for(int i = 16; i >= 0; i--){
        if(up[a][i] != up[b][i]){
            a = up[a][i]; b = up[b][i];
        }
    }
    return(up[a][0]);
}
void solve(int l, int r, vt<Qu>cand){
    if(l > r || cand.empty())return;
    int mid = (l + r) >> 1;
    for(int i = l; i <= mid; i++){
        int id = weight[i].se, u;
        if(dep[edge[id].fi] > dep[edge[id].se])u = edge[id].fi;
        else u = edge[id].se;
        
        sm.update(u, weight[i].fi);
        
        cnt.update(u, 1);
    }
    vt<Qu>yes, no;
    for(auto [s, t, x, y, id]: cand){
        ll w = sm.single(tin[s]) + sm.single(tin[t]) - 2 * sm.single(tin[lca(s, t)]);
        int countt = cnt.single(tin[s]) + cnt.single(tin[t]) - 2 * cnt.single(tin[lca(s, t)]);
        
        if(w <= y){
            
            y -= w;
            ans[id] += countt;
            yes.pb({s, t, x, y, id});
        }else{
            no.pb({s, t, x, y, id});
        }
    }
    for(int i = l; i <= mid; i++){
        int id = weight[i].se, u;
        if(dep[edge[id].fi] > dep[edge[id].se])u = edge[id].fi;
        else u = edge[id].se;
        sm.update(u, -weight[i].fi);
        cnt.update(u, -1);
    }
    solve(l, mid - 1, no); solve(mid + 1, r, yes);
}
void dfs(int s, int pre){
    tin[s] = ++tt;
    for(auto [i, id]: adj[s]){
        if(i != pre){
            dep2[i] = dep2[s] + cnt2[id];
            up[i][0] = s; dep[i] = dep[s] + 1;
            dfs(i, s);
        }
    }
    tout[s] = tt;
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m >> q;
    edge.resize(n + 1);
    forr(i, 1, n){
        int u, v; cin >> u >> v;
        edge[i] = {u, v};
        adj[u].pb({v, i}); adj[v].pb({u, i});
    }
    forr(i, 0, m){
        int p, c; cin >> p >> c;
        weight.pb({c, p});
        cnt2[p]++;
    }
    
    sort(weight.begin(), weight.end());
    dfs(1, -1);
    for(int i = 1; i < 17; i++){
        for(int j= 1; j <= n; j++){
            up[j][i] = up[up[j][i - 1]][i - 1];
        }
    }
    forr(i, 0, q){
        ll s, t, x, y; cin >> s >> t >> x >> y;
        qq.pb({s, t, x, y, i});
    }
    solve(0, weight.size() - 1, qq);
    
    forr(i, 0, q){
        //cout << ans[i] << " ";
        auto [s, t, x, y, id] = qq[i];
        int len = dep2[s] + dep2[t] - 2 * dep2[lca(s, t)];
        //cout << len << " ";
        if(len - ans[i] <= x){
            cout << x - (len - ans[i]) << "\n";
        }else{
            cout << -1 << "\n";
        }
    }
    return(0);
}

Compilation message

currencies.cpp: In function 'void solve(int, int, std::vector<Qu>)':
currencies.cpp:105:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  105 |     for(auto [s, t, x, y, id]: cand){
      |              ^
currencies.cpp: In function 'void dfs(int, int)':
currencies.cpp:129:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  129 |     for(auto [i, id]: adj[s]){
      |              ^
currencies.cpp: In function 'int main()':
currencies.cpp:168:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  168 |         auto [s, t, x, y, id] = qq[i];
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 5064 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 19 ms 6100 KB Output is correct
6 Correct 26 ms 6592 KB Output is correct
7 Correct 34 ms 6440 KB Output is correct
8 Correct 27 ms 6100 KB Output is correct
9 Correct 33 ms 6064 KB Output is correct
10 Correct 27 ms 6124 KB Output is correct
11 Correct 28 ms 6068 KB Output is correct
12 Correct 30 ms 6048 KB Output is correct
13 Correct 28 ms 6100 KB Output is correct
14 Correct 31 ms 6108 KB Output is correct
15 Correct 31 ms 6108 KB Output is correct
16 Correct 31 ms 6012 KB Output is correct
17 Correct 35 ms 6020 KB Output is correct
18 Correct 33 ms 5972 KB Output is correct
19 Correct 25 ms 6492 KB Output is correct
20 Correct 51 ms 6484 KB Output is correct
21 Correct 24 ms 6484 KB Output is correct
22 Correct 51 ms 6496 KB Output is correct
23 Correct 23 ms 6100 KB Output is correct
24 Correct 27 ms 6192 KB Output is correct
25 Correct 24 ms 6100 KB Output is correct
26 Correct 20 ms 6096 KB Output is correct
27 Correct 19 ms 6272 KB Output is correct
28 Correct 23 ms 6168 KB Output is correct
29 Correct 21 ms 7380 KB Output is correct
30 Correct 28 ms 6100 KB Output is correct
31 Correct 56 ms 6008 KB Output is correct
32 Correct 28 ms 6080 KB Output is correct
33 Correct 28 ms 6100 KB Output is correct
34 Correct 29 ms 6144 KB Output is correct
35 Correct 56 ms 6132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 28 ms 6088 KB Output is correct
3 Correct 27 ms 6100 KB Output is correct
4 Correct 27 ms 6076 KB Output is correct
5 Correct 2412 ms 71340 KB Output is correct
6 Correct 3012 ms 89396 KB Output is correct
7 Correct 2906 ms 87408 KB Output is correct
8 Correct 2112 ms 70860 KB Output is correct
9 Correct 2157 ms 68320 KB Output is correct
10 Correct 3288 ms 55184 KB Output is correct
11 Correct 3272 ms 56260 KB Output is correct
12 Correct 3266 ms 57232 KB Output is correct
13 Correct 3281 ms 59068 KB Output is correct
14 Correct 3412 ms 54236 KB Output is correct
15 Correct 4270 ms 57344 KB Output is correct
16 Correct 4231 ms 57564 KB Output is correct
17 Correct 4017 ms 56724 KB Output is correct
18 Correct 4224 ms 50688 KB Output is correct
19 Correct 4040 ms 50824 KB Output is correct
20 Correct 4536 ms 51476 KB Output is correct
21 Correct 3265 ms 86672 KB Output is correct
22 Correct 3331 ms 88384 KB Output is correct
23 Correct 2986 ms 86708 KB Output is correct
24 Correct 2763 ms 86736 KB Output is correct
25 Correct 2575 ms 64660 KB Output is correct
26 Correct 2582 ms 65728 KB Output is correct
27 Correct 2478 ms 68540 KB Output is correct
28 Correct 1581 ms 68908 KB Output is correct
29 Correct 1629 ms 77040 KB Output is correct
30 Correct 2132 ms 67480 KB Output is correct
31 Correct 2210 ms 69064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 27 ms 6172 KB Output is correct
3 Correct 26 ms 6140 KB Output is correct
4 Correct 28 ms 6152 KB Output is correct
5 Correct 2465 ms 74600 KB Output is correct
6 Correct 2470 ms 77380 KB Output is correct
7 Correct 2950 ms 78732 KB Output is correct
8 Correct 3987 ms 58816 KB Output is correct
9 Correct 3797 ms 58140 KB Output is correct
10 Correct 3823 ms 58312 KB Output is correct
11 Correct 2811 ms 58260 KB Output is correct
12 Correct 2852 ms 57932 KB Output is correct
13 Correct 2757 ms 57888 KB Output is correct
14 Correct 1994 ms 58064 KB Output is correct
15 Correct 1996 ms 58556 KB Output is correct
16 Correct 2180 ms 58256 KB Output is correct
17 Correct 2171 ms 57984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 5064 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 19 ms 6100 KB Output is correct
6 Correct 26 ms 6592 KB Output is correct
7 Correct 34 ms 6440 KB Output is correct
8 Correct 27 ms 6100 KB Output is correct
9 Correct 33 ms 6064 KB Output is correct
10 Correct 27 ms 6124 KB Output is correct
11 Correct 28 ms 6068 KB Output is correct
12 Correct 30 ms 6048 KB Output is correct
13 Correct 28 ms 6100 KB Output is correct
14 Correct 31 ms 6108 KB Output is correct
15 Correct 31 ms 6108 KB Output is correct
16 Correct 31 ms 6012 KB Output is correct
17 Correct 35 ms 6020 KB Output is correct
18 Correct 33 ms 5972 KB Output is correct
19 Correct 25 ms 6492 KB Output is correct
20 Correct 51 ms 6484 KB Output is correct
21 Correct 24 ms 6484 KB Output is correct
22 Correct 51 ms 6496 KB Output is correct
23 Correct 23 ms 6100 KB Output is correct
24 Correct 27 ms 6192 KB Output is correct
25 Correct 24 ms 6100 KB Output is correct
26 Correct 20 ms 6096 KB Output is correct
27 Correct 19 ms 6272 KB Output is correct
28 Correct 23 ms 6168 KB Output is correct
29 Correct 21 ms 7380 KB Output is correct
30 Correct 28 ms 6100 KB Output is correct
31 Correct 56 ms 6008 KB Output is correct
32 Correct 28 ms 6080 KB Output is correct
33 Correct 28 ms 6100 KB Output is correct
34 Correct 29 ms 6144 KB Output is correct
35 Correct 56 ms 6132 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 28 ms 6088 KB Output is correct
38 Correct 27 ms 6100 KB Output is correct
39 Correct 27 ms 6076 KB Output is correct
40 Correct 2412 ms 71340 KB Output is correct
41 Correct 3012 ms 89396 KB Output is correct
42 Correct 2906 ms 87408 KB Output is correct
43 Correct 2112 ms 70860 KB Output is correct
44 Correct 2157 ms 68320 KB Output is correct
45 Correct 3288 ms 55184 KB Output is correct
46 Correct 3272 ms 56260 KB Output is correct
47 Correct 3266 ms 57232 KB Output is correct
48 Correct 3281 ms 59068 KB Output is correct
49 Correct 3412 ms 54236 KB Output is correct
50 Correct 4270 ms 57344 KB Output is correct
51 Correct 4231 ms 57564 KB Output is correct
52 Correct 4017 ms 56724 KB Output is correct
53 Correct 4224 ms 50688 KB Output is correct
54 Correct 4040 ms 50824 KB Output is correct
55 Correct 4536 ms 51476 KB Output is correct
56 Correct 3265 ms 86672 KB Output is correct
57 Correct 3331 ms 88384 KB Output is correct
58 Correct 2986 ms 86708 KB Output is correct
59 Correct 2763 ms 86736 KB Output is correct
60 Correct 2575 ms 64660 KB Output is correct
61 Correct 2582 ms 65728 KB Output is correct
62 Correct 2478 ms 68540 KB Output is correct
63 Correct 1581 ms 68908 KB Output is correct
64 Correct 1629 ms 77040 KB Output is correct
65 Correct 2132 ms 67480 KB Output is correct
66 Correct 2210 ms 69064 KB Output is correct
67 Correct 3 ms 5076 KB Output is correct
68 Correct 27 ms 6172 KB Output is correct
69 Correct 26 ms 6140 KB Output is correct
70 Correct 28 ms 6152 KB Output is correct
71 Correct 2465 ms 74600 KB Output is correct
72 Correct 2470 ms 77380 KB Output is correct
73 Correct 2950 ms 78732 KB Output is correct
74 Correct 3987 ms 58816 KB Output is correct
75 Correct 3797 ms 58140 KB Output is correct
76 Correct 3823 ms 58312 KB Output is correct
77 Correct 2811 ms 58260 KB Output is correct
78 Correct 2852 ms 57932 KB Output is correct
79 Correct 2757 ms 57888 KB Output is correct
80 Correct 1994 ms 58064 KB Output is correct
81 Correct 1996 ms 58556 KB Output is correct
82 Correct 2180 ms 58256 KB Output is correct
83 Correct 2171 ms 57984 KB Output is correct
84 Correct 2584 ms 70120 KB Output is correct
85 Correct 2477 ms 68208 KB Output is correct
86 Correct 2224 ms 67660 KB Output is correct
87 Correct 3539 ms 55084 KB Output is correct
88 Correct 3348 ms 55420 KB Output is correct
89 Correct 3370 ms 55636 KB Output is correct
90 Correct 3402 ms 54128 KB Output is correct
91 Correct 3591 ms 55400 KB Output is correct
92 Correct 4320 ms 55172 KB Output is correct
93 Correct 4078 ms 56036 KB Output is correct
94 Correct 4173 ms 50508 KB Output is correct
95 Correct 4598 ms 57564 KB Output is correct
96 Correct 4762 ms 55392 KB Output is correct
97 Correct 4942 ms 56072 KB Output is correct
98 Correct 3486 ms 91032 KB Output is correct
99 Correct 3658 ms 91488 KB Output is correct
100 Correct 3674 ms 90020 KB Output is correct
101 Correct 3789 ms 92644 KB Output is correct
102 Correct 3077 ms 62656 KB Output is correct
103 Correct 3491 ms 65124 KB Output is correct
104 Correct 2855 ms 64576 KB Output is correct
105 Correct 1972 ms 67280 KB Output is correct
106 Correct 2160 ms 67376 KB Output is correct
107 Correct 2621 ms 70592 KB Output is correct
108 Correct 2375 ms 68956 KB Output is correct