Submission #398035

# Submission time Handle Problem Language Result Execution time Memory
398035 2021-05-03T14:54:00 Z Charis02 Election Campaign (JOI15_election_campaign) C++14
100 / 100
461 ms 67912 KB
/*

https://oj.uz/problem/view/JOI15_election_campaign

O(NM) Code

*/

#include<iostream>
#include<vector>
#include<string.h>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define MAX_N 200004

using namespace std;

vector < int > graph[MAX_N];
vector < pair < pi,ll > > tours[MAX_N];
ll jump_memo[MAX_N][21];
ll tin[MAX_N],tout[MAX_N],cur_time;
ll dp[MAX_N],S[MAX_N];
ll seg[4*MAX_N];
int n,m;

int jump(int x,int i) // find 2^i -th ancestor of x
{
    if(jump_memo[x][i] != -1)
        return jump_memo[x][i];
    return jump_memo[x][i] = jump(jump(x,i-1),i-1);
}

bool is_ancestor(int x,int y) // returns if x is ancestor of y
{
    return (tin[x] <= tin[y] && tout[x] >= tout[y]);
}

int get_lca(int x,int y)
{
    if(is_ancestor(x,y))
        return x;
    if(is_ancestor(y,x))
        return y;

    for(int i = 20;i >= 0;i--)
    {
        if(!is_ancestor(jump(x,i),y))
            x = jump(x,i);
    }

    return jump(x,0);
}

void dfs(int cur,int par)
{
    tin[cur] = cur_time++;
    jump_memo[cur][0] = par;

    for(int i = 0;i < graph[cur].size();i++)
    {
        int v = graph[cur][i];
        if(v == par)
            continue;
        dfs(v,cur);
    }

    tout[cur] = cur_time++;
    return;
}

void initialize()
{
    memset(jump_memo,-1,sizeof jump_memo);
    memset(dp,-1,sizeof dp);
    dfs(1,1);

    return;
}

void input()
{
    int a,b,c;

    cin >> n;

    for(int i = 0;i < n-1;i++)
    {
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    initialize();
    cin >> m;

    for(int i = 0;i < m;i++)
    {
        cin >> a >> b >> c;
        tours[get_lca(a,b)].push_back(mp(mp(a,b),c));
    }

    return;
}

void update(int low,int high,int pos,int slow,int val)
{
    if(low == high && low == slow)
    {
        seg[pos] = val;
        return;
    }
    if(low>slow||high<slow)
        return;

    int mid = (low+high)/2;

    update(low,mid,pos*2+1,slow,val);
    update(mid+1,high,pos*2+2,slow,val);

    seg[pos] = seg[pos*2+1]+seg[pos*2+2];
    return;
}

ll query(int low,int high,int pos,int slow,int shigh)
{
    if(low >= slow && high <= shigh)
        return seg[pos];
    if(low>shigh || high < slow)
        return 0;

    int mid = (low+high)/2;

    return query(low,mid,pos*2+1,slow,shigh) + query(mid+1,high,pos*2+2,slow,shigh);
}

ll go(int cur,int target)
{
    return query(0,2*n,0,tin[cur],tin[target])+S[target];
}

ll calc(int cur,int par)
{
    if(dp[cur] != -1)
        return dp[cur];

    for(int i = 0;i < graph[cur].size();i++)
    {
        int v = graph[cur][i];
        if(v==par)
            continue;

        S[cur] += calc(v,cur);
    }

    dp[cur] = S[cur];

    for(int i = 0;i < graph[cur].size();i++)
    {
        int v = graph[cur][i];
        if(v==par)
            continue;

        update(0,2*n,0,tin[v],S[cur]-dp[v]);
        update(0,2*n,0,tout[v],-S[cur]+dp[v]);
    }

    for(int i = 0;i < tours[cur].size();i++)
    {
        int x = tours[cur][i].first.first;
        int y = tours[cur][i].first.second;
        int C = tours[cur][i].second;

        dp[cur] = max(dp[cur],go(cur,x)+go(cur,y) - S[cur] + C); // S[cur] is counted twice
    }

    return dp[cur];
}

void solve()
{
    cout << calc(1,1) << endl;
}

int main()
{
    input();
    solve();

    return 0;
}

Compilation message

election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'long long int calc(int, int)':
election_campaign.cpp:147:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:158:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:168:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  168 |     for(int i = 0;i < tours[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 44108 KB Output is correct
2 Correct 21 ms 44092 KB Output is correct
3 Correct 20 ms 44124 KB Output is correct
4 Correct 22 ms 44252 KB Output is correct
5 Correct 221 ms 53828 KB Output is correct
6 Correct 153 ms 61492 KB Output is correct
7 Correct 200 ms 58732 KB Output is correct
8 Correct 169 ms 53492 KB Output is correct
9 Correct 196 ms 57228 KB Output is correct
10 Correct 168 ms 53420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 44068 KB Output is correct
2 Correct 21 ms 44164 KB Output is correct
3 Correct 21 ms 44256 KB Output is correct
4 Correct 280 ms 65076 KB Output is correct
5 Correct 309 ms 67556 KB Output is correct
6 Correct 275 ms 67512 KB Output is correct
7 Correct 285 ms 67516 KB Output is correct
8 Correct 283 ms 67524 KB Output is correct
9 Correct 273 ms 67476 KB Output is correct
10 Correct 292 ms 67520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 44068 KB Output is correct
2 Correct 21 ms 44164 KB Output is correct
3 Correct 21 ms 44256 KB Output is correct
4 Correct 280 ms 65076 KB Output is correct
5 Correct 309 ms 67556 KB Output is correct
6 Correct 275 ms 67512 KB Output is correct
7 Correct 285 ms 67516 KB Output is correct
8 Correct 283 ms 67524 KB Output is correct
9 Correct 273 ms 67476 KB Output is correct
10 Correct 292 ms 67520 KB Output is correct
11 Correct 45 ms 45508 KB Output is correct
12 Correct 292 ms 67780 KB Output is correct
13 Correct 301 ms 67824 KB Output is correct
14 Correct 281 ms 67912 KB Output is correct
15 Correct 292 ms 67780 KB Output is correct
16 Correct 282 ms 67908 KB Output is correct
17 Correct 290 ms 67796 KB Output is correct
18 Correct 296 ms 67772 KB Output is correct
19 Correct 279 ms 67796 KB Output is correct
20 Correct 292 ms 67804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 56816 KB Output is correct
2 Correct 274 ms 65136 KB Output is correct
3 Correct 446 ms 64464 KB Output is correct
4 Correct 339 ms 59176 KB Output is correct
5 Correct 406 ms 63840 KB Output is correct
6 Correct 338 ms 58928 KB Output is correct
7 Correct 437 ms 63524 KB Output is correct
8 Correct 356 ms 59460 KB Output is correct
9 Correct 291 ms 67576 KB Output is correct
10 Correct 429 ms 62452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 44108 KB Output is correct
2 Correct 21 ms 44092 KB Output is correct
3 Correct 20 ms 44124 KB Output is correct
4 Correct 22 ms 44252 KB Output is correct
5 Correct 221 ms 53828 KB Output is correct
6 Correct 153 ms 61492 KB Output is correct
7 Correct 200 ms 58732 KB Output is correct
8 Correct 169 ms 53492 KB Output is correct
9 Correct 196 ms 57228 KB Output is correct
10 Correct 168 ms 53420 KB Output is correct
11 Correct 22 ms 44236 KB Output is correct
12 Correct 21 ms 44236 KB Output is correct
13 Correct 23 ms 44236 KB Output is correct
14 Correct 23 ms 44196 KB Output is correct
15 Correct 24 ms 44236 KB Output is correct
16 Correct 23 ms 44284 KB Output is correct
17 Correct 23 ms 44236 KB Output is correct
18 Correct 23 ms 44288 KB Output is correct
19 Correct 22 ms 44276 KB Output is correct
20 Correct 22 ms 44236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 44108 KB Output is correct
2 Correct 21 ms 44092 KB Output is correct
3 Correct 20 ms 44124 KB Output is correct
4 Correct 22 ms 44252 KB Output is correct
5 Correct 221 ms 53828 KB Output is correct
6 Correct 153 ms 61492 KB Output is correct
7 Correct 200 ms 58732 KB Output is correct
8 Correct 169 ms 53492 KB Output is correct
9 Correct 196 ms 57228 KB Output is correct
10 Correct 168 ms 53420 KB Output is correct
11 Correct 20 ms 44068 KB Output is correct
12 Correct 21 ms 44164 KB Output is correct
13 Correct 21 ms 44256 KB Output is correct
14 Correct 280 ms 65076 KB Output is correct
15 Correct 309 ms 67556 KB Output is correct
16 Correct 275 ms 67512 KB Output is correct
17 Correct 285 ms 67516 KB Output is correct
18 Correct 283 ms 67524 KB Output is correct
19 Correct 273 ms 67476 KB Output is correct
20 Correct 292 ms 67520 KB Output is correct
21 Correct 45 ms 45508 KB Output is correct
22 Correct 292 ms 67780 KB Output is correct
23 Correct 301 ms 67824 KB Output is correct
24 Correct 281 ms 67912 KB Output is correct
25 Correct 292 ms 67780 KB Output is correct
26 Correct 282 ms 67908 KB Output is correct
27 Correct 290 ms 67796 KB Output is correct
28 Correct 296 ms 67772 KB Output is correct
29 Correct 279 ms 67796 KB Output is correct
30 Correct 292 ms 67804 KB Output is correct
31 Correct 360 ms 56816 KB Output is correct
32 Correct 274 ms 65136 KB Output is correct
33 Correct 446 ms 64464 KB Output is correct
34 Correct 339 ms 59176 KB Output is correct
35 Correct 406 ms 63840 KB Output is correct
36 Correct 338 ms 58928 KB Output is correct
37 Correct 437 ms 63524 KB Output is correct
38 Correct 356 ms 59460 KB Output is correct
39 Correct 291 ms 67576 KB Output is correct
40 Correct 429 ms 62452 KB Output is correct
41 Correct 22 ms 44236 KB Output is correct
42 Correct 21 ms 44236 KB Output is correct
43 Correct 23 ms 44236 KB Output is correct
44 Correct 23 ms 44196 KB Output is correct
45 Correct 24 ms 44236 KB Output is correct
46 Correct 23 ms 44284 KB Output is correct
47 Correct 23 ms 44236 KB Output is correct
48 Correct 23 ms 44288 KB Output is correct
49 Correct 22 ms 44276 KB Output is correct
50 Correct 22 ms 44236 KB Output is correct
51 Correct 357 ms 59740 KB Output is correct
52 Correct 291 ms 67780 KB Output is correct
53 Correct 461 ms 62856 KB Output is correct
54 Correct 316 ms 59188 KB Output is correct
55 Correct 377 ms 59536 KB Output is correct
56 Correct 302 ms 67808 KB Output is correct
57 Correct 434 ms 63608 KB Output is correct
58 Correct 341 ms 59156 KB Output is correct
59 Correct 364 ms 59724 KB Output is correct
60 Correct 298 ms 67796 KB Output is correct
61 Correct 418 ms 63828 KB Output is correct
62 Correct 342 ms 58748 KB Output is correct
63 Correct 381 ms 59256 KB Output is correct
64 Correct 298 ms 67772 KB Output is correct
65 Correct 452 ms 63788 KB Output is correct
66 Correct 319 ms 59124 KB Output is correct
67 Correct 386 ms 59392 KB Output is correct
68 Correct 289 ms 67780 KB Output is correct
69 Correct 419 ms 62604 KB Output is correct
70 Correct 344 ms 59120 KB Output is correct