Submission #102390

# Submission time Handle Problem Language Result Execution time Memory
102390 2019-03-24T15:22:16 Z Alexa2001 Election Campaign (JOI15_election_campaign) C++17
100 / 100
416 ms 28800 KB
#include <bits/stdc++.h>

using namespace std;

/// 17:03

const int Nmax = 1e5 + 5, lg = 18;
typedef long long ll;

int n, t[lg+2][Nmax], level[Nmax], In[Nmax], Out[Nmax], tmp;
ll sum[Nmax], dp[Nmax];

struct Plan
{
    int x, y, c;
};
vector<int> v[Nmax];
vector<Plan> plan[Nmax];



class AIB
{
    ll a[Nmax];
    int ub(int x) { return (x&(-x)); }

public:
    void update(int x, int y, ll val)
    {
        for(; x<=n; x+=ub(x)) a[x] += val;
        for(++y; y<=n; y+=ub(y)) a[y] -= val;
    }

    ll query(int x)
    {
        ll ans = 0;
        for(; x; x-=ub(x)) ans += a[x];
        return ans;
    }
} aib;


void dfs(int node)
{
    int i;
    for(i=1; i<=lg; ++i) t[i][node] = t[i-1][t[i-1][node]];

    In[node] = ++tmp;
    level[node] = level[t[0][node]] + 1;

    for(auto it : v[node])
        if(!level[it])
        {
            t[0][it] = node;
            dfs(it);
        }

    Out[node] = tmp;
}

int lca(int x, int y)
{
    if(level[x] < level[y]) swap(x, y);
    int up = level[x] - level[y], i;
    for(i=0; i<=lg; ++i)
        if(up & (1<<i)) x = t[i][x];

    if(x == y) return x;

    for(i=lg; i>=0; --i)
        if(t[i][x] != t[i][y]) x = t[i][x], y = t[i][y];
    return t[0][x];
}

void solve(int node)
{
    for(auto it : v[node])
        if(it != t[0][node])
        {
            solve(it);
            sum[node] += dp[it];
        }

    dp[node] = sum[node];

    for(auto it : plan[node])
    {
        ll now = aib.query(In[it.x]) + aib.query(In[it.y]) + sum[node] + it.c;
        dp[node] = max(dp[node], now);
    }

    aib.update(In[node], Out[node], sum[node] - dp[node]);
}


int main()
{
  //  freopen("input", "r", stdin);
    cin.sync_with_stdio(false);

    int i, x, y, m;

    cin >> n;
    for(i=1; i<n; ++i)
    {
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    dfs(1);

    cin >> m;
    for(i=1; i<=m; ++i)
    {
        int money;
        cin >> x >> y >> money;
        plan[lca(x,y)].push_back({x, y, money});
    }

    solve(1);
    cout << dp[1] << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 9 ms 5376 KB Output is correct
5 Correct 240 ms 20472 KB Output is correct
6 Correct 63 ms 24952 KB Output is correct
7 Correct 172 ms 23480 KB Output is correct
8 Correct 179 ms 20080 KB Output is correct
9 Correct 218 ms 22608 KB Output is correct
10 Correct 176 ms 20028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 8 ms 5348 KB Output is correct
4 Correct 263 ms 28244 KB Output is correct
5 Correct 153 ms 28380 KB Output is correct
6 Correct 171 ms 28404 KB Output is correct
7 Correct 195 ms 28280 KB Output is correct
8 Correct 237 ms 28404 KB Output is correct
9 Correct 179 ms 28328 KB Output is correct
10 Correct 239 ms 28280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 8 ms 5348 KB Output is correct
4 Correct 263 ms 28244 KB Output is correct
5 Correct 153 ms 28380 KB Output is correct
6 Correct 171 ms 28404 KB Output is correct
7 Correct 195 ms 28280 KB Output is correct
8 Correct 237 ms 28404 KB Output is correct
9 Correct 179 ms 28328 KB Output is correct
10 Correct 239 ms 28280 KB Output is correct
11 Correct 20 ms 6136 KB Output is correct
12 Correct 221 ms 28664 KB Output is correct
13 Correct 205 ms 28536 KB Output is correct
14 Correct 166 ms 28792 KB Output is correct
15 Correct 193 ms 28800 KB Output is correct
16 Correct 155 ms 28636 KB Output is correct
17 Correct 181 ms 28600 KB Output is correct
18 Correct 206 ms 28704 KB Output is correct
19 Correct 142 ms 28704 KB Output is correct
20 Correct 161 ms 28696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 286 ms 23232 KB Output is correct
2 Correct 199 ms 28408 KB Output is correct
3 Correct 416 ms 26484 KB Output is correct
4 Correct 241 ms 22960 KB Output is correct
5 Correct 331 ms 26116 KB Output is correct
6 Correct 246 ms 22840 KB Output is correct
7 Correct 362 ms 25908 KB Output is correct
8 Correct 233 ms 23544 KB Output is correct
9 Correct 170 ms 28280 KB Output is correct
10 Correct 363 ms 25332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 9 ms 5376 KB Output is correct
5 Correct 240 ms 20472 KB Output is correct
6 Correct 63 ms 24952 KB Output is correct
7 Correct 172 ms 23480 KB Output is correct
8 Correct 179 ms 20080 KB Output is correct
9 Correct 218 ms 22608 KB Output is correct
10 Correct 176 ms 20028 KB Output is correct
11 Correct 7 ms 5376 KB Output is correct
12 Correct 9 ms 5376 KB Output is correct
13 Correct 8 ms 5376 KB Output is correct
14 Correct 11 ms 5376 KB Output is correct
15 Correct 10 ms 5376 KB Output is correct
16 Correct 9 ms 5376 KB Output is correct
17 Correct 7 ms 5376 KB Output is correct
18 Correct 10 ms 5376 KB Output is correct
19 Correct 8 ms 5376 KB Output is correct
20 Correct 8 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 9 ms 5376 KB Output is correct
5 Correct 240 ms 20472 KB Output is correct
6 Correct 63 ms 24952 KB Output is correct
7 Correct 172 ms 23480 KB Output is correct
8 Correct 179 ms 20080 KB Output is correct
9 Correct 218 ms 22608 KB Output is correct
10 Correct 176 ms 20028 KB Output is correct
11 Correct 9 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 8 ms 5348 KB Output is correct
14 Correct 263 ms 28244 KB Output is correct
15 Correct 153 ms 28380 KB Output is correct
16 Correct 171 ms 28404 KB Output is correct
17 Correct 195 ms 28280 KB Output is correct
18 Correct 237 ms 28404 KB Output is correct
19 Correct 179 ms 28328 KB Output is correct
20 Correct 239 ms 28280 KB Output is correct
21 Correct 20 ms 6136 KB Output is correct
22 Correct 221 ms 28664 KB Output is correct
23 Correct 205 ms 28536 KB Output is correct
24 Correct 166 ms 28792 KB Output is correct
25 Correct 193 ms 28800 KB Output is correct
26 Correct 155 ms 28636 KB Output is correct
27 Correct 181 ms 28600 KB Output is correct
28 Correct 206 ms 28704 KB Output is correct
29 Correct 142 ms 28704 KB Output is correct
30 Correct 161 ms 28696 KB Output is correct
31 Correct 286 ms 23232 KB Output is correct
32 Correct 199 ms 28408 KB Output is correct
33 Correct 416 ms 26484 KB Output is correct
34 Correct 241 ms 22960 KB Output is correct
35 Correct 331 ms 26116 KB Output is correct
36 Correct 246 ms 22840 KB Output is correct
37 Correct 362 ms 25908 KB Output is correct
38 Correct 233 ms 23544 KB Output is correct
39 Correct 170 ms 28280 KB Output is correct
40 Correct 363 ms 25332 KB Output is correct
41 Correct 7 ms 5376 KB Output is correct
42 Correct 9 ms 5376 KB Output is correct
43 Correct 8 ms 5376 KB Output is correct
44 Correct 11 ms 5376 KB Output is correct
45 Correct 10 ms 5376 KB Output is correct
46 Correct 9 ms 5376 KB Output is correct
47 Correct 7 ms 5376 KB Output is correct
48 Correct 10 ms 5376 KB Output is correct
49 Correct 8 ms 5376 KB Output is correct
50 Correct 8 ms 5376 KB Output is correct
51 Correct 328 ms 23940 KB Output is correct
52 Correct 224 ms 28632 KB Output is correct
53 Correct 387 ms 25820 KB Output is correct
54 Correct 249 ms 23220 KB Output is correct
55 Correct 299 ms 23724 KB Output is correct
56 Correct 224 ms 28664 KB Output is correct
57 Correct 354 ms 26276 KB Output is correct
58 Correct 279 ms 23264 KB Output is correct
59 Correct 293 ms 23928 KB Output is correct
60 Correct 205 ms 28664 KB Output is correct
61 Correct 334 ms 26360 KB Output is correct
62 Correct 273 ms 22988 KB Output is correct
63 Correct 273 ms 23392 KB Output is correct
64 Correct 144 ms 28536 KB Output is correct
65 Correct 345 ms 26292 KB Output is correct
66 Correct 210 ms 23308 KB Output is correct
67 Correct 299 ms 23604 KB Output is correct
68 Correct 226 ms 28544 KB Output is correct
69 Correct 328 ms 25432 KB Output is correct
70 Correct 234 ms 23348 KB Output is correct