답안 #60366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
60366 2018-07-24T02:38:00 Z Benq Election Campaign (JOI15_election_campaign) C++11
100 / 100
516 ms 156316 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;


template<int SZ> struct LCA {
    const int MAXK = 32-__builtin_clz(SZ);
    
    int N, R = 1; // vertices from 1 to N, R = root
    vi edges[SZ];
    int parK[32-__builtin_clz(SZ)][SZ], depth[SZ];
    
    void addEdge(int u, int v) {
        edges[u].pb(v), edges[v].pb(u);
    }
    
    void dfs(int u, int prev){
        parK[0][u] = prev;
        depth[u] = depth[prev]+1;
        for (int v: edges[u]) if (v != prev) dfs(v, u);
    }
    
    void construct() {
        dfs(R, 0);
        FOR(k,1,MAXK) FOR(i,1,N+1)
            parK[k][i] = parK[k-1][parK[k-1][i]];
    }
    
    int lca(int u, int v){
        if (depth[u] < depth[v]) swap(u,v);
        
        F0Rd(k,MAXK) if (depth[u] >= depth[v]+(1<<k))  u = parK[k][u];
        F0Rd(k,MAXK) if (parK[k][u] != parK[k][v]) u = parK[k][u], v = parK[k][v];
        
        if(u != v) u = parK[0][u], v = parK[0][v];
        return u;
    }
    
    int dist(int u, int v) {
        return depth[u]+depth[v]-2*depth[lca(u,v)];
    }
};

LCA<MX> L;

template<class T, int SZ> struct BIT {
    T bit[SZ+1];
    
    BIT() { memset(bit,0,sizeof bit); }
    
    void upd(int k, T val) { // add val to index k
        for( ;k <= SZ; k += (k&-k)) bit[k] += val;
    }
    
    void upd(int l, int r, T val) {
        upd(l,val);
        upd(r+1,-val);
    }
    
    T query(int k) {
        T temp = 0;
        for (;k > 0;k -= (k&-k)) temp += bit[k];
        return temp;
    }
    T query(int l, int r) { return query(r)-query(l-1); } // range query [l,r]
};

BIT<ll,MX> child, path;

int N, nex = 1, rev[MX];
ll val[MX];
vi adj[MX];
vector<array<int,3>> que[MX];

void dfs(int x, int y) {
    pi range; range.f = nex++;
    rev[x] = range.f;
    
    ll cur = 0;
    for (int i: adj[x]) if (i != y) {
        dfs(i,x);
        cur += val[i];
    }
    
    val[x] = cur;
    range.s = nex-1;
    // cout << x << " " << range.f << " " << range.s << "\n";
    
    for (auto a: que[x]) {
        /*cout << "OOH " << x << " " << a[0] << ' ' << a[1] << ' ' << a[2] << ' ' << cur << "\n";
        cout << child.query(a[0])+child.query(a[1]) << "\n";
        cout << -path.query(a[0]) << " " << -path.query(a[1]) << "\n";*/
        val[x] = max(val[x],a[2]+cur
                                +child.query(rev[a[0]])+child.query(rev[a[1]])
                                -path.query(rev[a[0]])-path.query(rev[a[1]]));
    }
    
    // cout << "OH " << x << " " << val[x] << " " << sz(que[x]) << "\n";
    // if (x == 4) cout << "AH " << x << " " << y << " " << val[x] << "\n";
    child.upd(range.f,range.s,cur);
    path.upd(range.f,range.s,val[x]);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N;
    F0R(i,N-1) {
        int x,y; cin >> x >> y;
        adj[x].pb(y), adj[y].pb(x);
        L.addEdge(x,y);
    }
    L.N = N;
    L.construct();
    int M; cin >> M;
    F0R(i,M) {
        int x,y,z; cin >> x >> y >> z;
        // cout << x << " " << y << ' ' << z << ' ' << L.lca(x,y) << "\n";
        que[L.lca(x,y)].pb({x,y,z});
    }
    dfs(1,0);
    cout << val[1];
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8956 KB Output is correct
2 Correct 11 ms 9088 KB Output is correct
3 Correct 10 ms 9140 KB Output is correct
4 Correct 12 ms 9304 KB Output is correct
5 Correct 246 ms 25164 KB Output is correct
6 Correct 140 ms 35292 KB Output is correct
7 Correct 243 ms 35292 KB Output is correct
8 Correct 152 ms 35292 KB Output is correct
9 Correct 220 ms 35292 KB Output is correct
10 Correct 192 ms 35292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 35292 KB Output is correct
2 Correct 13 ms 35292 KB Output is correct
3 Correct 13 ms 35292 KB Output is correct
4 Correct 352 ms 44540 KB Output is correct
5 Correct 233 ms 47064 KB Output is correct
6 Correct 248 ms 49512 KB Output is correct
7 Correct 258 ms 51908 KB Output is correct
8 Correct 264 ms 54324 KB Output is correct
9 Correct 228 ms 56840 KB Output is correct
10 Correct 276 ms 59240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 35292 KB Output is correct
2 Correct 13 ms 35292 KB Output is correct
3 Correct 13 ms 35292 KB Output is correct
4 Correct 352 ms 44540 KB Output is correct
5 Correct 233 ms 47064 KB Output is correct
6 Correct 248 ms 49512 KB Output is correct
7 Correct 258 ms 51908 KB Output is correct
8 Correct 264 ms 54324 KB Output is correct
9 Correct 228 ms 56840 KB Output is correct
10 Correct 276 ms 59240 KB Output is correct
11 Correct 31 ms 59240 KB Output is correct
12 Correct 302 ms 62396 KB Output is correct
13 Correct 276 ms 65216 KB Output is correct
14 Correct 292 ms 67880 KB Output is correct
15 Correct 326 ms 70620 KB Output is correct
16 Correct 286 ms 73412 KB Output is correct
17 Correct 346 ms 76128 KB Output is correct
18 Correct 276 ms 78900 KB Output is correct
19 Correct 240 ms 81552 KB Output is correct
20 Correct 319 ms 84452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 348 ms 84452 KB Output is correct
2 Correct 286 ms 86916 KB Output is correct
3 Correct 509 ms 86916 KB Output is correct
4 Correct 302 ms 86916 KB Output is correct
5 Correct 424 ms 89948 KB Output is correct
6 Correct 261 ms 89948 KB Output is correct
7 Correct 516 ms 94564 KB Output is correct
8 Correct 370 ms 94564 KB Output is correct
9 Correct 213 ms 104152 KB Output is correct
10 Correct 424 ms 104152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8956 KB Output is correct
2 Correct 11 ms 9088 KB Output is correct
3 Correct 10 ms 9140 KB Output is correct
4 Correct 12 ms 9304 KB Output is correct
5 Correct 246 ms 25164 KB Output is correct
6 Correct 140 ms 35292 KB Output is correct
7 Correct 243 ms 35292 KB Output is correct
8 Correct 152 ms 35292 KB Output is correct
9 Correct 220 ms 35292 KB Output is correct
10 Correct 192 ms 35292 KB Output is correct
11 Correct 14 ms 104152 KB Output is correct
12 Correct 13 ms 104152 KB Output is correct
13 Correct 13 ms 104152 KB Output is correct
14 Correct 12 ms 104152 KB Output is correct
15 Correct 14 ms 104152 KB Output is correct
16 Correct 14 ms 104152 KB Output is correct
17 Correct 13 ms 104152 KB Output is correct
18 Correct 17 ms 104152 KB Output is correct
19 Correct 14 ms 104152 KB Output is correct
20 Correct 11 ms 104152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8956 KB Output is correct
2 Correct 11 ms 9088 KB Output is correct
3 Correct 10 ms 9140 KB Output is correct
4 Correct 12 ms 9304 KB Output is correct
5 Correct 246 ms 25164 KB Output is correct
6 Correct 140 ms 35292 KB Output is correct
7 Correct 243 ms 35292 KB Output is correct
8 Correct 152 ms 35292 KB Output is correct
9 Correct 220 ms 35292 KB Output is correct
10 Correct 192 ms 35292 KB Output is correct
11 Correct 14 ms 35292 KB Output is correct
12 Correct 13 ms 35292 KB Output is correct
13 Correct 13 ms 35292 KB Output is correct
14 Correct 352 ms 44540 KB Output is correct
15 Correct 233 ms 47064 KB Output is correct
16 Correct 248 ms 49512 KB Output is correct
17 Correct 258 ms 51908 KB Output is correct
18 Correct 264 ms 54324 KB Output is correct
19 Correct 228 ms 56840 KB Output is correct
20 Correct 276 ms 59240 KB Output is correct
21 Correct 31 ms 59240 KB Output is correct
22 Correct 302 ms 62396 KB Output is correct
23 Correct 276 ms 65216 KB Output is correct
24 Correct 292 ms 67880 KB Output is correct
25 Correct 326 ms 70620 KB Output is correct
26 Correct 286 ms 73412 KB Output is correct
27 Correct 346 ms 76128 KB Output is correct
28 Correct 276 ms 78900 KB Output is correct
29 Correct 240 ms 81552 KB Output is correct
30 Correct 319 ms 84452 KB Output is correct
31 Correct 348 ms 84452 KB Output is correct
32 Correct 286 ms 86916 KB Output is correct
33 Correct 509 ms 86916 KB Output is correct
34 Correct 302 ms 86916 KB Output is correct
35 Correct 424 ms 89948 KB Output is correct
36 Correct 261 ms 89948 KB Output is correct
37 Correct 516 ms 94564 KB Output is correct
38 Correct 370 ms 94564 KB Output is correct
39 Correct 213 ms 104152 KB Output is correct
40 Correct 424 ms 104152 KB Output is correct
41 Correct 14 ms 104152 KB Output is correct
42 Correct 13 ms 104152 KB Output is correct
43 Correct 13 ms 104152 KB Output is correct
44 Correct 12 ms 104152 KB Output is correct
45 Correct 14 ms 104152 KB Output is correct
46 Correct 14 ms 104152 KB Output is correct
47 Correct 13 ms 104152 KB Output is correct
48 Correct 17 ms 104152 KB Output is correct
49 Correct 14 ms 104152 KB Output is correct
50 Correct 11 ms 104152 KB Output is correct
51 Correct 356 ms 104152 KB Output is correct
52 Correct 360 ms 112220 KB Output is correct
53 Correct 510 ms 112220 KB Output is correct
54 Correct 326 ms 112220 KB Output is correct
55 Correct 414 ms 112220 KB Output is correct
56 Correct 306 ms 123312 KB Output is correct
57 Correct 430 ms 123312 KB Output is correct
58 Correct 282 ms 123312 KB Output is correct
59 Correct 389 ms 123312 KB Output is correct
60 Correct 232 ms 134356 KB Output is correct
61 Correct 377 ms 134356 KB Output is correct
62 Correct 270 ms 134356 KB Output is correct
63 Correct 457 ms 134356 KB Output is correct
64 Correct 312 ms 145280 KB Output is correct
65 Correct 470 ms 145280 KB Output is correct
66 Correct 264 ms 145280 KB Output is correct
67 Correct 347 ms 145280 KB Output is correct
68 Correct 264 ms 156316 KB Output is correct
69 Correct 402 ms 156316 KB Output is correct
70 Correct 279 ms 156316 KB Output is correct