結果
問題 | No.1216 灯籠流し/Lanterns |
ユーザー | 👑 hos.lyric |
提出日時 | 2020-08-30 15:53:42 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,607 bytes |
コンパイル時間 | 1,916 ms |
コンパイル使用メモリ | 137,800 KB |
実行使用メモリ | 146,816 KB |
最終ジャッジ日時 | 2024-11-15 10:20:52 |
合計ジャッジ時間 | 37,603 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 43 ms
19,968 KB |
testcase_05 | AC | 1,048 ms
106,404 KB |
testcase_06 | AC | 59 ms
13,184 KB |
testcase_07 | AC | 1,039 ms
93,776 KB |
testcase_08 | AC | 899 ms
72,192 KB |
testcase_09 | AC | 373 ms
44,288 KB |
testcase_10 | AC | 404 ms
44,032 KB |
testcase_11 | AC | 153 ms
30,080 KB |
testcase_12 | AC | 35 ms
16,368 KB |
testcase_13 | AC | 203 ms
39,268 KB |
testcase_14 | AC | 89 ms
25,280 KB |
testcase_15 | AC | 171 ms
35,196 KB |
testcase_16 | AC | 151 ms
39,232 KB |
testcase_17 | WA | - |
testcase_18 | AC | 203 ms
37,376 KB |
testcase_19 | AC | 1,106 ms
131,864 KB |
testcase_20 | AC | 463 ms
69,756 KB |
testcase_21 | AC | 225 ms
44,080 KB |
testcase_22 | AC | 893 ms
97,152 KB |
testcase_23 | AC | 56 ms
26,252 KB |
testcase_24 | AC | 64 ms
12,672 KB |
testcase_25 | AC | 30 ms
9,388 KB |
testcase_26 | AC | 502 ms
44,672 KB |
testcase_27 | AC | 131 ms
26,972 KB |
testcase_28 | AC | 81 ms
24,824 KB |
testcase_29 | AC | 214 ms
39,132 KB |
testcase_30 | AC | 351 ms
47,104 KB |
testcase_31 | WA | - |
testcase_32 | AC | 340 ms
48,848 KB |
testcase_33 | AC | 71 ms
20,480 KB |
testcase_34 | AC | 1,057 ms
110,484 KB |
testcase_35 | AC | 1,729 ms
127,516 KB |
testcase_36 | AC | 1,246 ms
82,208 KB |
testcase_37 | AC | 1,036 ms
115,736 KB |
testcase_38 | AC | 1,292 ms
114,064 KB |
testcase_39 | AC | 1,278 ms
108,944 KB |
testcase_40 | AC | 1,516 ms
129,248 KB |
testcase_41 | AC | 1,537 ms
129,312 KB |
testcase_42 | AC | 1,533 ms
129,508 KB |
testcase_43 | AC | 1,518 ms
129,640 KB |
testcase_44 | AC | 1,519 ms
129,632 KB |
testcase_45 | AC | 1,380 ms
146,664 KB |
testcase_46 | AC | 1,303 ms
146,816 KB |
testcase_47 | AC | 1,347 ms
146,468 KB |
testcase_48 | AC | 1,312 ms
146,788 KB |
testcase_49 | AC | 1,291 ms
146,020 KB |
ソースコード
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } int N, Q; vector<int> A, B; vector<Int> C; vector<int> TYP, V; vector<Int> T, L; vector<vector<int>> G; vector<int> par, sz, dep; vector<Int> dist; int O; vector<int> ord, head; void dfs0(int u, int p, int ip) { par[u] = p; sz[u] = 1; dep[u] = (p == -1) ? 0 : (dep[p] + 1); dist[u] = (p == -1) ? 0 : (dist[p] + C[ip]); for (const int i : G[u]) { const int v = A[i] ^ B[i] ^ u; if (v != p) { dfs0(v, u, i); sz[u] += sz[v]; } } } void dfs1(int u, int p, int h) { ord[u] = O++; head[u] = h; int vm = -1; for (const int i : G[u]) { const int v = A[i] ^ B[i] ^ u; if (v != p) { if (vm == -1 || sz[vm] < sz[v]) { vm = v; } } } if (vm != -1) { dfs1(vm, u, h); for (const int i : G[u]) { const int v = A[i] ^ B[i] ^ u; if (v != p && v != vm) { dfs1(v, u, v); } } } } int BIT_N; struct BIT { map<int, int> as; void add(int pos, int val) { for (int x = pos; x < BIT_N; x |= x + 1) { as[x] += val; } } int sum(int pos) { int ret = 0; for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) { auto it = as.find(x); if (it != as.end()) { ret += it->second; } } return ret; } }; constexpr int E = 16; int main() { for (; ~scanf("%d%d", &N, &Q); ) { A.resize(N - 1); B.resize(N - 1); C.resize(N - 1); for (int i = 0; i < N - 1; ++i) { scanf("%d%d%lld", &A[i], &B[i], &C[i]); --A[i]; --B[i]; assert(C[i]>0); } TYP.resize(Q); V.resize(Q); T.resize(Q); L.resize(Q); for (int q = 0; q < Q; ++q) { scanf("%d%d%lld%lld", &TYP[q], &V[q], &T[q], &L[q]); --V[q]; } G.assign(N, {}); for (int i = 0; i < N - 1; ++i) { G[A[i]].push_back(i); G[B[i]].push_back(i); } par.resize(N); sz.resize(N); dep.resize(N); dist.resize(N); constexpr int rt = 0; dfs0(rt, -1, -1); O = 0; ord.resize(N); head.resize(N); dfs1(rt, -1, rt); // cerr<<"dist = ";pv(dist.begin(),dist.end()); // cerr<<"ord = ";pv(ord.begin(),ord.end()); // cerr<<"head = ";pv(head.begin(),head.end()); vector<int> lens(N, 0); for (int u = 0; u < N; ++u) { ++lens[head[u]]; } BIT_N = Q; vector<vector<BIT>> bits(N); for (int h = 0; h < N; ++h) { bits[h].assign(lens[h], {}); } auto bAdd = [&](int h, int pos, int key, int val) { // cerr<<"bAdd "<<h<<" "<<pos<<" "<<key<<" "<<val<<endl; for (int x = pos; x < lens[h]; x |= x + 1) { bits[h][x].add(key, val); } }; auto bSum = [&](int h, int pos, int key) { int ret = 0; for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) { ret += bits[h][x].sum(key); } return ret; }; // the path from u to root auto hldAdd = [&](int u, int key, int val) { // cerr<<"hldAdd "<<u<<" "<<key<<" "<<val<<endl; for (; u != -1; ) { const int h = head[u]; // add to [0, ord[u] - ord[h]] // cerr<<" "<<h<<" "<<(lens[h] - (ord[u] - ord[h]) - 1)<<endl; bAdd(h, lens[h] - (ord[u] - ord[h]) - 1, key, val); u = par[h]; } }; auto hldSum = [&](int u, int key) { // cerr<<"hldSum "<<u<<" "<<key<<endl; const int h = head[u]; // sum at (ord[u] - ord[h]) // cerr<<" "<<h<<" "<<(lens[h] - (ord[u] - ord[h]))<<endl; const int ret = bSum(h, lens[h] - (ord[u] - ord[h]), key); return ret; }; vector<vector<int>> pp(E, vector<int>(N)); vector<vector<Int>> dd(E, vector<Int>(N)); pp[0] = par; for (int u = 0; u < N - 1; ++u) { dd[0][u] = (par[u] == -1) ? 0 : (dist[u] - dist[par[u]]); } for (int e = 0; e < E - 1; ++e) { for (int u = 0; u < N; ++u) { if (pp[e][u] == -1) { pp[e + 1][u] = -1; dd[e + 1][u] = dd[e][u]; } else { pp[e + 1][u] = pp[e][pp[e][u]]; dd[e + 1][u] = dd[e][u] + dd[e][pp[e][u]]; } } } /* vector<int>brt(Q,-58); if(N<=2000&&Q<=2000){ vector<vector<Int>>wss(N); for(int q=0;q<Q;++q){ if(TYP[q]==0){ for(int u=V[q];u!=-1&&dist[V[q]]-dist[u]<=L[q];u=par[u]){ // cerr<<"brt add "<<u<<" "<<T[q]+dist[V[q]]<<endl; wss[u].push_back(T[q]+dist[V[q]]); } }else{ sort(wss[V[q]].begin(),wss[V[q]].end()); brt[q]=upper_bound(wss[V[q]].begin(),wss[V[q]].end(),T[q]+dist[V[q]])-wss[V[q]].begin(); // printf("%d\n",brt[q]); } } // continue; } // exit(1); //*/ vector<int> ans(Q, -58); vector<pair<pair<Int, int>, int>> qrys(Q); for (int q = 0; q < Q; ++q) { qrys[q] = make_pair(make_pair(T[q] + dist[V[q]], TYP[q]), q); } sort(qrys.begin(), qrys.end()); for (const auto &qry : qrys) { const int q = qry.second; switch (TYP[q]) { case 0: { Int l = L[q]; int u = V[q]; for (int e = E; e--; ) { if (pp[e][u] != -1 && l - dd[e][u] >= 0) { l -= dd[e][u]; u = pp[e][u]; } } // cerr<<"q = "<<q<<": u = "<<u<<endl; // assert(dist[V[q]]-dist[u]<=L[q]); // if(par[u]!=-1)assert(dist[V[q]]-dist[par[u]]>L[q]); hldAdd(V[q], q, +1); hldAdd(par[u], q, -1); } break; case 1: { // cerr<<"q = "<<q<<endl; ans[q] = hldSum(V[q], q); } break; default: assert(false); } } for (int q = 0; q < Q; ++q) { if (TYP[q] == 1) { printf("%d\n", ans[q]); } } /* if(N<=2000&&Q<=2000){ for(int q=0;q<Q;++q)if(brt[q]!=ans[q]){ cerr<<"q = "<<q<<": "<<brt[q]<<" "<<ans[q]<<endl; assert(false); } } //*/ } return 0; }