結果

問題 No.1216 灯籠流し/Lanterns
ユーザー mtsdmtsd
提出日時 2020-08-30 15:00:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,256 ms / 4,500 ms
コード長 9,879 bytes
コンパイル時間 2,676 ms
コンパイル使用メモリ 165,296 KB
実行使用メモリ 284,296 KB
最終ジャッジ日時 2023-08-09 12:54:18
合計ジャッジ時間 29,248 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
19,816 KB
testcase_01 AC 17 ms
19,912 KB
testcase_02 AC 18 ms
19,588 KB
testcase_03 AC 18 ms
20,268 KB
testcase_04 AC 50 ms
24,272 KB
testcase_05 AC 1,210 ms
268,132 KB
testcase_06 AC 109 ms
43,692 KB
testcase_07 AC 1,199 ms
271,284 KB
testcase_08 AC 1,082 ms
253,596 KB
testcase_09 AC 508 ms
129,764 KB
testcase_10 AC 544 ms
135,352 KB
testcase_11 AC 234 ms
68,632 KB
testcase_12 AC 47 ms
23,620 KB
testcase_13 AC 263 ms
71,652 KB
testcase_14 AC 81 ms
31,684 KB
testcase_15 AC 120 ms
38,440 KB
testcase_16 AC 122 ms
37,900 KB
testcase_17 AC 445 ms
83,760 KB
testcase_18 AC 130 ms
40,548 KB
testcase_19 AC 550 ms
97,712 KB
testcase_20 AC 258 ms
59,012 KB
testcase_21 AC 150 ms
42,592 KB
testcase_22 AC 455 ms
86,668 KB
testcase_23 AC 72 ms
30,712 KB
testcase_24 AC 71 ms
39,400 KB
testcase_25 AC 48 ms
29,688 KB
testcase_26 AC 327 ms
140,048 KB
testcase_27 AC 119 ms
51,408 KB
testcase_28 AC 86 ms
33,988 KB
testcase_29 AC 177 ms
67,884 KB
testcase_30 AC 261 ms
108,580 KB
testcase_31 AC 452 ms
192,496 KB
testcase_32 AC 244 ms
91,300 KB
testcase_33 AC 77 ms
34,020 KB
testcase_34 AC 1,256 ms
284,296 KB
testcase_35 AC 1,028 ms
104,924 KB
testcase_36 AC 772 ms
103,924 KB
testcase_37 AC 671 ms
265,132 KB
testcase_38 AC 574 ms
182,512 KB
testcase_39 AC 1,142 ms
191,280 KB
testcase_40 AC 968 ms
106,776 KB
testcase_41 AC 993 ms
106,932 KB
testcase_42 AC 1,008 ms
107,064 KB
testcase_43 AC 995 ms
107,196 KB
testcase_44 AC 995 ms
106,764 KB
testcase_45 AC 573 ms
109,856 KB
testcase_46 AC 574 ms
109,604 KB
testcase_47 AC 577 ms
109,472 KB
testcase_48 AC 577 ms
109,604 KB
testcase_49 AC 552 ms
109,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}
template<typename T> class segtree {
private:
    int n,sz;
    vector<T> node;
public:
    void init(const vector<T>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.assign(2*n,0);
        for(int i = 0; i < sz; i++){
            node[i+n] = v[i];
        }
        for(int i=n-1; i>=1; i--){
            node[i] =node[2*i] + node[2*i+1];
        }
    }
    void update(int k,T a){
    	node[k+=n] += a;
    	while(k>>=1){
            node[k] = node[2*k]+node[2*k+1];
    	}
    }
    T query(int a,int b){
        T res1 = 0;
        T res2 = 0;
        a += n, b += n;
        while(a != b){
            if(a & 1) res1 = res1+node[a++];
            if(b & 1) res2 = node[--b]+ res2;
            a >>= 1, b>>= 1;
        }
        return res1+res2;
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};
 
//座標の型, 値の型
template<typename CandidateType, typename ValueType> class RangeTree
{
public:
    static_assert(std::is_integral<CandidateType>::value, "Integral required.");
private:
    using CT = CandidateType;
    using VT = ValueType;
    using pcc = pair<CT, CT>;
    using pci = pair<CT, int>;
    int n, sz;
    vector<segtree<VT> > seg;
    // y座標, x座標
    vector<vector<pcc> > yx;
    // y座標, x座標
    vector<pcc> sorted;
    void update_(int id, const CT x, const CT y, const VT val) {
        id += n-1;
        const int yid = lower_bound(all(yx[id]), pcc(y, x)) - yx[id].begin();
        seg[id].update(yid, val);
        while(id > 0){
            id = (id - 1) / 2;
            const int yid = lower_bound(all(yx[id]), pcc(y, x)) - yx[id].begin();
            seg[id].update(yid, val);
        }
    }
    VT query(const int lxid, const int rxid, const CT ly, const CT ry, const int k, const int l, const int r) {
        if(r <= lxid || rxid <= l) return 0;
        if(lxid <= l && r <= rxid){
            const int lyid = lower_bound(all(yx[k]), pcc(ly, numeric_limits<CT>::min())) - yx[k].begin();
            const int ryid = upper_bound(all(yx[k]), pcc(ry, numeric_limits<CT>::min())) - yx[k].begin();
            return (lyid >= ryid) ? 0 : seg[k].query(lyid, ryid);
        }else{
            return query(lxid, rxid, ly, ry, 2*k+1, l, (l+r)/2) + query(lxid, rxid, ly, ry, 2*k+2, (l+r)/2, r);
        }
    }
public:
    // 座標, 点の値
    RangeTree(const vector<pcc>& cand, const vector<VT>& val) : n(1), sz((int)cand.size()), sorted(sz){
        while(n < sz) n *= 2;
        for(int i = 0; i < sz; ++i){
            sorted[i] = {cand[i].first, i};
        }
        sort(all(sorted), [&](const pcc& a, const pcc& b){
            return (a.first == b.first) ? (cand[a.second].second < cand[b.second].second) : (a.first < b.first);
        });
        yx.resize(2*n-1), seg.resize(2*n-1);
        for(int i = 0; i < sz; ++i){
            yx[i+n-1] = {{sorted[i].second, sorted[i].first}};
            vector<VT> arg = {val[sorted[i].second]};
            seg[i+n-1].init(arg);
            sorted[i].second = cand[sorted[i].second].second;
        }
        for(int i = n-2; i >= 0; --i){
            yx[i].resize((int)yx[2*i+1].size() + (int)yx[2*i+2].size());
            if(yx[i].empty()) continue;
            merge(all(yx[2*i+1]), all(yx[2*i+2]), yx[i].begin(), [&](const pcc& a, const pcc& b){
                return (cand[a.first].second == cand[b.first].second)
                        ? (a.second < b.second) : (cand[a.first].second < cand[b.first].second);
            });
            vector<VT> arg((int)yx[i].size());
            for(int j = 0; j < (int)yx[i].size(); ++j){
                arg[j] = val[yx[i][j].first];
            }
            seg[i].init(arg);
        }
        for(int i = 0; i < 2*n-1; ++i){
            for(pcc& e : yx[i]){
                e.first = cand[e.first].second;
            }
        }
    }
    // 点 (x,y) の更新を行う
    void update(const CT x, const CT y, const VT val){
        const int id = lower_bound(all(sorted), pcc(x, y)) - sorted.begin();
        return update_(id, x, y, val);
    }
    // [lx,rx) × [ly,ry) の長方形領域のクエリに答える
    VT query(const CT lx, const CT ly, const CT rx, const CT ry){
        const int lxid = lower_bound(all(sorted), pcc(lx, numeric_limits<CT>::min())) - sorted.begin();
        const int rxid = upper_bound(all(sorted), pcc(rx, numeric_limits<CT>::min())) - sorted.begin();
        return (lxid >= rxid) ? 0 : query(lxid, rxid, ly, ry, 0, 0, n);
    }
};



vector<vector<pair<int,ll> > > g;

ll parent[17][50000];
ll dep[50000];
void init(int id,int pre){
    for(auto x:g[id]){
        if(x.first == pre)continue;
        dep[x.first] = dep[id] + x.second;
        parent[0][x.first] = id;
        init(x.first,id);
    }
}

vector<pair<ll,int>>add[50000];
vector<pair<ll,int>>del[50000];
vector<pair<ll,int>>question[50000];
vector<pair<ll,ll> > cand[50000];
set<pair<ll,ll> > q[50000];
int leaf[50000];
void cand_init(int id,int pre){
    int mx_size = -1;
    
    for(auto x:g[id]){
        if(x.first == pre)continue;
        cand_init(x.first,id);
        if(mx_size < (int)cand[leaf[x.first]].size()){
            
            mx_size = cand[leaf[x.first]].size();
            leaf[id] = leaf[x.first];
        }
    }
    if(mx_size==-1){
        leaf[id] = id;
    }else{
        for(auto x:g[id]){
            if(x.first==pre)continue;
            if(leaf[id]!=leaf[x.first]){
                for(auto &x:cand[leaf[x.first]]){
                    cand[leaf[id]].push_back(x);
                }
            }
        }
    }
    
    for(auto x:add[id]){
        cand[leaf[id]].push_back(x);
    }
    for(auto x:question[id]){
        cand[leaf[id]].push_back(x);
    }
}
vector<RangeTree<ll,int>> rt;

vector<pair<int,int> > ans;
void ans_dfs(int id,int pre){
    for(auto x:g[id]){
        if(x.first == pre)continue;
        ans_dfs(x.first,id);
        
    }
    for(auto x:g[id]){
        if(x.first == pre)continue;
        if(leaf[x.first]!=leaf[id]){
            for(auto y:q[leaf[x.first]]){
                q[leaf[id]].insert(y);
                rt[leaf[id]].update(y.first,y.second,1);
            }
        }
    }
    // cerr << "answer" << endl;
    // cerr << id << endl;
    // for(auto x:q[leaf[id]]){
    //     cerr << x.first << " " << x.second << endl;
    // }
    for(auto x:add[id]){
        rt[leaf[id]].update(x.first,x.second,1);
        q[leaf[id]].insert(x);
    }
    for(auto x:question[id]){
        ans.push_back({x.second,rt[leaf[id]].query(0,0,x.first+1,x.second+1)});
    }

    for(auto x:del[id]){
        rt[leaf[id]].update(x.first,x.second,-1);
        q[leaf[id]].erase(x);
    }
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,Q;
    cin >> n >> Q;
    g.resize(n);
    rep(i,n-1){
        int a,b;
        ll c;
        cin >> a >> b >> c;
        a--;b--;
        g[a].push_back({b,c});
        g[b].push_back({a,c});
    }
    init(0,-1);
    vector<int> tlist;

    rep(tt,Q){
        int type,V;
        ll T,L;
        cin >> type >> V >> T >> L;
        V--;
        if(type==0){
            add[V].push_back({T+dep[V],tt});
            ll TT = T+dep[V];
            while(V!=0){
                bool ff = 0;
                for(int i=16;i>=0;i--){
                    int P = parent[i][V];
                    if(dep[V]-dep[P] <= L){
                        ff = 1;
                        L -= dep[V]-dep[P];
                        V = P;
                        break;
                    }
                }
                if(!ff){
                    break;
                }
            }
            del[V].push_back({TT,tt});
        }else{
            question[V].push_back({T+dep[V],tt});
        }
    }
    rep(i,50000){
        leaf[i] = -1;
    }
    cand_init(0,-1);
    rep(i,50000){
        vector<int> pp;
        rep(zz,cand[i].size()){
            pp.push_back(0);
        }
        rt.push_back(RangeTree<ll,int>(cand[i],pp));
    }
    // rep(i,n){
    //     cout <<"test:" << i << " " << leaf[i] << endl;
    //     cerr << "cand\n";
    //     for(auto x:cand[i]){
    //         cout << x.first << " " << x.second << endl;
    //     }
    //     cerr << "add\n";
    //     for(auto x:add[i]){
    //         cout << x.first << " " << x.second << endl;
    //     }
    //     cerr << "question\n";
    //     for(auto x:question[i]){
    //         cout << x.first << " " << x.second << endl;
    //     }
    //     cerr << "del\n";
    //     for(auto x:del[i]){
    //         cout << x.first << " " << x.second << endl;
    //     }
    // }
    ans_dfs(0,-1);
    sort(all(ans));
    for(auto x:ans){
        cout << x.second << "\n";
    }
    return 0;
}
0