Submission #2330443


Source Code Expand

#include <bits/stdc++.h>
#define ll long long
#define INF 1LL << 60
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<ll,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct edge
{
    int from,to;
    ll cost;
};

struct eda
{
    int to,id;
};

pair<int,int> inc[MAX_N];
vector<edge> es;
vector<eda> G[MAX_N];

template<typename V> class segtree {
public:
    int n,sz;
    vector<V> node;
    vector<int> node_id;
    segtree(vector<V>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1,P(INF,0)),node_id.resize(2*n-1);
        rep(i,sz){
            node[i+n-1] = v[i];
            node_id[i+n-1] = i;
        }
        for(int i=n-2; i>=0; i--){
            if(node[2*i+1] > node[2*i+2]){
                node[i] = node[2*i+2];
                node_id[i] = node_id[2*i+2];
            }else{
                node[i] = node[2*i+1];
                node_id[i] = node_id[2*i+1];
            }
        }
    }
    void update(int k,ll a)
    {
    	k += n-1;
    	node[k].fi += a,node_id[k] = k-(n-1);
    	while(k>0){
    		k = (k-1)/2;
    		if(node[2*k+1] < node[2*k+2]){
                node[k] = node[2*k+1],node_id[k] = node_id[2*k+1];
            }else{
                node[k] = node[2*k+2],node_id[k] = node_id[2*k+2];
            }
    	}
    }
    pair<V,int> query(int a,int b,int k=0,int l=0,int r=-1)
    {
        if(r < 0) r = n;
    	if(r <= a || b <= l){
    		return pair<V,int>(P(INF,0),-1);
    	}
    	if(a <= l && r <= b){
    		return pair<V,int>(node[k],node_id[k]);
    	}else{
    		pair<V,int> vl = query(a,b,2*k+1,l,(l+r)/2);
    		pair<V,int> vr = query(a,b,2*k+2,(l+r)/2,r);
    		return min(vl,vr);
    	}
    }
    void print()
    {
        rep(i,sz){
            pair<V,int> p;
            p = query(i,i+1);
            cout << "st[" << i << "]: " << p.fi << " " << p.se << endl;
        }
    }
};

vector<segtree<P> > vec;
int sz[MAX_N];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,q;
    cin >> n;
    es.resize(n-1);
    vector<P> pr(n-1);
    rep(i,n-1){
        int a,b,c;
        cin >> a >> b >> c;
        --a,--b;
        es[i] = (edge){a,b,c};
        G[a].pb((eda){b,i}),G[b].pb((eda){a,i});
        pr[i] = P(c,i);
        inc[i] = P(0,i);
    }
    segtree<P> seg(pr);
    vec.pb(seg);
    sz[0] = seg.sz;
    cin >> q;
    rep(i,q){
        int a;
        cin >> a;
        if(a == 1){
            int id,d;
            cin >> id >> d;
            --id;
            if(inc[id].fi < 0) continue;
            es[id].cost += d;
            vec[inc[id].fi].update(inc[id].se,d);
        }else{
            int id;
            cin >> id;
            --id;
            if(inc[id].fi < 0){
                cout << "-1\n";
                continue;
            }
            int tmp = inc[id].fi;
            segtree<P>& seg = vec[inc[id].fi];
            auto hoge = seg.query(0,seg.sz);
            P p = hoge.fi;
            seg.update(hoge.se,INF);
            sz[tmp]--;
            inc[p.se].fi = -1;
            cout << p.se+1 << "\n";
            queue<int> q[2];
            vi h[2];
            vector<P> nvec;
            q[0].push(es[p.se].to),q[1].push(es[p.se].from);
            int index[2] = {}, ch = -1;
            set<int> st[2];
            st[0] = {es[p.se].to}, st[1] = {es[p.se].from};
            while(1){
                rep(j,2){
                    bool ok = false;
                    while(!q[j].empty()){
                        int p = q[j].front();
                        for(;index[j]<len(G[p]);index[j]++){
                            auto& e = G[p][index[j]];
                            if(inc[e.id].fi >= 0 && st[j].find(e.to) == st[j].end()){
                                h[j].pb(e.id);
                                q[j].push(e.to);
                                st[j].insert(e.to);
                                ok = true;
                                index[j]++;
                                break;
                            }
                        }
                        if(ok) break;
                        q[j].pop();
                        index[j] = 0;
                    }
                    if(!ok){
                        ch = j;
                        break;
                    }
                }
                if(ch >= 0){
                    break;
                }
            }
            for(int j = 0; j < len(h[ch]); j++){
                seg.update(inc[h[ch][j]].se,INF);
                sz[tmp]--;
                inc[h[ch][j]] = P(len(vec),j);
                nvec.pb(P(es[h[ch][j]].cost,h[ch][j]));
            }
            if(len(nvec)){
                segtree<P> nseg(nvec);
                sz[len(vec)] = len(nvec);
                vec.pb(nseg);
            }
        }
    }
    return 0;
}

Submission Info

Submission Time
Task I - 盆栽
User kopricky
Language C++14 (GCC 5.4.1)
Score 300
Code Size 6253 Byte
Status AC
Exec Time 304 ms
Memory 33020 KB

Judge Result

Set Name Subtask All
Score / Max Score 30 / 30 270 / 270
Status
AC × 17
AC × 50
Set Name Test Cases
Subtask atetubou-challenge1.in, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt
All scrambled_00.txt, scrambled_01.txt, scrambled_02.txt, scrambled_03.txt, scrambled_04.txt, scrambled_05.txt, scrambled_06.txt, scrambled_07.txt, scrambled_08.txt, scrambled_09.txt, scrambled_10.txt, scrambled_11.txt, scrambled_12.txt, scrambled_13.txt, scrambled_14.txt, scrambled_15.txt, scrambled_16.txt, scrambled_17.txt, scrambled_18.txt, scrambled_19.txt, scrambled_20.txt, scrambled_21.txt, scrambled_22.txt, scrambled_23.txt, scrambled_24.txt, scrambled_25.txt, scrambled_26.txt, scrambled_27.txt, scrambled_28.txt, scrambled_29.txt, scrambled_30.txt, scrambled_31.txt, scrambled_32.txt, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt
Case Name Status Exec Time Memory
scrambled_00.txt AC 2 ms 2560 KB
scrambled_01.txt AC 4 ms 2816 KB
scrambled_02.txt AC 4 ms 2816 KB
scrambled_03.txt AC 4 ms 2816 KB
scrambled_04.txt AC 4 ms 2816 KB
scrambled_05.txt AC 4 ms 2816 KB
scrambled_06.txt AC 4 ms 2816 KB
scrambled_07.txt AC 4 ms 2816 KB
scrambled_08.txt AC 4 ms 2816 KB
scrambled_09.txt AC 4 ms 2816 KB
scrambled_10.txt AC 4 ms 2816 KB
scrambled_11.txt AC 256 ms 31460 KB
scrambled_12.txt AC 263 ms 31828 KB
scrambled_13.txt AC 253 ms 31488 KB
scrambled_14.txt AC 246 ms 30932 KB
scrambled_15.txt AC 256 ms 31296 KB
scrambled_16.txt AC 234 ms 30396 KB
scrambled_17.txt AC 240 ms 30680 KB
scrambled_18.txt AC 271 ms 31420 KB
scrambled_19.txt AC 237 ms 30488 KB
scrambled_20.txt AC 235 ms 30040 KB
scrambled_21.txt AC 130 ms 21236 KB
scrambled_22.txt AC 5 ms 2944 KB
scrambled_23.txt AC 4 ms 2944 KB
scrambled_24.txt AC 4 ms 2944 KB
scrambled_25.txt AC 4 ms 2816 KB
scrambled_26.txt AC 4 ms 2816 KB
scrambled_27.txt AC 282 ms 31956 KB
scrambled_28.txt AC 294 ms 32060 KB
scrambled_29.txt AC 304 ms 33020 KB
scrambled_30.txt AC 302 ms 32696 KB
scrambled_31.txt AC 283 ms 31700 KB
scrambled_32.txt AC 2 ms 2560 KB
subtask_00.txt AC 2 ms 2560 KB
subtask_01.txt AC 4 ms 2816 KB
subtask_02.txt AC 4 ms 2816 KB
subtask_03.txt AC 4 ms 2816 KB
subtask_04.txt AC 4 ms 2816 KB
subtask_05.txt AC 4 ms 2816 KB
subtask_06.txt AC 4 ms 2816 KB
subtask_07.txt AC 4 ms 2816 KB
subtask_08.txt AC 4 ms 2816 KB
subtask_09.txt AC 4 ms 2816 KB
subtask_10.txt AC 4 ms 2816 KB
subtask_11.txt AC 4 ms 2816 KB
subtask_12.txt AC 4 ms 2816 KB
subtask_13.txt AC 4 ms 2816 KB
subtask_14.txt AC 4 ms 2816 KB
subtask_15.txt AC 4 ms 2816 KB
subtask_16.txt AC 2 ms 2560 KB