Submission #444693

# Submission time Handle Problem Language Result Execution time Memory
444693 2021-07-14T21:05:21 Z NintsiChkhaidze Election Campaign (JOI15_election_campaign) C++14
100 / 100
536 ms 48948 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define ll long long
#define pii pair<int,int>
#define left (node<<1),l,((l+r)>>1)
#define right ((node<<1)|1),((l+r)>>1) + 1,r
#define int ll
using namespace std;
const int N = 100005;
int cnt,a[N],d[25][N],dep[N],dp[N],in[N],out[N],fen[N];
vector <int> v[N];
set <pii> st;
vector <pair<pii,int> > vec[N];
void dfs(int x,int par){
    dep[x] = dep[par] + 1;
    in[x] = ++cnt;
    d[0][x] = par;
    for (int j = 0; j < v[x].size(); j++){
        int to = v[x][j];
        if (to != par) dfs(to,x);
    }
    out[x] = cnt;
}
void upd(int ind,int val){
    while(ind <= N){
        fen[ind] += val;
        ind += (ind&(-ind));
    }
}
int get(int ind){
    int s=0;
    while(ind){
        s += fen[ind];
        ind -= (ind&(-ind));
    }
    return s;
}
void solve(int x){
    int S = 0;
    for (int j = 0; j < v[x].size(); j++){
        int to = v[x][j];
        if (to == d[0][x]) continue;
        solve(to);
        S += dp[to];
    }
    dp[x] = S;
    upd(in[x], S);
    upd(in[x] + 1,-S);
    
    st.clear(); st.insert({N,0});
    for (int j = 0; j < v[x].size(); j++){
        int to = v[x][j];
        if (to == d[0][x]) continue;
        st.insert({in[to],dp[to]});
    }
    
    for (int j = 0; j < vec[x].size(); j++){
        int l = vec[x][j].f.f,r = vec[x][j].f.s;
        int s = vec[x][j].s + S;
        set <pii> :: iterator it;
        
        if (l != x){
            s += get(in[l]);
            it = st.lower_bound({in[l],1e18});
            it--;
            s -= (it->s);
        } 
        
        if (r != x){
            s += get(in[r]);
            it = st.lower_bound({in[r],1e18});
            it--;
            s -= (it->s);
        } 
    
        dp[x] = max(dp[x],s);
    }
    
    for (int j = 0; j < v[x].size(); j++){
        int to = v[x][j];
        if (to == d[0][x]) continue;
        S -= dp[to];
        upd(in[to], S);
        upd(out[to] + 1,-S);
        S += dp[to];
    }
}
int lca(int x,int y){
    if (dep[x] > dep[y]) swap(x,y);
    
    for (int j = 20; j >= 0; j--)
        if (dep[d[j][y]] >= dep[x]) y = d[j][y];
        
    if (x == y) return x;   
    
    for (int j = 20; j >= 0; j--)
        if (d[j][x] != d[j][y]) x = d[j][x],y = d[j][y];
    
    return d[0][x];
}
main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    int n;
    cin>>n;
    for (int i =1; i <n;i++){
        int a,b;
        cin>>a>>b;
        v[a].pb(b),v[b].pb(a);
    }
    dfs(1,1);
    
    for (int i = 1; i <= 20; i++)
        for (int j = 1; j <= n; j++)
            d[i][j] = d[i - 1][d[i - 1][j]];
    
    int q;
    cin>>q;
    while(q--){
        int x,y,z;
        cin>>x>>y>>z;
        int c = lca(x,y);
        vec[c].pb({{x,y},z});
    }
    
    solve(1);
    cout<<dp[1];
}

Compilation message

election_campaign.cpp: In function 'void dfs(long long int, long long int)':
election_campaign.cpp:20:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int j = 0; j < v[x].size(); j++){
      |                     ~~^~~~~~~~~~~~~
election_campaign.cpp: In function 'void solve(long long int)':
election_campaign.cpp:42:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int j = 0; j < v[x].size(); j++){
      |                     ~~^~~~~~~~~~~~~
election_campaign.cpp:53:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for (int j = 0; j < v[x].size(); j++){
      |                     ~~^~~~~~~~~~~~~
election_campaign.cpp:59:23: warning: comparison of integer expressions of different signedness: 'long long 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]
   59 |     for (int j = 0; j < vec[x].size(); j++){
      |                     ~~^~~~~~~~~~~~~~~
election_campaign.cpp:81:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int j = 0; j < v[x].size(); j++){
      |                     ~~^~~~~~~~~~~~~
election_campaign.cpp: At global scope:
election_campaign.cpp:103:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  103 | main (){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5248 KB Output is correct
4 Correct 3 ms 5452 KB Output is correct
5 Correct 111 ms 30444 KB Output is correct
6 Correct 87 ms 43636 KB Output is correct
7 Correct 108 ms 39240 KB Output is correct
8 Correct 109 ms 31116 KB Output is correct
9 Correct 112 ms 36464 KB Output is correct
10 Correct 134 ms 31172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5196 KB Output is correct
3 Correct 4 ms 5580 KB Output is correct
4 Correct 198 ms 47044 KB Output is correct
5 Correct 209 ms 47148 KB Output is correct
6 Correct 260 ms 47120 KB Output is correct
7 Correct 247 ms 47112 KB Output is correct
8 Correct 242 ms 47228 KB Output is correct
9 Correct 208 ms 47124 KB Output is correct
10 Correct 210 ms 47088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5196 KB Output is correct
3 Correct 4 ms 5580 KB Output is correct
4 Correct 198 ms 47044 KB Output is correct
5 Correct 209 ms 47148 KB Output is correct
6 Correct 260 ms 47120 KB Output is correct
7 Correct 247 ms 47112 KB Output is correct
8 Correct 242 ms 47228 KB Output is correct
9 Correct 208 ms 47124 KB Output is correct
10 Correct 210 ms 47088 KB Output is correct
11 Correct 17 ms 6792 KB Output is correct
12 Correct 235 ms 47052 KB Output is correct
13 Correct 248 ms 47136 KB Output is correct
14 Correct 222 ms 47076 KB Output is correct
15 Correct 199 ms 47156 KB Output is correct
16 Correct 208 ms 47192 KB Output is correct
17 Correct 236 ms 47252 KB Output is correct
18 Correct 197 ms 47044 KB Output is correct
19 Correct 216 ms 47152 KB Output is correct
20 Correct 201 ms 47116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 522 ms 33296 KB Output is correct
2 Correct 214 ms 48540 KB Output is correct
3 Correct 435 ms 43388 KB Output is correct
4 Correct 291 ms 35064 KB Output is correct
5 Correct 448 ms 42344 KB Output is correct
6 Correct 298 ms 34992 KB Output is correct
7 Correct 536 ms 42036 KB Output is correct
8 Correct 420 ms 34628 KB Output is correct
9 Correct 235 ms 48468 KB Output is correct
10 Correct 461 ms 40324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5248 KB Output is correct
4 Correct 3 ms 5452 KB Output is correct
5 Correct 111 ms 30444 KB Output is correct
6 Correct 87 ms 43636 KB Output is correct
7 Correct 108 ms 39240 KB Output is correct
8 Correct 109 ms 31116 KB Output is correct
9 Correct 112 ms 36464 KB Output is correct
10 Correct 134 ms 31172 KB Output is correct
11 Correct 5 ms 5452 KB Output is correct
12 Correct 5 ms 5548 KB Output is correct
13 Correct 5 ms 5452 KB Output is correct
14 Correct 5 ms 5452 KB Output is correct
15 Correct 5 ms 5416 KB Output is correct
16 Correct 5 ms 5452 KB Output is correct
17 Correct 5 ms 5452 KB Output is correct
18 Correct 5 ms 5452 KB Output is correct
19 Correct 5 ms 5488 KB Output is correct
20 Correct 5 ms 5580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5248 KB Output is correct
4 Correct 3 ms 5452 KB Output is correct
5 Correct 111 ms 30444 KB Output is correct
6 Correct 87 ms 43636 KB Output is correct
7 Correct 108 ms 39240 KB Output is correct
8 Correct 109 ms 31116 KB Output is correct
9 Correct 112 ms 36464 KB Output is correct
10 Correct 134 ms 31172 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5196 KB Output is correct
13 Correct 4 ms 5580 KB Output is correct
14 Correct 198 ms 47044 KB Output is correct
15 Correct 209 ms 47148 KB Output is correct
16 Correct 260 ms 47120 KB Output is correct
17 Correct 247 ms 47112 KB Output is correct
18 Correct 242 ms 47228 KB Output is correct
19 Correct 208 ms 47124 KB Output is correct
20 Correct 210 ms 47088 KB Output is correct
21 Correct 17 ms 6792 KB Output is correct
22 Correct 235 ms 47052 KB Output is correct
23 Correct 248 ms 47136 KB Output is correct
24 Correct 222 ms 47076 KB Output is correct
25 Correct 199 ms 47156 KB Output is correct
26 Correct 208 ms 47192 KB Output is correct
27 Correct 236 ms 47252 KB Output is correct
28 Correct 197 ms 47044 KB Output is correct
29 Correct 216 ms 47152 KB Output is correct
30 Correct 201 ms 47116 KB Output is correct
31 Correct 522 ms 33296 KB Output is correct
32 Correct 214 ms 48540 KB Output is correct
33 Correct 435 ms 43388 KB Output is correct
34 Correct 291 ms 35064 KB Output is correct
35 Correct 448 ms 42344 KB Output is correct
36 Correct 298 ms 34992 KB Output is correct
37 Correct 536 ms 42036 KB Output is correct
38 Correct 420 ms 34628 KB Output is correct
39 Correct 235 ms 48468 KB Output is correct
40 Correct 461 ms 40324 KB Output is correct
41 Correct 5 ms 5452 KB Output is correct
42 Correct 5 ms 5548 KB Output is correct
43 Correct 5 ms 5452 KB Output is correct
44 Correct 5 ms 5452 KB Output is correct
45 Correct 5 ms 5416 KB Output is correct
46 Correct 5 ms 5452 KB Output is correct
47 Correct 5 ms 5452 KB Output is correct
48 Correct 5 ms 5452 KB Output is correct
49 Correct 5 ms 5488 KB Output is correct
50 Correct 5 ms 5580 KB Output is correct
51 Correct 451 ms 34916 KB Output is correct
52 Correct 216 ms 48788 KB Output is correct
53 Correct 494 ms 40820 KB Output is correct
54 Correct 362 ms 35216 KB Output is correct
55 Correct 446 ms 35068 KB Output is correct
56 Correct 207 ms 48820 KB Output is correct
57 Correct 414 ms 41892 KB Output is correct
58 Correct 320 ms 35272 KB Output is correct
59 Correct 408 ms 34964 KB Output is correct
60 Correct 212 ms 48948 KB Output is correct
61 Correct 409 ms 41940 KB Output is correct
62 Correct 294 ms 35096 KB Output is correct
63 Correct 454 ms 34648 KB Output is correct
64 Correct 200 ms 48884 KB Output is correct
65 Correct 462 ms 41812 KB Output is correct
66 Correct 360 ms 35300 KB Output is correct
67 Correct 495 ms 34808 KB Output is correct
68 Correct 234 ms 48768 KB Output is correct
69 Correct 391 ms 39760 KB Output is correct
70 Correct 369 ms 35280 KB Output is correct