Submission #736716

# Submission time Handle Problem Language Result Execution time Memory
736716 2023-05-06T06:58:15 Z Cookie Two Currencies (JOI23_currencies) C++14
100 / 100
1402 ms 82268 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 BIT{
    ll bit[mxn + 1];
    void upd(int p, ll v){
        while(p <= n){
            bit[p] += v; p += p & (-p);
        }
    }
    ll get(int p){
        ll ans = 0;
        while(p){
            ans += bit[p];
            p -= p & (-p);
        }
        return(ans);
    }
    void update(int u, ll v){
        upd(tin[u], v); upd(tout[u] + 1, -v);
    }
    ll single(int u){
        return(get(u));
    }
};
BIT 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:90:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   90 |     for(auto [s, t, x, y, id]: cand){
      |              ^
currencies.cpp: In function 'void dfs(int, int)':
currencies.cpp:114:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  114 |     for(auto [i, id]: adj[s]){
      |              ^
currencies.cpp: In function 'int main()':
currencies.cpp:153:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  153 |         auto [s, t, x, y, id] = qq[i];
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 8 ms 5972 KB Output is correct
6 Correct 10 ms 6356 KB Output is correct
7 Correct 10 ms 6328 KB Output is correct
8 Correct 11 ms 6024 KB Output is correct
9 Correct 11 ms 5940 KB Output is correct
10 Correct 11 ms 5972 KB Output is correct
11 Correct 14 ms 5968 KB Output is correct
12 Correct 17 ms 5980 KB Output is correct
13 Correct 13 ms 6080 KB Output is correct
14 Correct 12 ms 5972 KB Output is correct
15 Correct 12 ms 5972 KB Output is correct
16 Correct 17 ms 5844 KB Output is correct
17 Correct 12 ms 5844 KB Output is correct
18 Correct 11 ms 5972 KB Output is correct
19 Correct 13 ms 6380 KB Output is correct
20 Correct 10 ms 6408 KB Output is correct
21 Correct 10 ms 6324 KB Output is correct
22 Correct 16 ms 6356 KB Output is correct
23 Correct 10 ms 6100 KB Output is correct
24 Correct 14 ms 6124 KB Output is correct
25 Correct 16 ms 5972 KB Output is correct
26 Correct 11 ms 5972 KB Output is correct
27 Correct 12 ms 6120 KB Output is correct
28 Correct 8 ms 6040 KB Output is correct
29 Correct 9 ms 7252 KB Output is correct
30 Correct 11 ms 5956 KB Output is correct
31 Correct 10 ms 5972 KB Output is correct
32 Correct 11 ms 5972 KB Output is correct
33 Correct 9 ms 5972 KB Output is correct
34 Correct 10 ms 5972 KB Output is correct
35 Correct 11 ms 5992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 11 ms 5972 KB Output is correct
3 Correct 11 ms 5972 KB Output is correct
4 Correct 10 ms 5996 KB Output is correct
5 Correct 668 ms 64756 KB Output is correct
6 Correct 741 ms 82268 KB Output is correct
7 Correct 724 ms 80420 KB Output is correct
8 Correct 611 ms 63788 KB Output is correct
9 Correct 743 ms 61396 KB Output is correct
10 Correct 836 ms 48424 KB Output is correct
11 Correct 843 ms 49504 KB Output is correct
12 Correct 954 ms 50612 KB Output is correct
13 Correct 845 ms 52400 KB Output is correct
14 Correct 867 ms 47596 KB Output is correct
15 Correct 1123 ms 50800 KB Output is correct
16 Correct 1087 ms 50880 KB Output is correct
17 Correct 1196 ms 50208 KB Output is correct
18 Correct 1402 ms 43968 KB Output is correct
19 Correct 1163 ms 44232 KB Output is correct
20 Correct 1275 ms 44844 KB Output is correct
21 Correct 576 ms 79964 KB Output is correct
22 Correct 621 ms 81728 KB Output is correct
23 Correct 668 ms 80116 KB Output is correct
24 Correct 795 ms 80008 KB Output is correct
25 Correct 647 ms 57940 KB Output is correct
26 Correct 721 ms 58936 KB Output is correct
27 Correct 671 ms 61896 KB Output is correct
28 Correct 414 ms 62284 KB Output is correct
29 Correct 363 ms 70428 KB Output is correct
30 Correct 487 ms 60716 KB Output is correct
31 Correct 488 ms 62240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 9 ms 6056 KB Output is correct
3 Correct 9 ms 5960 KB Output is correct
4 Correct 9 ms 6008 KB Output is correct
5 Correct 688 ms 67792 KB Output is correct
6 Correct 627 ms 70544 KB Output is correct
7 Correct 727 ms 75380 KB Output is correct
8 Correct 1135 ms 52216 KB Output is correct
9 Correct 988 ms 51364 KB Output is correct
10 Correct 995 ms 51816 KB Output is correct
11 Correct 801 ms 51428 KB Output is correct
12 Correct 726 ms 51392 KB Output is correct
13 Correct 679 ms 51140 KB Output is correct
14 Correct 402 ms 51260 KB Output is correct
15 Correct 407 ms 51640 KB Output is correct
16 Correct 622 ms 51540 KB Output is correct
17 Correct 540 ms 51248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 8 ms 5972 KB Output is correct
6 Correct 10 ms 6356 KB Output is correct
7 Correct 10 ms 6328 KB Output is correct
8 Correct 11 ms 6024 KB Output is correct
9 Correct 11 ms 5940 KB Output is correct
10 Correct 11 ms 5972 KB Output is correct
11 Correct 14 ms 5968 KB Output is correct
12 Correct 17 ms 5980 KB Output is correct
13 Correct 13 ms 6080 KB Output is correct
14 Correct 12 ms 5972 KB Output is correct
15 Correct 12 ms 5972 KB Output is correct
16 Correct 17 ms 5844 KB Output is correct
17 Correct 12 ms 5844 KB Output is correct
18 Correct 11 ms 5972 KB Output is correct
19 Correct 13 ms 6380 KB Output is correct
20 Correct 10 ms 6408 KB Output is correct
21 Correct 10 ms 6324 KB Output is correct
22 Correct 16 ms 6356 KB Output is correct
23 Correct 10 ms 6100 KB Output is correct
24 Correct 14 ms 6124 KB Output is correct
25 Correct 16 ms 5972 KB Output is correct
26 Correct 11 ms 5972 KB Output is correct
27 Correct 12 ms 6120 KB Output is correct
28 Correct 8 ms 6040 KB Output is correct
29 Correct 9 ms 7252 KB Output is correct
30 Correct 11 ms 5956 KB Output is correct
31 Correct 10 ms 5972 KB Output is correct
32 Correct 11 ms 5972 KB Output is correct
33 Correct 9 ms 5972 KB Output is correct
34 Correct 10 ms 5972 KB Output is correct
35 Correct 11 ms 5992 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 11 ms 5972 KB Output is correct
38 Correct 11 ms 5972 KB Output is correct
39 Correct 10 ms 5996 KB Output is correct
40 Correct 668 ms 64756 KB Output is correct
41 Correct 741 ms 82268 KB Output is correct
42 Correct 724 ms 80420 KB Output is correct
43 Correct 611 ms 63788 KB Output is correct
44 Correct 743 ms 61396 KB Output is correct
45 Correct 836 ms 48424 KB Output is correct
46 Correct 843 ms 49504 KB Output is correct
47 Correct 954 ms 50612 KB Output is correct
48 Correct 845 ms 52400 KB Output is correct
49 Correct 867 ms 47596 KB Output is correct
50 Correct 1123 ms 50800 KB Output is correct
51 Correct 1087 ms 50880 KB Output is correct
52 Correct 1196 ms 50208 KB Output is correct
53 Correct 1402 ms 43968 KB Output is correct
54 Correct 1163 ms 44232 KB Output is correct
55 Correct 1275 ms 44844 KB Output is correct
56 Correct 576 ms 79964 KB Output is correct
57 Correct 621 ms 81728 KB Output is correct
58 Correct 668 ms 80116 KB Output is correct
59 Correct 795 ms 80008 KB Output is correct
60 Correct 647 ms 57940 KB Output is correct
61 Correct 721 ms 58936 KB Output is correct
62 Correct 671 ms 61896 KB Output is correct
63 Correct 414 ms 62284 KB Output is correct
64 Correct 363 ms 70428 KB Output is correct
65 Correct 487 ms 60716 KB Output is correct
66 Correct 488 ms 62240 KB Output is correct
67 Correct 2 ms 5076 KB Output is correct
68 Correct 9 ms 6056 KB Output is correct
69 Correct 9 ms 5960 KB Output is correct
70 Correct 9 ms 6008 KB Output is correct
71 Correct 688 ms 67792 KB Output is correct
72 Correct 627 ms 70544 KB Output is correct
73 Correct 727 ms 75380 KB Output is correct
74 Correct 1135 ms 52216 KB Output is correct
75 Correct 988 ms 51364 KB Output is correct
76 Correct 995 ms 51816 KB Output is correct
77 Correct 801 ms 51428 KB Output is correct
78 Correct 726 ms 51392 KB Output is correct
79 Correct 679 ms 51140 KB Output is correct
80 Correct 402 ms 51260 KB Output is correct
81 Correct 407 ms 51640 KB Output is correct
82 Correct 622 ms 51540 KB Output is correct
83 Correct 540 ms 51248 KB Output is correct
84 Correct 648 ms 63180 KB Output is correct
85 Correct 597 ms 64820 KB Output is correct
86 Correct 544 ms 64232 KB Output is correct
87 Correct 888 ms 48404 KB Output is correct
88 Correct 995 ms 48792 KB Output is correct
89 Correct 862 ms 48804 KB Output is correct
90 Correct 1091 ms 47480 KB Output is correct
91 Correct 865 ms 48744 KB Output is correct
92 Correct 1342 ms 48332 KB Output is correct
93 Correct 1217 ms 49504 KB Output is correct
94 Correct 1209 ms 43920 KB Output is correct
95 Correct 1307 ms 45432 KB Output is correct
96 Correct 1284 ms 43480 KB Output is correct
97 Correct 1250 ms 43956 KB Output is correct
98 Correct 700 ms 79684 KB Output is correct
99 Correct 735 ms 80236 KB Output is correct
100 Correct 709 ms 78996 KB Output is correct
101 Correct 695 ms 81396 KB Output is correct
102 Correct 755 ms 50640 KB Output is correct
103 Correct 707 ms 52864 KB Output is correct
104 Correct 667 ms 52480 KB Output is correct
105 Correct 439 ms 55480 KB Output is correct
106 Correct 429 ms 56008 KB Output is correct
107 Correct 536 ms 59048 KB Output is correct
108 Correct 522 ms 57244 KB Output is correct