답안 #1051309

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1051309 2024-08-10T04:16:32 Z phong Election Campaign (JOI15_election_campaign) C++17
60 / 100
261 ms 45304 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

#define ll long long
//#define int long long
const int nmax = 1e5 + 5, N = 1e6;
const ll oo = 1e9 + 5, base = 311;
const int lg = 17, tx = 26;
const ll mod = 998244353;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;

int n;
vector<int> adj[nmax];
struct node{
    ll x, y, c;
};
vector<node>Q[nmax];
node a[nmax];
int h[nmax], up[nmax][lg + 1], sz[nmax];
void dfs(int u, int p){
    sz[u] = 1;
    for(auto v : adj[u]){
        if(v == p) continue;
        h[v] = h[u] + 1;
        up[v][0] = u;
        for(int j = 1; j <= lg; ++j)up[v][j] = up[up[v][j - 1]][j - 1];
        dfs(v, u);
        sz[u] += sz[v];
    }
}

int lca(int u, int v){
    if(h[u] != h[v]){
        if(h[u] < h[v]) swap(u, v);
        int k = h[u] - h[v];
        for(int j = 0; j <= lg; ++j){
            if(k >> j & 1){
                u = up[u][j];
            }
        }
    }
    if(u == v) return u;
    for(int j = lg; j >= 0; --j){
        if(up[u][j] != up[v][j]){
            u = up[u][j];
            v = up[v][j];
        }
    }
    return up[u][0];
}

int nchain = 1, m = 0, pos[nmax], ind[nmax], head[nmax];
int fly(int u, int a){
    int k = h[u] - h[a] - 1;
    if(k < 0) return -1;
    for(int j = 0; j <= lg; ++j){
        if(k >> j & 1) u = up[u][j];
    }
    return u;
}
void dfs_2(int u, int p){
    if(!head[nchain]) head[nchain] = u;
    pos[u]= ++m;
    ind[u] = nchain;
    int idx = -1;
    for(auto v : adj[u]){
        if(v == p) continue;
        if(idx == -1 || sz[v]> sz[idx]){
            idx = v;
        }
    }
    if(idx != -1) dfs_2(idx, u);
    for(auto v: adj[u]){
        if(v == p || v == idx) continue;
        ++nchain;
        dfs_2(v, u);
    }
}
struct hld1{
    ll tree[nmax << 2];
    void update(int id, int l, int r, int u, ll val){
        if(l > u || r < u) return;
        if(l == r){
            tree[id] = val;
            return;
        }
        int mid = r + l >> 1;
        update(id << 1, l, mid, u, val);
        update(id << 1 | 1, mid + 1, r, u, val);
        tree[id] = tree[id << 1] + tree[id << 1 | 1];
    }
    ll get(int id, int l, int r, int u, int v){
        if(l >= u && r <= v) return tree[id];
        int mid = r + l >> 1;
        if(mid < u) return get(id << 1 | 1, mid + 1, r, u, v);
        if(mid + 1 > v) return get(id << 1, l, mid, u, v);
        return get(id << 1, l, mid, u, v) + get(id << 1| 1, mid + 1, r, u, v);
    }
    ll get(int u, int a){
        int uchain = ind[u], achain = ind[a];
        ll res = 0;
        while(1){
            if(achain == uchain){
                res += get(1, 1, n, pos[a], pos[u]);
                return res;
            }
            res += get(1, 1, n, pos[head[uchain]], pos[u]);
            u = up[u][0];
            uchain = ind[u];
        }
    }
    void update(int u, int  val){
        update(1, 1, n, pos[u], val);
    }
}A[2];
ll f[nmax][2];
void dfs_3(int u, int p){
    ll sum = 0;
    for(auto v : adj[u]){
        if(v == p) continue;
        dfs_3(v, u);
        sum += max(f[v][0], f[v][1]);
    }
    for(auto [x, y, c] : Q[u]){
        int t1 = fly(x, u);
        int t2 = fly(y, u);
       ll cur = 0;
        if(t1 != -1){
            cur += A[0].get(x, t1);
            cur -= A[1].get(x, t1);
        }
        if(t2 != -1){
            cur += A[0].get(y, t2);
            cur -= A[1].get(y, t2);

        }
//        if(u == 5) cout << c << ' ' << cur <<endl;

        cur += sum;

        f[u][1] = max(f[u][1], cur + c);
    }
    f[u][0] = sum;
    A[0].update(u, sum);

    A[1].update(u, max(f[u][1], f[u][0]));
//    cout << u << ' ' << max(f[u][0], f[u][1]) << ' ' << A[0].get(u, u) << endl;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, -1);
    int m;
    cin >>m;
    for(int i = 1; i <= m; ++i){
        cin >> a[i].x >> a[i].y >> a[i].c;
        int lc = lca(a[i].x, a[i].y);
        Q[lc].push_back(a[i]);
    }
    dfs_2(1, -1);
    dfs_3(1, -1);
//    cout << A[0].get(6, 6);
    cout << max(f[1][1], f[1][0]);
}
/*

7
3 4
6 5
2 7
1 5
7 5
4 5
5
4 3 10
5 6 5
2 6 9
7 2 2
1 3 8



*/

Compilation message

election_campaign.cpp: In member function 'void hld1::update(int, int, int, int, long long int)':
election_campaign.cpp:94:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   94 |         int mid = r + l >> 1;
      |                   ~~^~~
election_campaign.cpp: In member function 'long long int hld1::get(int, int, int, int, int)':
election_campaign.cpp:101:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  101 |         int mid = r + l >> 1;
      |                   ~~^~~
election_campaign.cpp: At global scope:
election_campaign.cpp:156:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  156 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5464 KB Output is correct
2 Correct 2 ms 5208 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 49 ms 23960 KB Output is correct
6 Correct 42 ms 37716 KB Output is correct
7 Correct 54 ms 32848 KB Output is correct
8 Correct 43 ms 24404 KB Output is correct
9 Correct 54 ms 30032 KB Output is correct
10 Correct 41 ms 24400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5212 KB Output is correct
2 Correct 2 ms 5164 KB Output is correct
3 Correct 2 ms 5468 KB Output is correct
4 Correct 101 ms 44996 KB Output is correct
5 Correct 109 ms 44848 KB Output is correct
6 Correct 98 ms 44944 KB Output is correct
7 Correct 99 ms 44884 KB Output is correct
8 Correct 99 ms 44880 KB Output is correct
9 Correct 82 ms 44884 KB Output is correct
10 Correct 105 ms 44820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5212 KB Output is correct
2 Correct 2 ms 5164 KB Output is correct
3 Correct 2 ms 5468 KB Output is correct
4 Correct 101 ms 44996 KB Output is correct
5 Correct 109 ms 44848 KB Output is correct
6 Correct 98 ms 44944 KB Output is correct
7 Correct 99 ms 44884 KB Output is correct
8 Correct 99 ms 44880 KB Output is correct
9 Correct 82 ms 44884 KB Output is correct
10 Correct 105 ms 44820 KB Output is correct
11 Correct 9 ms 7260 KB Output is correct
12 Correct 100 ms 45208 KB Output is correct
13 Correct 105 ms 45140 KB Output is correct
14 Correct 83 ms 45136 KB Output is correct
15 Correct 101 ms 45248 KB Output is correct
16 Correct 89 ms 45136 KB Output is correct
17 Correct 101 ms 45140 KB Output is correct
18 Correct 99 ms 45136 KB Output is correct
19 Correct 83 ms 45304 KB Output is correct
20 Correct 103 ms 45140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 261 ms 30656 KB Output is correct
2 Correct 80 ms 44884 KB Output is correct
3 Correct 153 ms 39388 KB Output is correct
4 Correct 101 ms 31436 KB Output is correct
5 Correct 112 ms 38556 KB Output is correct
6 Correct 94 ms 31156 KB Output is correct
7 Correct 157 ms 38156 KB Output is correct
8 Correct 139 ms 30800 KB Output is correct
9 Correct 96 ms 44784 KB Output is correct
10 Correct 152 ms 36228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5464 KB Output is correct
2 Correct 2 ms 5208 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 49 ms 23960 KB Output is correct
6 Correct 42 ms 37716 KB Output is correct
7 Correct 54 ms 32848 KB Output is correct
8 Correct 43 ms 24404 KB Output is correct
9 Correct 54 ms 30032 KB Output is correct
10 Correct 41 ms 24400 KB Output is correct
11 Correct 3 ms 5464 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5468 KB Output is correct
14 Correct 2 ms 5468 KB Output is correct
15 Correct 3 ms 5468 KB Output is correct
16 Correct 2 ms 5468 KB Output is correct
17 Correct 3 ms 5468 KB Output is correct
18 Correct 2 ms 5468 KB Output is correct
19 Correct 2 ms 5468 KB Output is correct
20 Correct 3 ms 5468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5464 KB Output is correct
2 Correct 2 ms 5208 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 49 ms 23960 KB Output is correct
6 Correct 42 ms 37716 KB Output is correct
7 Correct 54 ms 32848 KB Output is correct
8 Correct 43 ms 24404 KB Output is correct
9 Correct 54 ms 30032 KB Output is correct
10 Correct 41 ms 24400 KB Output is correct
11 Correct 1 ms 5212 KB Output is correct
12 Correct 2 ms 5164 KB Output is correct
13 Correct 2 ms 5468 KB Output is correct
14 Correct 101 ms 44996 KB Output is correct
15 Correct 109 ms 44848 KB Output is correct
16 Correct 98 ms 44944 KB Output is correct
17 Correct 99 ms 44884 KB Output is correct
18 Correct 99 ms 44880 KB Output is correct
19 Correct 82 ms 44884 KB Output is correct
20 Correct 105 ms 44820 KB Output is correct
21 Correct 9 ms 7260 KB Output is correct
22 Correct 100 ms 45208 KB Output is correct
23 Correct 105 ms 45140 KB Output is correct
24 Correct 83 ms 45136 KB Output is correct
25 Correct 101 ms 45248 KB Output is correct
26 Correct 89 ms 45136 KB Output is correct
27 Correct 101 ms 45140 KB Output is correct
28 Correct 99 ms 45136 KB Output is correct
29 Correct 83 ms 45304 KB Output is correct
30 Correct 103 ms 45140 KB Output is correct
31 Correct 261 ms 30656 KB Output is correct
32 Correct 80 ms 44884 KB Output is correct
33 Correct 153 ms 39388 KB Output is correct
34 Correct 101 ms 31436 KB Output is correct
35 Correct 112 ms 38556 KB Output is correct
36 Correct 94 ms 31156 KB Output is correct
37 Correct 157 ms 38156 KB Output is correct
38 Correct 139 ms 30800 KB Output is correct
39 Correct 96 ms 44784 KB Output is correct
40 Correct 152 ms 36228 KB Output is correct
41 Correct 3 ms 5464 KB Output is correct
42 Correct 2 ms 5468 KB Output is correct
43 Correct 2 ms 5468 KB Output is correct
44 Correct 2 ms 5468 KB Output is correct
45 Correct 3 ms 5468 KB Output is correct
46 Correct 2 ms 5468 KB Output is correct
47 Correct 3 ms 5468 KB Output is correct
48 Correct 2 ms 5468 KB Output is correct
49 Correct 2 ms 5468 KB Output is correct
50 Correct 3 ms 5468 KB Output is correct
51 Incorrect 140 ms 31128 KB Output isn't correct
52 Halted 0 ms 0 KB -