Submission #733031

# Submission time Handle Problem Language Result Execution time Memory
733031 2023-04-30T03:18:35 Z LittleCube Two Currencies (JOI23_currencies) C++14
100 / 100
1120 ms 52732 KB
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

int N, M, Q, t, child[100005], pre[100005][20], S[100005], T[100005], L[100005];
ll X[100005], Y[100005];
pii io[100005], range[100005];
vector<pii> E[100005];
pll check[100005];

struct BIT
{
    ll bit[100005];
    void init()
    {
        for (int i = 0; i <= N; i++)
            bit[i] = 0;
    }
    void modify(int pos, ll val)
    {
        assert(pos != 0);
        for (; pos <= N; pos += (pos & -pos))
            bit[pos] += val;
    }
    ll query(int pos)
    {
        ll ans = 0;
        for (; pos > 0; pos -= (pos & -pos))
            ans += bit[pos];
        return ans;
    }
} bit;

bool ischild(int r, int c)
{
    if (io[r].F <= io[c].F && io[c].S <= io[r].S)
        return 1;
    return 0;
}

int lca(int u, int v)
{
    int l = u;
    for (int p = 19; p >= 0; p--)
        if (pre[l][p] && !ischild(pre[l][p], v))
            l = pre[l][p];
    if (!ischild(l, v))
        l = pre[l][0];
    return l;
}

void dfs(int u)
{
    io[u].F = ++t;
    for (auto [v, i] : E[u])
        if (v != pre[u][0])
        {
            child[i] = v;
            pre[v][0] = u;
            for (int p = 1; p < 20; p++)
                pre[v][p] = pre[pre[v][p - 1]][p - 1];
            dfs(v);
        }
    io[u].S = t;
}

signed main()
{
    cin >> N >> M >> Q;
    for (int i = 1; i < N; i++)
    {
        int u, v;
        cin >> u >> v;
        E[u].emplace_back(pii(v, i));
        E[v].emplace_back(pii(u, i));
    }
    for (int i = 1; i <= M; i++)
        cin >> check[i].S >> check[i].F;
    sort(check + 1, check + 1 + M);
    dfs(1);
    //cerr << "dfs\n";
    vector<pii> query, nxt;
    for (int i = 1; i <= Q; i++)
    {
        cin >> S[i] >> T[i] >> X[i] >> Y[i];
        L[i] = lca(S[i], T[i]);
        range[i] = pii(0, M);
        query.emplace_back(pii(i, (range[i].F + range[i].S + 1) / 2));
    }
    //cerr << "lca\n";
    int iter = 20;
    while (iter--)
    {
        int qdx = 0;
        bit.init();
        for (int i = 0; i <= M; i++)
        {
            vector<pii> suc, fail;
            if (i)
            {
                bit.modify(io[child[check[i].S]].F, check[i].F);
                bit.modify(io[child[check[i].S]].S + 1, -check[i].F);
            }
            while (qdx < query.size() && query[qdx].S == i)
            {
                int j = query[qdx].F;
                if (bit.query(io[S[j]].F) + bit.query(io[T[j]].F) - 2 * bit.query(io[L[j]].F) <= Y[j])
                {
                    range[j].F = query[qdx].S;
                    suc.emplace_back(pii(j, (range[j].F + range[j].S + 1) / 2));
                }
                else
                {
                    range[j].S = query[qdx].S - 1;
                    fail.emplace_back(pii(j, (range[j].F + range[j].S + 1) / 2));
                }
                //cerr << j << ' ' << range[j].F << ' ' << range[j].S << ' ' << bit.query(io[S[j]].F) + bit.query(io[T[j]].F) - 2 * bit.query(io[L[j]].F) << '\n';
                qdx++;
            }
            for (auto p : fail)
                nxt.emplace_back(p);
            for (auto p : suc)
                nxt.emplace_back(p);
        }
        query.clear();
        query.swap(nxt);
        //cerr << "bs iter " << iter << '\n';
    }
    bit.init();
    int qdx = 0;
    for (int i = 1; i <= M; i++)
    {
        bit.modify(io[child[check[i].S]].F, -1);
        bit.modify(io[child[check[i].S]].S + 1, 1);
    }
    for (int i = 0; i <= M; i++)
    {
        vector<pii> suc, fail;
        if (i)
        {
            bit.modify(io[child[check[i].S]].F, 1);
            bit.modify(io[child[check[i].S]].S + 1, -1);
        }
        while (qdx < query.size() && query[qdx].S == i)
        {
            int j = query[qdx].F;
            X[j] += bit.query(io[S[j]].F) + bit.query(io[T[j]].F) - 2 * bit.query(io[L[j]].F);
            qdx++;
        }
    }
    //cerr << "calc ans\n";
    for (int i = 1; i <= Q; i++)
    {
        //cout << range[i].F << ' ';
        cout << max(-1LL, X[i]) << '\n';
    }
}

Compilation message

currencies.cpp: In function 'void dfs(long long int)':
currencies.cpp:60:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   60 |     for (auto [v, i] : E[u])
      |               ^
currencies.cpp: In function 'int main()':
currencies.cpp:109:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |             while (qdx < query.size() && query[qdx].S == i)
      |                    ~~~~^~~~~~~~~~~~~~
currencies.cpp:149:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |         while (qdx < query.size() && query[qdx].S == i)
      |                ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 9 ms 3404 KB Output is correct
6 Correct 11 ms 3496 KB Output is correct
7 Correct 12 ms 3236 KB Output is correct
8 Correct 12 ms 3492 KB Output is correct
9 Correct 13 ms 3540 KB Output is correct
10 Correct 16 ms 3492 KB Output is correct
11 Correct 11 ms 3492 KB Output is correct
12 Correct 12 ms 3560 KB Output is correct
13 Correct 12 ms 3668 KB Output is correct
14 Correct 13 ms 3668 KB Output is correct
15 Correct 13 ms 3540 KB Output is correct
16 Correct 12 ms 3540 KB Output is correct
17 Correct 18 ms 3608 KB Output is correct
18 Correct 13 ms 3624 KB Output is correct
19 Correct 12 ms 3540 KB Output is correct
20 Correct 12 ms 3488 KB Output is correct
21 Correct 11 ms 3480 KB Output is correct
22 Correct 12 ms 3460 KB Output is correct
23 Correct 11 ms 3540 KB Output is correct
24 Correct 15 ms 3540 KB Output is correct
25 Correct 11 ms 3540 KB Output is correct
26 Correct 11 ms 3564 KB Output is correct
27 Correct 9 ms 3484 KB Output is correct
28 Correct 20 ms 3540 KB Output is correct
29 Correct 14 ms 3524 KB Output is correct
30 Correct 13 ms 3540 KB Output is correct
31 Correct 12 ms 3472 KB Output is correct
32 Correct 16 ms 3556 KB Output is correct
33 Correct 13 ms 3676 KB Output is correct
34 Correct 13 ms 3620 KB Output is correct
35 Correct 13 ms 3616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 14 ms 3552 KB Output is correct
3 Correct 12 ms 3496 KB Output is correct
4 Correct 14 ms 3540 KB Output is correct
5 Correct 605 ms 40080 KB Output is correct
6 Correct 579 ms 36800 KB Output is correct
7 Correct 562 ms 39516 KB Output is correct
8 Correct 455 ms 37628 KB Output is correct
9 Correct 440 ms 35568 KB Output is correct
10 Correct 693 ms 45868 KB Output is correct
11 Correct 745 ms 45832 KB Output is correct
12 Correct 685 ms 45936 KB Output is correct
13 Correct 723 ms 45912 KB Output is correct
14 Correct 687 ms 45888 KB Output is correct
15 Correct 875 ms 52280 KB Output is correct
16 Correct 808 ms 52600 KB Output is correct
17 Correct 854 ms 52012 KB Output is correct
18 Correct 841 ms 46264 KB Output is correct
19 Correct 810 ms 46268 KB Output is correct
20 Correct 757 ms 46608 KB Output is correct
21 Correct 619 ms 44728 KB Output is correct
22 Correct 606 ms 44932 KB Output is correct
23 Correct 608 ms 45004 KB Output is correct
24 Correct 589 ms 45240 KB Output is correct
25 Correct 654 ms 46104 KB Output is correct
26 Correct 629 ms 46176 KB Output is correct
27 Correct 628 ms 46116 KB Output is correct
28 Correct 545 ms 45664 KB Output is correct
29 Correct 544 ms 45972 KB Output is correct
30 Correct 561 ms 45820 KB Output is correct
31 Correct 579 ms 46008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 12 ms 3620 KB Output is correct
3 Correct 12 ms 3676 KB Output is correct
4 Correct 11 ms 3680 KB Output is correct
5 Correct 520 ms 43844 KB Output is correct
6 Correct 520 ms 44448 KB Output is correct
7 Correct 590 ms 36408 KB Output is correct
8 Correct 949 ms 52208 KB Output is correct
9 Correct 862 ms 52228 KB Output is correct
10 Correct 852 ms 52552 KB Output is correct
11 Correct 804 ms 52276 KB Output is correct
12 Correct 874 ms 52644 KB Output is correct
13 Correct 767 ms 52424 KB Output is correct
14 Correct 797 ms 52276 KB Output is correct
15 Correct 723 ms 52092 KB Output is correct
16 Correct 904 ms 52344 KB Output is correct
17 Correct 836 ms 52732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 9 ms 3404 KB Output is correct
6 Correct 11 ms 3496 KB Output is correct
7 Correct 12 ms 3236 KB Output is correct
8 Correct 12 ms 3492 KB Output is correct
9 Correct 13 ms 3540 KB Output is correct
10 Correct 16 ms 3492 KB Output is correct
11 Correct 11 ms 3492 KB Output is correct
12 Correct 12 ms 3560 KB Output is correct
13 Correct 12 ms 3668 KB Output is correct
14 Correct 13 ms 3668 KB Output is correct
15 Correct 13 ms 3540 KB Output is correct
16 Correct 12 ms 3540 KB Output is correct
17 Correct 18 ms 3608 KB Output is correct
18 Correct 13 ms 3624 KB Output is correct
19 Correct 12 ms 3540 KB Output is correct
20 Correct 12 ms 3488 KB Output is correct
21 Correct 11 ms 3480 KB Output is correct
22 Correct 12 ms 3460 KB Output is correct
23 Correct 11 ms 3540 KB Output is correct
24 Correct 15 ms 3540 KB Output is correct
25 Correct 11 ms 3540 KB Output is correct
26 Correct 11 ms 3564 KB Output is correct
27 Correct 9 ms 3484 KB Output is correct
28 Correct 20 ms 3540 KB Output is correct
29 Correct 14 ms 3524 KB Output is correct
30 Correct 13 ms 3540 KB Output is correct
31 Correct 12 ms 3472 KB Output is correct
32 Correct 16 ms 3556 KB Output is correct
33 Correct 13 ms 3676 KB Output is correct
34 Correct 13 ms 3620 KB Output is correct
35 Correct 13 ms 3616 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 14 ms 3552 KB Output is correct
38 Correct 12 ms 3496 KB Output is correct
39 Correct 14 ms 3540 KB Output is correct
40 Correct 605 ms 40080 KB Output is correct
41 Correct 579 ms 36800 KB Output is correct
42 Correct 562 ms 39516 KB Output is correct
43 Correct 455 ms 37628 KB Output is correct
44 Correct 440 ms 35568 KB Output is correct
45 Correct 693 ms 45868 KB Output is correct
46 Correct 745 ms 45832 KB Output is correct
47 Correct 685 ms 45936 KB Output is correct
48 Correct 723 ms 45912 KB Output is correct
49 Correct 687 ms 45888 KB Output is correct
50 Correct 875 ms 52280 KB Output is correct
51 Correct 808 ms 52600 KB Output is correct
52 Correct 854 ms 52012 KB Output is correct
53 Correct 841 ms 46264 KB Output is correct
54 Correct 810 ms 46268 KB Output is correct
55 Correct 757 ms 46608 KB Output is correct
56 Correct 619 ms 44728 KB Output is correct
57 Correct 606 ms 44932 KB Output is correct
58 Correct 608 ms 45004 KB Output is correct
59 Correct 589 ms 45240 KB Output is correct
60 Correct 654 ms 46104 KB Output is correct
61 Correct 629 ms 46176 KB Output is correct
62 Correct 628 ms 46116 KB Output is correct
63 Correct 545 ms 45664 KB Output is correct
64 Correct 544 ms 45972 KB Output is correct
65 Correct 561 ms 45820 KB Output is correct
66 Correct 579 ms 46008 KB Output is correct
67 Correct 2 ms 2644 KB Output is correct
68 Correct 12 ms 3620 KB Output is correct
69 Correct 12 ms 3676 KB Output is correct
70 Correct 11 ms 3680 KB Output is correct
71 Correct 520 ms 43844 KB Output is correct
72 Correct 520 ms 44448 KB Output is correct
73 Correct 590 ms 36408 KB Output is correct
74 Correct 949 ms 52208 KB Output is correct
75 Correct 862 ms 52228 KB Output is correct
76 Correct 852 ms 52552 KB Output is correct
77 Correct 804 ms 52276 KB Output is correct
78 Correct 874 ms 52644 KB Output is correct
79 Correct 767 ms 52424 KB Output is correct
80 Correct 797 ms 52276 KB Output is correct
81 Correct 723 ms 52092 KB Output is correct
82 Correct 904 ms 52344 KB Output is correct
83 Correct 836 ms 52732 KB Output is correct
84 Correct 637 ms 36032 KB Output is correct
85 Correct 615 ms 30932 KB Output is correct
86 Correct 576 ms 30148 KB Output is correct
87 Correct 980 ms 45780 KB Output is correct
88 Correct 1004 ms 45164 KB Output is correct
89 Correct 895 ms 45868 KB Output is correct
90 Correct 1000 ms 45700 KB Output is correct
91 Correct 857 ms 45800 KB Output is correct
92 Correct 1120 ms 50384 KB Output is correct
93 Correct 1107 ms 51684 KB Output is correct
94 Correct 966 ms 46392 KB Output is correct
95 Correct 969 ms 46392 KB Output is correct
96 Correct 944 ms 46292 KB Output is correct
97 Correct 918 ms 46304 KB Output is correct
98 Correct 764 ms 44920 KB Output is correct
99 Correct 757 ms 44872 KB Output is correct
100 Correct 815 ms 44716 KB Output is correct
101 Correct 835 ms 45228 KB Output is correct
102 Correct 819 ms 46132 KB Output is correct
103 Correct 827 ms 46096 KB Output is correct
104 Correct 760 ms 46020 KB Output is correct
105 Correct 747 ms 45528 KB Output is correct
106 Correct 758 ms 45420 KB Output is correct
107 Correct 696 ms 45884 KB Output is correct
108 Correct 654 ms 45476 KB Output is correct