Submission #702849

# Submission time Handle Problem Language Result Execution time Memory
702849 2023-02-25T08:57:25 Z jiahng Magic Tree (CEOI19_magictree) C++14
16 / 100
433 ms 339128 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 1000010
#define INF (ll)1e9
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
#define DEBUG 0


int TC,N,M,K;
int D[maxn],W[maxn];
vi adj[maxn];
int p;
struct node{
    int s,e,m;
    node *l=nullptr,*r=nullptr;
    int val = 0;
    
    node(int ss,int ee){
        s = ss; e = ee; m = (s + e) / 2;
    }
    int qry(int a,int b){
        if (a <= s && e <= b) return val;
        else if (a > e || s > b) return 0;
        else{
            int ans = 0;
            if (l != nullptr) ans += l->qry(a,b);
            if (r != nullptr) ans += r->qry(a,b);
            return ans;
        }
    }
    void upd(int p,int v){
        val += v;
        if (s == e) return;
        else if (p > m){
            if (r == nullptr) r = new node(m+1,e);
            r->upd(p,v);
        }else{
            if (l == nullptr) l = new node(s,m);
            l->upd(p,v);
        }
    }
    int bs(int v){
        if (v == 0) return s;
        if (val < v) return e+1;
        if (s == e) return s;
        if (l == nullptr){
            assert(r != nullptr); return r->bs(v);
        }
        if (l->val < v){
            assert(r != nullptr); return r->bs(v - l->val);
        }
        else return l->bs(v);
    }
}*root[maxn];

set <pi> st[maxn];
void dfs(int x){
    int mx = -1;
    aFOR(i, adj[x]){
        dfs(i);
        if (mx == -1 || st[i].size() > st[mx].size()) mx = i;
    }
    if (mx != -1){
        swap(root[x], root[mx]); st[x].swap(st[mx]);
    }else root[x] = new node(1,K);

    aFOR(i,adj[x]) if (i != mx){
        aFOR(j,st[i]){
            root[x]->upd(j.f, j.s);
            st[x].insert(j);
        }
    }
    int v = root[x]->qry(1,D[x]) + W[x];
    // update everything after D[x] by y -> max(y,v)
    
    int pt = root[x]->bs(v); // earliest point where f(pt) >= v

    int vpt = root[x]->qry(1, pt);
    if (pt < D[x] || pt == 1) return;

    int shiftamt = v - root[x]->qry(1,D[x]);
    root[x]->upd(D[x], shiftamt); // shift up first, then flatten
    if (DEBUG){
        cout << x << ' ' << pt << '\n';
        aFOR(i, st[x]) cout << i.f << ' ' << i.s << '\n';
        cout << "\n\n";
    }
    // flatten such that root[x]->qry(1,pt) - vpt
    while (1){
        auto it = st[x].lower_bound(pi(D[x], -1));
        if (it == st[x].end()) break;
           
        assert(it->f <= pt);
        if (it->f == pt){
            // last one, might need to keep
            st[x].erase(it); if (vpt != v) st[x].insert(pi(pt, vpt - v));
            root[x]->upd(pt, vpt - root[x]->qry(1,pt));
            break;
        }
        root[x]->upd(it->f, -it->s); st[x].erase(it); 
    }
    st[x].insert(pi(D[x], shiftamt));
    
}
void solve(){
    cin >> N >> M >> K;
    FOR(i,2,N){
        cin >> p; adj[p].pb(i);
    }
    int v,d,w;
    FOR(i,1,M){
        cin >> v >> d >> w;
        D[v] = d; W[v] = w;
    }
    root[1] = new node(1,K);
    dfs(1);
    cout << root[1]->qry(1,K);    
}
int32_t main(){
    fast;
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Incorrect 33 ms 70784 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 177 ms 165124 KB Output is correct
2 Correct 121 ms 114964 KB Output is correct
3 Correct 433 ms 339128 KB Output is correct
4 Correct 223 ms 197052 KB Output is correct
5 Correct 330 ms 198940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 37 ms 70996 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 122 ms 86844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Incorrect 33 ms 70784 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 42 ms 74556 KB Output is correct
2 Correct 86 ms 80856 KB Output is correct
3 Correct 76 ms 81032 KB Output is correct
4 Correct 70 ms 81780 KB Output is correct
5 Correct 46 ms 81992 KB Output is correct
6 Correct 71 ms 83996 KB Output is correct
7 Correct 65 ms 85196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Incorrect 33 ms 70784 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Incorrect 33 ms 70784 KB Output isn't correct
3 Halted 0 ms 0 KB -