Submission #859702

# Submission time Handle Problem Language Result Execution time Memory
859702 2023-10-10T13:42:01 Z pemguimn Two Currencies (JOI23_currencies) C++14
100 / 100
3754 ms 65432 KB
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define endl "\n"
#define task "hihi"
using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7;  
int n, m, q, par[18][N], h[N], c[N], num[N], tail[N], lo[N], hi[N], bit1[N], bit2[N], ans[N], timedfs = 0;
vector<pii> edges(N + 5);
vector<int> adj[N];
vector<int> pos[N];
array<int, 4> a[N];
pii b[N];
void dfs(int i, int pre){
    num[i] = ++timedfs;
    for(int x : adj[i]){
        if(x == pre) continue;
        h[x] = h[i] + 1;
        par[0][x] = i;
        for(int j = 1; j < 18; j++) par[j][x] = par[j - 1][par[j - 1][x]];
        dfs(x, i);
    }
    tail[i] = timedfs;
}
void upd(int bit[], int x, int val){
    for(; x <= n; x += x & -x){
        bit[x] += val;
    }
}
void upd(int bit[], int l, int r, int val){
    upd(bit, l, val);
    upd(bit, r + 1, -val);
}
int query(int bit[], int x){
    int res = 0;
    for(; x; x -= x & -x){
        res += bit[x];
    }
    return res;
}
int lca(int x, int y){
    if(h[x] < h[y]) swap(x, y);
    int dist = h[x] - h[y];
    for(int i = 0; i < 18; i++){
        if(dist & (1 << i)) x = par[i][x];
    }
    if(x == y) return x;
    for(int i = 17; i >= 0; i--){
        if(par[i][x] != par[i][y]) x = par[i][x], y = par[i][y];
    }
    return par[0][x];
}
int get(int bit[], int x, int y){
    if(h[x] > h[y]) swap(x, y);
    int u = lca(x, y);
    if(x == u){
        return query(bit, num[y]) - query(bit, num[x]);
    } else{
        return query(bit, num[x]) + query(bit, num[y]) - 2 * query(bit, num[u]);
    }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen(task".inp", "r", stdin);
    //freopen(task".out", "w", stdout);
    cin >> n >> m >> q;
    for(int i = 1, x, y; i < n; i++){
        cin >> x >> y;
        adj[x].pb(y), adj[y].pb(x);
        edges[i] = {x, y};
    }
    dfs(1, 1);
    for(int i = 1; i <= n; i++){
        if(h[edges[i].first] > h[edges[i].second]) swap(edges[i].first, edges[i].second);
    }
    for(int i = 1; i <= m; i++){
        int x, y; cin >> x >> y;
        b[i] = {edges[x].second, y};
    }
    sort(b + 1, b + m + 1, [](pii x, pii y){return x.second < y.second;});

    for(int i = 1; i <= q; i++){
        cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3];
        // start end gold silver
        lo[i] = 1, hi[i] = m, ans[i] = -1;
    }
    for(int i = 1; i <= m; i++){
        upd(bit2, num[b[i].first], tail[b[i].first], 1);
    }
    for(int i = 1; i <= q; i++){
        int cur2 = get(bit2, a[i][0], a[i][1]);
        if(cur2 <= a[i][2]) ans[i] = a[i][2] - cur2;
    }
    for(int i = 1; i <= m; i++){
        upd(bit2, num[b[i].first], tail[b[i].first], -1);
    }
    // cout << endl;
    // for(int i = 1; i <= m; i++){
    //     cout << b[i].first << " " << b[i].second << endl;
    // }
    // cout << endl;

    for(int phase = 0; phase < 18; phase++){
        for(int j = 1; j <= m; j++){
            pos[j].clear();       
        }
        for(int j = 1; j <= q; j++){
            if(lo[j] > hi[j]) continue;
            int mid = (lo[j] + hi[j]) / 2;
            pos[mid].pb(j);
        }
        for(int i = 1; i <= m; i++){
            upd(bit2, num[b[i].first], tail[b[i].first], 1);
        }
        for(int i = 1; i <= m; i++){
            upd(bit1, num[b[i].first], tail[b[i].first], b[i].second);
            upd(bit2, num[b[i].first], tail[b[i].first], -1);
            for(int x : pos[i]){
                int cur = get(bit1, a[x][0], a[x][1]), cur2 = get(bit2, a[x][0], a[x][1]);
                // cout << i << " " << x << " " << a[x][0] << " " << a[x][1] << " " << cur << endl;
                if(cur <= a[x][3]){
                    lo[x] = i + 1;
                    if(cur2 <= a[x][2]) ans[x] = a[x][2] - cur2;
                } else{
                    hi[x] = i - 1;
                }
            }
        }
        for(int i = 0; i <= n; i++){
            bit1[i] = bit2[i] = 0;
        }
    }
    for(int i = 1; i <= q; i++) cout << ans[i] << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26456 KB Output is correct
2 Correct 5 ms 26460 KB Output is correct
3 Correct 4 ms 26460 KB Output is correct
4 Correct 4 ms 26460 KB Output is correct
5 Correct 11 ms 26972 KB Output is correct
6 Correct 13 ms 26968 KB Output is correct
7 Correct 11 ms 26972 KB Output is correct
8 Correct 13 ms 27048 KB Output is correct
9 Correct 13 ms 27160 KB Output is correct
10 Correct 14 ms 27048 KB Output is correct
11 Correct 13 ms 27080 KB Output is correct
12 Correct 13 ms 26972 KB Output is correct
13 Correct 12 ms 27228 KB Output is correct
14 Correct 12 ms 27228 KB Output is correct
15 Correct 13 ms 27048 KB Output is correct
16 Correct 13 ms 26972 KB Output is correct
17 Correct 14 ms 26972 KB Output is correct
18 Correct 14 ms 27228 KB Output is correct
19 Correct 12 ms 26972 KB Output is correct
20 Correct 12 ms 26972 KB Output is correct
21 Correct 13 ms 26972 KB Output is correct
22 Correct 12 ms 27164 KB Output is correct
23 Correct 11 ms 26972 KB Output is correct
24 Correct 11 ms 27156 KB Output is correct
25 Correct 12 ms 26968 KB Output is correct
26 Correct 10 ms 27116 KB Output is correct
27 Correct 11 ms 26972 KB Output is correct
28 Correct 11 ms 27228 KB Output is correct
29 Correct 11 ms 26972 KB Output is correct
30 Correct 13 ms 27152 KB Output is correct
31 Correct 13 ms 26972 KB Output is correct
32 Correct 13 ms 27176 KB Output is correct
33 Correct 11 ms 27224 KB Output is correct
34 Correct 11 ms 27228 KB Output is correct
35 Correct 11 ms 27228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26712 KB Output is correct
2 Correct 14 ms 26972 KB Output is correct
3 Correct 13 ms 27208 KB Output is correct
4 Correct 13 ms 26972 KB Output is correct
5 Correct 1612 ms 50524 KB Output is correct
6 Correct 1200 ms 55576 KB Output is correct
7 Correct 1679 ms 54348 KB Output is correct
8 Correct 1385 ms 49764 KB Output is correct
9 Correct 1239 ms 49124 KB Output is correct
10 Correct 2481 ms 59048 KB Output is correct
11 Correct 2312 ms 59044 KB Output is correct
12 Correct 2567 ms 58716 KB Output is correct
13 Correct 2262 ms 58516 KB Output is correct
14 Correct 2510 ms 59244 KB Output is correct
15 Correct 1542 ms 64704 KB Output is correct
16 Correct 1295 ms 65140 KB Output is correct
17 Correct 1569 ms 64316 KB Output is correct
18 Correct 2328 ms 58916 KB Output is correct
19 Correct 2392 ms 59036 KB Output is correct
20 Correct 2369 ms 58796 KB Output is correct
21 Correct 2550 ms 59148 KB Output is correct
22 Correct 2456 ms 59060 KB Output is correct
23 Correct 2540 ms 58916 KB Output is correct
24 Correct 2615 ms 58944 KB Output is correct
25 Correct 2063 ms 58732 KB Output is correct
26 Correct 2455 ms 58508 KB Output is correct
27 Correct 1107 ms 59564 KB Output is correct
28 Correct 618 ms 57628 KB Output is correct
29 Correct 587 ms 56572 KB Output is correct
30 Correct 1014 ms 56360 KB Output is correct
31 Correct 896 ms 56396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26456 KB Output is correct
2 Correct 11 ms 27228 KB Output is correct
3 Correct 11 ms 27316 KB Output is correct
4 Correct 13 ms 27224 KB Output is correct
5 Correct 620 ms 56032 KB Output is correct
6 Correct 1036 ms 55812 KB Output is correct
7 Correct 612 ms 55304 KB Output is correct
8 Correct 1764 ms 65312 KB Output is correct
9 Correct 1512 ms 65100 KB Output is correct
10 Correct 1167 ms 65124 KB Output is correct
11 Correct 838 ms 65432 KB Output is correct
12 Correct 713 ms 65128 KB Output is correct
13 Correct 665 ms 65220 KB Output is correct
14 Correct 471 ms 64708 KB Output is correct
15 Correct 339 ms 64860 KB Output is correct
16 Correct 804 ms 64972 KB Output is correct
17 Correct 564 ms 64956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26456 KB Output is correct
2 Correct 5 ms 26460 KB Output is correct
3 Correct 4 ms 26460 KB Output is correct
4 Correct 4 ms 26460 KB Output is correct
5 Correct 11 ms 26972 KB Output is correct
6 Correct 13 ms 26968 KB Output is correct
7 Correct 11 ms 26972 KB Output is correct
8 Correct 13 ms 27048 KB Output is correct
9 Correct 13 ms 27160 KB Output is correct
10 Correct 14 ms 27048 KB Output is correct
11 Correct 13 ms 27080 KB Output is correct
12 Correct 13 ms 26972 KB Output is correct
13 Correct 12 ms 27228 KB Output is correct
14 Correct 12 ms 27228 KB Output is correct
15 Correct 13 ms 27048 KB Output is correct
16 Correct 13 ms 26972 KB Output is correct
17 Correct 14 ms 26972 KB Output is correct
18 Correct 14 ms 27228 KB Output is correct
19 Correct 12 ms 26972 KB Output is correct
20 Correct 12 ms 26972 KB Output is correct
21 Correct 13 ms 26972 KB Output is correct
22 Correct 12 ms 27164 KB Output is correct
23 Correct 11 ms 26972 KB Output is correct
24 Correct 11 ms 27156 KB Output is correct
25 Correct 12 ms 26968 KB Output is correct
26 Correct 10 ms 27116 KB Output is correct
27 Correct 11 ms 26972 KB Output is correct
28 Correct 11 ms 27228 KB Output is correct
29 Correct 11 ms 26972 KB Output is correct
30 Correct 13 ms 27152 KB Output is correct
31 Correct 13 ms 26972 KB Output is correct
32 Correct 13 ms 27176 KB Output is correct
33 Correct 11 ms 27224 KB Output is correct
34 Correct 11 ms 27228 KB Output is correct
35 Correct 11 ms 27228 KB Output is correct
36 Correct 4 ms 26712 KB Output is correct
37 Correct 14 ms 26972 KB Output is correct
38 Correct 13 ms 27208 KB Output is correct
39 Correct 13 ms 26972 KB Output is correct
40 Correct 1612 ms 50524 KB Output is correct
41 Correct 1200 ms 55576 KB Output is correct
42 Correct 1679 ms 54348 KB Output is correct
43 Correct 1385 ms 49764 KB Output is correct
44 Correct 1239 ms 49124 KB Output is correct
45 Correct 2481 ms 59048 KB Output is correct
46 Correct 2312 ms 59044 KB Output is correct
47 Correct 2567 ms 58716 KB Output is correct
48 Correct 2262 ms 58516 KB Output is correct
49 Correct 2510 ms 59244 KB Output is correct
50 Correct 1542 ms 64704 KB Output is correct
51 Correct 1295 ms 65140 KB Output is correct
52 Correct 1569 ms 64316 KB Output is correct
53 Correct 2328 ms 58916 KB Output is correct
54 Correct 2392 ms 59036 KB Output is correct
55 Correct 2369 ms 58796 KB Output is correct
56 Correct 2550 ms 59148 KB Output is correct
57 Correct 2456 ms 59060 KB Output is correct
58 Correct 2540 ms 58916 KB Output is correct
59 Correct 2615 ms 58944 KB Output is correct
60 Correct 2063 ms 58732 KB Output is correct
61 Correct 2455 ms 58508 KB Output is correct
62 Correct 1107 ms 59564 KB Output is correct
63 Correct 618 ms 57628 KB Output is correct
64 Correct 587 ms 56572 KB Output is correct
65 Correct 1014 ms 56360 KB Output is correct
66 Correct 896 ms 56396 KB Output is correct
67 Correct 4 ms 26456 KB Output is correct
68 Correct 11 ms 27228 KB Output is correct
69 Correct 11 ms 27316 KB Output is correct
70 Correct 13 ms 27224 KB Output is correct
71 Correct 620 ms 56032 KB Output is correct
72 Correct 1036 ms 55812 KB Output is correct
73 Correct 612 ms 55304 KB Output is correct
74 Correct 1764 ms 65312 KB Output is correct
75 Correct 1512 ms 65100 KB Output is correct
76 Correct 1167 ms 65124 KB Output is correct
77 Correct 838 ms 65432 KB Output is correct
78 Correct 713 ms 65128 KB Output is correct
79 Correct 665 ms 65220 KB Output is correct
80 Correct 471 ms 64708 KB Output is correct
81 Correct 339 ms 64860 KB Output is correct
82 Correct 804 ms 64972 KB Output is correct
83 Correct 564 ms 64956 KB Output is correct
84 Correct 1815 ms 49976 KB Output is correct
85 Correct 1120 ms 49872 KB Output is correct
86 Correct 1377 ms 49040 KB Output is correct
87 Correct 2487 ms 59004 KB Output is correct
88 Correct 3101 ms 58656 KB Output is correct
89 Correct 3214 ms 58688 KB Output is correct
90 Correct 3150 ms 58924 KB Output is correct
91 Correct 3340 ms 58892 KB Output is correct
92 Correct 2490 ms 62852 KB Output is correct
93 Correct 2452 ms 64104 KB Output is correct
94 Correct 2439 ms 58700 KB Output is correct
95 Correct 3331 ms 58896 KB Output is correct
96 Correct 3266 ms 58664 KB Output is correct
97 Correct 2615 ms 58724 KB Output is correct
98 Correct 3547 ms 58980 KB Output is correct
99 Correct 3716 ms 58652 KB Output is correct
100 Correct 3754 ms 59212 KB Output is correct
101 Correct 3494 ms 59232 KB Output is correct
102 Correct 1583 ms 59256 KB Output is correct
103 Correct 1990 ms 59396 KB Output is correct
104 Correct 2333 ms 59220 KB Output is correct
105 Correct 602 ms 56072 KB Output is correct
106 Correct 637 ms 57820 KB Output is correct
107 Correct 968 ms 55832 KB Output is correct
108 Correct 1133 ms 55844 KB Output is correct