Submission #736712

# Submission time Handle Problem Language Result Execution time Memory
736712 2023-05-06T06:52:33 Z Cookie Two Currencies (JOI23_currencies) C++14
68 / 100
5000 ms 102032 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define int long long
#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{
    int s, t, x, y, id;
};

vt<pii>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] += (mid - l + 1) * lz[nd]; lz[nd << 1] += lz[nd];
        st[nd << 1 | 1] += (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){
        int 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(long long int, long long 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(long long int, long long 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 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 22 ms 6380 KB Output is correct
6 Correct 26 ms 6852 KB Output is correct
7 Correct 23 ms 6680 KB Output is correct
8 Correct 28 ms 6356 KB Output is correct
9 Correct 28 ms 6348 KB Output is correct
10 Correct 29 ms 6344 KB Output is correct
11 Correct 27 ms 6352 KB Output is correct
12 Correct 35 ms 6332 KB Output is correct
13 Correct 28 ms 6448 KB Output is correct
14 Correct 29 ms 6356 KB Output is correct
15 Correct 30 ms 6316 KB Output is correct
16 Correct 35 ms 6284 KB Output is correct
17 Correct 32 ms 6324 KB Output is correct
18 Correct 31 ms 6360 KB Output is correct
19 Correct 26 ms 6732 KB Output is correct
20 Correct 30 ms 6748 KB Output is correct
21 Correct 27 ms 6672 KB Output is correct
22 Correct 27 ms 6760 KB Output is correct
23 Correct 27 ms 6452 KB Output is correct
24 Correct 24 ms 6412 KB Output is correct
25 Correct 25 ms 6356 KB Output is correct
26 Correct 23 ms 6436 KB Output is correct
27 Correct 20 ms 6504 KB Output is correct
28 Correct 22 ms 6484 KB Output is correct
29 Correct 26 ms 7636 KB Output is correct
30 Correct 29 ms 6432 KB Output is correct
31 Correct 29 ms 6316 KB Output is correct
32 Correct 29 ms 6380 KB Output is correct
33 Correct 26 ms 6388 KB Output is correct
34 Correct 28 ms 6280 KB Output is correct
35 Correct 30 ms 6400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 28 ms 6296 KB Output is correct
3 Correct 32 ms 6316 KB Output is correct
4 Correct 28 ms 6392 KB Output is correct
5 Correct 2562 ms 85040 KB Output is correct
6 Correct 3256 ms 100832 KB Output is correct
7 Correct 3133 ms 99192 KB Output is correct
8 Correct 2387 ms 82736 KB Output is correct
9 Correct 2301 ms 79964 KB Output is correct
10 Correct 3583 ms 69920 KB Output is correct
11 Correct 3724 ms 70944 KB Output is correct
12 Correct 3433 ms 71944 KB Output is correct
13 Correct 3481 ms 73968 KB Output is correct
14 Correct 3411 ms 69140 KB Output is correct
15 Correct 4317 ms 72148 KB Output is correct
16 Correct 4136 ms 72468 KB Output is correct
17 Correct 3887 ms 71848 KB Output is correct
18 Correct 4377 ms 66352 KB Output is correct
19 Correct 4257 ms 66576 KB Output is correct
20 Correct 4212 ms 67236 KB Output is correct
21 Correct 2980 ms 100292 KB Output is correct
22 Correct 2886 ms 102032 KB Output is correct
23 Correct 2996 ms 100572 KB Output is correct
24 Correct 2913 ms 100356 KB Output is correct
25 Correct 2684 ms 79720 KB Output is correct
26 Correct 2737 ms 80764 KB Output is correct
27 Correct 2659 ms 83148 KB Output is correct
28 Correct 1655 ms 83704 KB Output is correct
29 Correct 1796 ms 91912 KB Output is correct
30 Correct 2138 ms 82312 KB Output is correct
31 Correct 2278 ms 83980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 30 ms 6384 KB Output is correct
3 Correct 28 ms 6356 KB Output is correct
4 Correct 25 ms 6396 KB Output is correct
5 Correct 2465 ms 86460 KB Output is correct
6 Correct 2422 ms 89116 KB Output is correct
7 Correct 2953 ms 88164 KB Output is correct
8 Correct 3803 ms 72628 KB Output is correct
9 Correct 3856 ms 71976 KB Output is correct
10 Correct 4238 ms 72280 KB Output is correct
11 Correct 2704 ms 72104 KB Output is correct
12 Correct 2738 ms 71988 KB Output is correct
13 Correct 2644 ms 71876 KB Output is correct
14 Correct 1888 ms 71788 KB Output is correct
15 Correct 1845 ms 72220 KB Output is correct
16 Correct 1999 ms 72400 KB Output is correct
17 Correct 2039 ms 72028 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 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 22 ms 6380 KB Output is correct
6 Correct 26 ms 6852 KB Output is correct
7 Correct 23 ms 6680 KB Output is correct
8 Correct 28 ms 6356 KB Output is correct
9 Correct 28 ms 6348 KB Output is correct
10 Correct 29 ms 6344 KB Output is correct
11 Correct 27 ms 6352 KB Output is correct
12 Correct 35 ms 6332 KB Output is correct
13 Correct 28 ms 6448 KB Output is correct
14 Correct 29 ms 6356 KB Output is correct
15 Correct 30 ms 6316 KB Output is correct
16 Correct 35 ms 6284 KB Output is correct
17 Correct 32 ms 6324 KB Output is correct
18 Correct 31 ms 6360 KB Output is correct
19 Correct 26 ms 6732 KB Output is correct
20 Correct 30 ms 6748 KB Output is correct
21 Correct 27 ms 6672 KB Output is correct
22 Correct 27 ms 6760 KB Output is correct
23 Correct 27 ms 6452 KB Output is correct
24 Correct 24 ms 6412 KB Output is correct
25 Correct 25 ms 6356 KB Output is correct
26 Correct 23 ms 6436 KB Output is correct
27 Correct 20 ms 6504 KB Output is correct
28 Correct 22 ms 6484 KB Output is correct
29 Correct 26 ms 7636 KB Output is correct
30 Correct 29 ms 6432 KB Output is correct
31 Correct 29 ms 6316 KB Output is correct
32 Correct 29 ms 6380 KB Output is correct
33 Correct 26 ms 6388 KB Output is correct
34 Correct 28 ms 6280 KB Output is correct
35 Correct 30 ms 6400 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 28 ms 6296 KB Output is correct
38 Correct 32 ms 6316 KB Output is correct
39 Correct 28 ms 6392 KB Output is correct
40 Correct 2562 ms 85040 KB Output is correct
41 Correct 3256 ms 100832 KB Output is correct
42 Correct 3133 ms 99192 KB Output is correct
43 Correct 2387 ms 82736 KB Output is correct
44 Correct 2301 ms 79964 KB Output is correct
45 Correct 3583 ms 69920 KB Output is correct
46 Correct 3724 ms 70944 KB Output is correct
47 Correct 3433 ms 71944 KB Output is correct
48 Correct 3481 ms 73968 KB Output is correct
49 Correct 3411 ms 69140 KB Output is correct
50 Correct 4317 ms 72148 KB Output is correct
51 Correct 4136 ms 72468 KB Output is correct
52 Correct 3887 ms 71848 KB Output is correct
53 Correct 4377 ms 66352 KB Output is correct
54 Correct 4257 ms 66576 KB Output is correct
55 Correct 4212 ms 67236 KB Output is correct
56 Correct 2980 ms 100292 KB Output is correct
57 Correct 2886 ms 102032 KB Output is correct
58 Correct 2996 ms 100572 KB Output is correct
59 Correct 2913 ms 100356 KB Output is correct
60 Correct 2684 ms 79720 KB Output is correct
61 Correct 2737 ms 80764 KB Output is correct
62 Correct 2659 ms 83148 KB Output is correct
63 Correct 1655 ms 83704 KB Output is correct
64 Correct 1796 ms 91912 KB Output is correct
65 Correct 2138 ms 82312 KB Output is correct
66 Correct 2278 ms 83980 KB Output is correct
67 Correct 3 ms 5076 KB Output is correct
68 Correct 30 ms 6384 KB Output is correct
69 Correct 28 ms 6356 KB Output is correct
70 Correct 25 ms 6396 KB Output is correct
71 Correct 2465 ms 86460 KB Output is correct
72 Correct 2422 ms 89116 KB Output is correct
73 Correct 2953 ms 88164 KB Output is correct
74 Correct 3803 ms 72628 KB Output is correct
75 Correct 3856 ms 71976 KB Output is correct
76 Correct 4238 ms 72280 KB Output is correct
77 Correct 2704 ms 72104 KB Output is correct
78 Correct 2738 ms 71988 KB Output is correct
79 Correct 2644 ms 71876 KB Output is correct
80 Correct 1888 ms 71788 KB Output is correct
81 Correct 1845 ms 72220 KB Output is correct
82 Correct 1999 ms 72400 KB Output is correct
83 Correct 2039 ms 72028 KB Output is correct
84 Correct 2644 ms 82324 KB Output is correct
85 Correct 2574 ms 77604 KB Output is correct
86 Correct 2332 ms 76616 KB Output is correct
87 Correct 3704 ms 69880 KB Output is correct
88 Correct 3714 ms 69944 KB Output is correct
89 Correct 3777 ms 70416 KB Output is correct
90 Correct 3783 ms 69272 KB Output is correct
91 Correct 3634 ms 69980 KB Output is correct
92 Correct 4341 ms 70252 KB Output is correct
93 Correct 4448 ms 71084 KB Output is correct
94 Execution timed out 5039 ms 66432 KB Time limit exceeded
95 Halted 0 ms 0 KB -