結果
問題 | No.1216 灯籠流し/Lanterns |
ユーザー | mtsd |
提出日時 | 2020-09-01 23:58:07 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,149 ms / 4,500 ms |
コード長 | 8,051 bytes |
コンパイル時間 | 2,109 ms |
コンパイル使用メモリ | 153,288 KB |
実行使用メモリ | 162,860 KB |
最終ジャッジ日時 | 2024-04-30 23:33:20 |
合計ジャッジ時間 | 28,610 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
7,800 KB |
testcase_01 | AC | 3 ms
7,672 KB |
testcase_02 | AC | 3 ms
7,680 KB |
testcase_03 | AC | 3 ms
7,540 KB |
testcase_04 | AC | 29 ms
13,620 KB |
testcase_05 | AC | 803 ms
127,344 KB |
testcase_06 | AC | 60 ms
22,416 KB |
testcase_07 | AC | 802 ms
126,860 KB |
testcase_08 | AC | 724 ms
119,444 KB |
testcase_09 | AC | 302 ms
62,208 KB |
testcase_10 | AC | 339 ms
65,832 KB |
testcase_11 | AC | 122 ms
30,556 KB |
testcase_12 | AC | 25 ms
13,528 KB |
testcase_13 | AC | 143 ms
33,784 KB |
testcase_14 | AC | 73 ms
25,504 KB |
testcase_15 | AC | 143 ms
39,652 KB |
testcase_16 | AC | 111 ms
32,536 KB |
testcase_17 | AC | 909 ms
138,268 KB |
testcase_18 | AC | 178 ms
42,172 KB |
testcase_19 | AC | 1,111 ms
160,608 KB |
testcase_20 | AC | 413 ms
79,732 KB |
testcase_21 | AC | 192 ms
46,008 KB |
testcase_22 | AC | 889 ms
140,204 KB |
testcase_23 | AC | 46 ms
17,844 KB |
testcase_24 | AC | 70 ms
24,060 KB |
testcase_25 | AC | 33 ms
15,128 KB |
testcase_26 | AC | 520 ms
82,196 KB |
testcase_27 | AC | 117 ms
30,092 KB |
testcase_28 | AC | 63 ms
21,576 KB |
testcase_29 | AC | 188 ms
43,060 KB |
testcase_30 | AC | 337 ms
66,560 KB |
testcase_31 | AC | 778 ms
123,244 KB |
testcase_32 | AC | 283 ms
53,776 KB |
testcase_33 | AC | 58 ms
19,988 KB |
testcase_34 | AC | 876 ms
131,472 KB |
testcase_35 | AC | 899 ms
134,708 KB |
testcase_36 | AC | 736 ms
133,656 KB |
testcase_37 | AC | 777 ms
82,060 KB |
testcase_38 | AC | 991 ms
160,388 KB |
testcase_39 | AC | 699 ms
132,372 KB |
testcase_40 | AC | 710 ms
133,792 KB |
testcase_41 | AC | 709 ms
132,936 KB |
testcase_42 | AC | 687 ms
132,636 KB |
testcase_43 | AC | 709 ms
133,484 KB |
testcase_44 | AC | 698 ms
132,660 KB |
testcase_45 | AC | 1,093 ms
162,432 KB |
testcase_46 | AC | 1,123 ms
162,860 KB |
testcase_47 | AC | 1,106 ms
162,744 KB |
testcase_48 | AC | 1,127 ms
162,716 KB |
testcase_49 | AC | 1,149 ms
162,596 KB |
ソースコード
#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]; int in[50000]; int out[50000]; int te; void init(int id,int pre){ in[id] = te; te++; 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); } out[id] = te; te++; } 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); // rep(i,n){ // cerr << in[i] << " " << out[i] << endl; // } for(int i=1;i<17;i++){ for(int j=0;j<n;j++){ parent[i][j] = parent[i-1][parent[i-1][j]]; } } vector<pair<pair<ll,ll>,int> > p; vector<pair<ll,ll> > cand; cand.push_back({0,0}); rep(tt,Q){ int type,V; ll T,L; cin >> type >> V >> T >> L; V--; if(type==0){ p.push_back({{T+dep[V],in[V]},1});cand.push_back({T+dep[V],in[V]}); ll TT = T+dep[V]; for(int i=16;i>=0;i--){ int P = parent[i][V]; if(dep[V]-dep[P] <= L){ L -= dep[V]-dep[P]; V = P; } } if(V!=0){ p.push_back({{TT,in[parent[0][V]]},-1}); cand.push_back({TT,in[parent[0][V]]}); } }else{ p.push_back({{T+dep[V],V},0}); cand.push_back({T+dep[V],in[V]}); cand.push_back({T+dep[V],out[V]}); } } sort(all(cand)); cand.erase(unique(all(cand)),cand.end()); vector<int> ppp((int)cand.size()); RangeTree<ll,int> RT(cand,ppp); for(auto&x:p){ if(x.second!=0){ RT.update(x.first.first,x.first.second,x.second); // cerr << x.first.first << " " << x.first.second << " " << x.second << endl; }else{ // cerr << x.first.second << endl; // cerr << "Q:" << 0 << " " << in[x.first.second] << " " << x.first.first+1 << " " << out[x.first.second] + 1 << endl; cout << RT.query(0,in[x.first.second],x.first.first+1,out[x.first.second]+1) << "\n"; } } return 0; }