Submission #672499

# Submission time Handle Problem Language Result Execution time Memory
672499 2022-12-16T10:42:25 Z tamthegod Election Campaign (JOI15_election_campaign) C++14
100 / 100
511 ms 41932 KB
// Make the best become better
// No room for laziness
#include<bits/stdc++.h>
 
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, m;
vector<int> adj[maxN];
struct TPath
{
    int s, t, w;
} a[maxN];
int sz[maxN], p[maxN], depth[maxN];
int chainhead[maxN], chainid[maxN], nChain = 0;
int timein[maxN], Time = 0;
vector<TPath> vc[maxN];
int f[maxN];
struct SegTree
{
    int st[4 * maxN];
    void Init()
    {
        memset(st, 0, sizeof st);
    }
    void update(int id, int l, int r, int pos, int val)
    {
        if(l > pos || r < pos) return;
        if(l == r && l == pos)
        {
            st[id] += val;
            return;
        }
        int mid = (l + r) / 2;
        update(id * 2, l, mid, pos, val);
        update(id * 2 + 1, mid + 1, r, pos, val);
        st[id] = st[id * 2] + st[id * 2 + 1];
    }
    int get(int id, int l, int r, int u, int v)
    {
        if(l > v || r < u) return 0;
        if(l >= u && r <= v) return st[id];
        int mid = (l + r) / 2;
        return get(id * 2, l, mid, u, v) + get(id * 2 + 1, mid + 1, r, u, v);
    }
} tree1, tree2;
void dfs(int u)
{
    sz[u] = 1;
    for(int v : adj[u])
    {
        if(v == p[u]) continue;
        p[v] = u;
        depth[v] = depth[u] + 1;
        dfs(v);
        sz[u] += sz[v];
    }
}
 
void HLD(int u)
{
    if(!chainhead[nChain]) chainhead[nChain] = u;
    chainid[u] = nChain;
    timein[u] = ++Time;
    int bigChild = -1;
    for(int v : adj[u])
    {
        if(v == p[u]) continue;
        if(bigChild == -1 || sz[bigChild] < sz[v]) bigChild = v;
    }
    if(bigChild != -1) HLD(bigChild);
    for(int v : adj[u])
    {
        if(v == p[u] || v == bigChild) continue;
        nChain++;
        HLD(v);
    }
}
int LCA(int u, int v)
{
    while(chainhead[chainid[u]] != chainhead[chainid[v]])
    {
        if(depth[chainhead[chainid[u]]] < depth[chainhead[chainid[v]]]) swap(u, v);
        u = p[chainhead[chainid[u]]];
    }
    if(depth[u] > depth[v]) swap(u, v);
    return u;
}
int Query(int u, int v)
{
    int res = 0;
    while(chainhead[chainid[u]] != chainhead[chainid[v]])
    {
        if(depth[chainhead[chainid[u]]] < depth[chainhead[chainid[v]]]) swap(u, v);
        res += tree1.get(1, 1, n, timein[chainhead[chainid[u]]], timein[u]);
        u = p[chainhead[chainid[u]]];
    }
    if(depth[u] > depth[v]) swap(u, v);
    res += tree1.get(1, 1, n, timein[u], timein[v]);
    return res;
}
int Query1(int u, int v)
{
    int res = 0;
    while(chainhead[chainid[u]] != chainhead[chainid[v]])
    {
        if(depth[chainhead[chainid[u]]] < depth[chainhead[chainid[v]]]) swap(u, v);
        res += tree2.get(1, 1, n, timein[chainhead[chainid[u]]], timein[u]);
        u = p[chainhead[chainid[u]]];
    }
    if(depth[u] > depth[v]) swap(u, v);
    res += tree2.get(1, 1, n, timein[u], timein[v]);
    return res;
}
void ReadInput()
{
    cin >> n;
    for(int i=1; i<n; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    cin >> m;
    for(int i=1; i<=m; i++)
    {
        cin >> a[i].s >> a[i].t >> a[i].w;
    }
}
void DFS(int u)
{
    int sum = 0;
    for(int v : adj[u])
    {
        if(v == p[u]) continue;
        DFS(v);
        sum += f[v];
    }
    f[u] = sum;
    for(int v : adj[u])
    {
        if(v == p[u]) continue;
        tree1.update(1, 1, n, timein[u], f[v]);
    }
    for(TPath a : vc[u])
    {
        f[u] = max(f[u], Query(a.s, a.t) - Query1(a.s, a.t) + a.w);
    }
    tree2.update(1, 1, n, timein[u], f[u]);
}
void Solve()
{
    dfs(1);
    HLD(1);
    tree1.Init();
    tree2.Init();
    for(int i=1; i<=m; i++)
       vc[LCA(a[i].s, a[i].t)].pb(a[i]);
    DFS(1);
    cout << f[1];
}
int32_t main()
{
    //freopen("x.inp", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11316 KB Output is correct
2 Correct 6 ms 11220 KB Output is correct
3 Correct 6 ms 11220 KB Output is correct
4 Correct 7 ms 11348 KB Output is correct
5 Correct 116 ms 21380 KB Output is correct
6 Correct 57 ms 34280 KB Output is correct
7 Correct 88 ms 30128 KB Output is correct
8 Correct 65 ms 21980 KB Output is correct
9 Correct 95 ms 27276 KB Output is correct
10 Correct 67 ms 21940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11328 KB Output is correct
2 Correct 6 ms 11220 KB Output is correct
3 Correct 7 ms 11572 KB Output is correct
4 Correct 153 ms 41516 KB Output is correct
5 Correct 140 ms 41580 KB Output is correct
6 Correct 119 ms 41528 KB Output is correct
7 Correct 138 ms 41460 KB Output is correct
8 Correct 138 ms 41532 KB Output is correct
9 Correct 130 ms 41624 KB Output is correct
10 Correct 165 ms 41484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11328 KB Output is correct
2 Correct 6 ms 11220 KB Output is correct
3 Correct 7 ms 11572 KB Output is correct
4 Correct 153 ms 41516 KB Output is correct
5 Correct 140 ms 41580 KB Output is correct
6 Correct 119 ms 41528 KB Output is correct
7 Correct 138 ms 41460 KB Output is correct
8 Correct 138 ms 41532 KB Output is correct
9 Correct 130 ms 41624 KB Output is correct
10 Correct 165 ms 41484 KB Output is correct
11 Correct 17 ms 13248 KB Output is correct
12 Correct 136 ms 41784 KB Output is correct
13 Correct 141 ms 41812 KB Output is correct
14 Correct 144 ms 41828 KB Output is correct
15 Correct 139 ms 41832 KB Output is correct
16 Correct 136 ms 41932 KB Output is correct
17 Correct 157 ms 41796 KB Output is correct
18 Correct 137 ms 41792 KB Output is correct
19 Correct 128 ms 41904 KB Output is correct
20 Correct 145 ms 41912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 437 ms 27940 KB Output is correct
2 Correct 124 ms 41532 KB Output is correct
3 Correct 244 ms 36636 KB Output is correct
4 Correct 217 ms 28800 KB Output is correct
5 Correct 212 ms 35584 KB Output is correct
6 Correct 210 ms 28476 KB Output is correct
7 Correct 226 ms 35364 KB Output is correct
8 Correct 289 ms 28208 KB Output is correct
9 Correct 119 ms 41548 KB Output is correct
10 Correct 253 ms 33436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11316 KB Output is correct
2 Correct 6 ms 11220 KB Output is correct
3 Correct 6 ms 11220 KB Output is correct
4 Correct 7 ms 11348 KB Output is correct
5 Correct 116 ms 21380 KB Output is correct
6 Correct 57 ms 34280 KB Output is correct
7 Correct 88 ms 30128 KB Output is correct
8 Correct 65 ms 21980 KB Output is correct
9 Correct 95 ms 27276 KB Output is correct
10 Correct 67 ms 21940 KB Output is correct
11 Correct 7 ms 11476 KB Output is correct
12 Correct 6 ms 11604 KB Output is correct
13 Correct 6 ms 11476 KB Output is correct
14 Correct 6 ms 11488 KB Output is correct
15 Correct 7 ms 11476 KB Output is correct
16 Correct 8 ms 11476 KB Output is correct
17 Correct 6 ms 11444 KB Output is correct
18 Correct 6 ms 11476 KB Output is correct
19 Correct 7 ms 11452 KB Output is correct
20 Correct 6 ms 11604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11316 KB Output is correct
2 Correct 6 ms 11220 KB Output is correct
3 Correct 6 ms 11220 KB Output is correct
4 Correct 7 ms 11348 KB Output is correct
5 Correct 116 ms 21380 KB Output is correct
6 Correct 57 ms 34280 KB Output is correct
7 Correct 88 ms 30128 KB Output is correct
8 Correct 65 ms 21980 KB Output is correct
9 Correct 95 ms 27276 KB Output is correct
10 Correct 67 ms 21940 KB Output is correct
11 Correct 6 ms 11328 KB Output is correct
12 Correct 6 ms 11220 KB Output is correct
13 Correct 7 ms 11572 KB Output is correct
14 Correct 153 ms 41516 KB Output is correct
15 Correct 140 ms 41580 KB Output is correct
16 Correct 119 ms 41528 KB Output is correct
17 Correct 138 ms 41460 KB Output is correct
18 Correct 138 ms 41532 KB Output is correct
19 Correct 130 ms 41624 KB Output is correct
20 Correct 165 ms 41484 KB Output is correct
21 Correct 17 ms 13248 KB Output is correct
22 Correct 136 ms 41784 KB Output is correct
23 Correct 141 ms 41812 KB Output is correct
24 Correct 144 ms 41828 KB Output is correct
25 Correct 139 ms 41832 KB Output is correct
26 Correct 136 ms 41932 KB Output is correct
27 Correct 157 ms 41796 KB Output is correct
28 Correct 137 ms 41792 KB Output is correct
29 Correct 128 ms 41904 KB Output is correct
30 Correct 145 ms 41912 KB Output is correct
31 Correct 437 ms 27940 KB Output is correct
32 Correct 124 ms 41532 KB Output is correct
33 Correct 244 ms 36636 KB Output is correct
34 Correct 217 ms 28800 KB Output is correct
35 Correct 212 ms 35584 KB Output is correct
36 Correct 210 ms 28476 KB Output is correct
37 Correct 226 ms 35364 KB Output is correct
38 Correct 289 ms 28208 KB Output is correct
39 Correct 119 ms 41548 KB Output is correct
40 Correct 253 ms 33436 KB Output is correct
41 Correct 7 ms 11476 KB Output is correct
42 Correct 6 ms 11604 KB Output is correct
43 Correct 6 ms 11476 KB Output is correct
44 Correct 6 ms 11488 KB Output is correct
45 Correct 7 ms 11476 KB Output is correct
46 Correct 8 ms 11476 KB Output is correct
47 Correct 6 ms 11444 KB Output is correct
48 Correct 6 ms 11476 KB Output is correct
49 Correct 7 ms 11452 KB Output is correct
50 Correct 6 ms 11604 KB Output is correct
51 Correct 299 ms 28400 KB Output is correct
52 Correct 141 ms 41864 KB Output is correct
53 Correct 232 ms 33964 KB Output is correct
54 Correct 176 ms 28924 KB Output is correct
55 Correct 424 ms 28136 KB Output is correct
56 Correct 170 ms 41736 KB Output is correct
57 Correct 190 ms 34980 KB Output is correct
58 Correct 240 ms 28868 KB Output is correct
59 Correct 256 ms 28384 KB Output is correct
60 Correct 143 ms 41764 KB Output is correct
61 Correct 190 ms 35220 KB Output is correct
62 Correct 218 ms 28452 KB Output is correct
63 Correct 511 ms 27976 KB Output is correct
64 Correct 139 ms 41704 KB Output is correct
65 Correct 220 ms 34976 KB Output is correct
66 Correct 203 ms 28872 KB Output is correct
67 Correct 428 ms 28136 KB Output is correct
68 Correct 145 ms 41788 KB Output is correct
69 Correct 184 ms 32960 KB Output is correct
70 Correct 193 ms 28876 KB Output is correct