結果
| 問題 |
No.1216 灯籠流し/Lanterns
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2020-08-30 15:37:27 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 9,639 bytes |
| コンパイル時間 | 4,818 ms |
| コンパイル使用メモリ | 263,644 KB |
| 最終ジャッジ日時 | 2025-01-13 22:48:40 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 23 RE * 15 TLE * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const char newl = '\n';
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
vector<T> ts(n);
for(size_t i=0;i<n;i++) cin>>ts[i];
return ts;
}
struct Sack{
using F = function<void(Int)>;
Int n;
vector<Int> sz,hvy,big;
vector<vector<Int> > G,Q;
F expand,shrink,query,reset;
Sack(Int n,F expand,F shrink,F query,F reset):
sz(n,1),hvy(n,-1),big(n,0),G(n),Q(n),
expand(expand),shrink(shrink),
query(query),reset(reset){}
void add_edge(Int u,Int v){
G[u].emplace_back(v);
G[v].emplace_back(u);
}
void add_query(Int v,Int k){
Q[v].emplace_back(k);
sz[v]++;
}
void add(Int v,Int p,Int x){
if(x==1) expand(v);
else shrink(v);
for(Int u:G[v])
if(u!=p&&!big[u]) add(u,v,x);
}
void dfs(Int v=0,Int p=-1,bool k=0){
for(Int u:G[v])
if(u!=p&&u!=hvy[v]) dfs(u,v,0);
if(~hvy[v]){
dfs(hvy[v],v,1);
big[hvy[v]]=1;
}
add(v,p,1);
for(Int k:Q[v]) query(k);
if(~hvy[v]) big[hvy[v]]=0;
if(!k) add(v,p,0),reset(v);
}
void build(Int v=0,Int p=-1){
for(Int u:G[v]){
if(u==p) continue;
build(u,v);
if(hvy[v]<0||sz[hvy[v]]<sz[u]) hvy[v]=u;
sz[v]+=sz[u];
}
if(p==-1) dfs(v,p);
}
};
class EulerTourForVertex{
private:
vector<Int> ls,rs;
Int pos;
void dfs(Int v,Int p){
ls[v]=pos++;
for(Int u:G[v])
if(u!=p) dfs(u,v);
rs[v]=pos;
}
public:
vector< vector<Int> > G;
EulerTourForVertex(){}
EulerTourForVertex(Int n):ls(n),rs(n),G(n){}
void add_edge(Int u,Int v){
G[u].emplace_back(v);
G[v].emplace_back(u);
}
void build(Int r=0){
pos=0;
dfs(r,-1);
}
Int idx(Int v){return ls[v];}
template<typename F>
void exec(Int v,F f){
f(ls[v],rs[v]);
}
};
struct LowestCommonAncestor{
Int n,h;
vector< vector<Int> > G,par;
vector<Int> dep;
LowestCommonAncestor(){}
LowestCommonAncestor(Int n):n(n),G(n),dep(n){
h=1;
while((1<<h)<=n) h++;
par.assign(h,vector<Int>(n,-1));
}
void add_edge(Int u,Int v){
G[u].emplace_back(v);
G[v].emplace_back(u);
}
void dfs(Int v,Int p,Int d){
par[0][v]=p;
dep[v]=d;
for(Int u:G[v])
if(u!=p) dfs(u,v,d+1);
}
void build(Int r=0){
dfs(r,-1,0);
for(Int k=0;k+1<h;k++)
for(Int v=0;v<n;v++)
if(~par[k][v])
par[k+1][v]=par[k][par[k][v]];
}
Int lca(Int u,Int v){
if(dep[u]>dep[v]) swap(u,v);
for(Int k=0;k<h;k++)
if((dep[v]-dep[u])>>k&1)
v=par[k][v];
if(u==v) return u;
for(Int k=h-1;k>=0;k--)
if(par[k][u]!=par[k][v])
u=par[k][u],v=par[k][v];
return par[0][u];
}
Int distance(Int u,Int v){
return dep[u]+dep[v]-dep[lca(u,v)]*2;
}
};
struct AuxiliaryTree : EulerTourForVertex{
using super = EulerTourForVertex;
LowestCommonAncestor lca;
vector<vector<Int>> T;
AuxiliaryTree(){}
AuxiliaryTree(Int n):super(n),lca(n),T(n){}
void build(Int r=0){
super::build(r);
lca.G=super::G;
lca.build(r);
}
void add_aux_edge(Int u,Int v){
T[u].emplace_back(v);
T[v].emplace_back(u);
}
using super::idx;
void query(vector<Int> &vs){
assert(!vs.empty());
sort(vs.begin(),vs.end(),
[&](Int a,Int b){return idx(a)<idx(b);});
Int k=vs.size();
stack<Int> st;
st.emplace(vs[0]);
for(Int i=0;i+1<k;i++){
Int w=lca.lca(vs[i],vs[i+1]);
if(w!=vs[i]){
Int l=st.top();st.pop();
while(!st.empty()&&lca.dep[w]<lca.dep[st.top()]){
add_aux_edge(st.top(),l);
l=st.top();st.pop();
}
if(st.empty()||st.top()!=w){
st.emplace(w);
vs.emplace_back(w);
}
add_aux_edge(w,l);
}
st.emplace(vs[i+1]);
}
while(st.size()>1){
Int c=st.top();st.pop();
add_aux_edge(st.top(),c);
}
}
void clear(const vector<Int> &ws){
for(Int w:ws) T[w].clear();
}
};
template<typename T>
struct RangeCount{
struct BIT{
vector<T> dat;
BIT(){}
BIT(Int n){dat.assign(++n,0);}
T sum(Int k){
T res=0;
for(;k;k-=k&-k) res+=dat[k];
return res;
}
void add(Int k,T v){
for(++k;k<(Int)dat.size();k+=k&-k) dat[k]+=v;
}
};
Int n;
using ll = long long;
vector< vector<ll> > val;
vector<BIT> dat;
RangeCount(){}
RangeCount(Int n_){
n=1;
while(n<n_) n<<=1;
val.assign(n<<1,vector<ll>());
dat.reserve(n<<1);
}
void preupdate(Int a,ll x){
a+=n;
while(a){
val[a].emplace_back(x);
a>>=1;
}
}
void build(){
for(Int i=0;i<n*2;i++){
auto &v=val[i];
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
dat.emplace_back(v.size());
}
}
void update(Int a,ll x,T z){
// cout<<a<<' '<<x<<' '<<z<<endl;
a+=n;
while(a){
auto &v=val[a];
Int k=lower_bound(v.begin(),v.end(),x)-v.begin();
assert(v[k]==x);
dat[a].add(k,z);
a>>=1;
}
}
T calc(Int k,ll x,ll y){
auto &v=val[k];
Int p=lower_bound(v.begin(),v.end(),x)-v.begin();
Int q=lower_bound(v.begin(),v.end(),y)-v.begin();
return dat[k].sum(q)-dat[k].sum(p);
}
// [a, b) * [x, y)
T query(Int a,Int b,ll x,ll y){
// cout<<a<<' '<<b<<' '<<x<<' '<<y<<endl;
T res=0;
for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1){
if(l&1) res+=calc(l++,x,y);
if(r&1) res+=calc(--r,x,y);
}
return res;
}
};
template<typename TV, const Int N> void read_tuple_impl(TV&) {}
template<typename TV, const Int N, typename Head, typename... Tail>
void read_tuple_impl(TV& ts) {
get<N>(ts).emplace_back(*(istream_iterator<Head>(cin)));
read_tuple_impl<TV, N+1, Tail...>(ts);
}
template<typename... Ts> decltype(auto) read_tuple(size_t n) {
tuple<vector<Ts>...> ts;
for(size_t i=0;i<n;i++) read_tuple_impl<decltype(ts), 0, Ts...>(ts);
return ts;
}
template<typename F>
struct FixPoint : F{
FixPoint(F&& f):F(forward<F>(f)){}
template<typename... Args>
decltype(auto) operator()(Args&&... args) const{
return F::operator()(*this,forward<Args>(args)...);
}
};
template<typename F>
inline decltype(auto) MFP(F&& f){
return FixPoint<F>{forward<F>(f)};
}
template<typename V>
V compress(V vs){
sort(vs.begin(),vs.end());
vs.erase(unique(vs.begin(),vs.end()),vs.end());
return vs;
}
template<typename T>
map<T, Int> dict(const vector<T> &vs){
map<T, Int> res;
for(Int i=0;i<(Int)vs.size();i++)
res[vs[i]]=i;
return res;
}
map<char, Int> dict(const string &s){
return dict(vector<char>(s.begin(),s.end()));
}
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
// AtCoder
const i64 CYCLES_PER_SEC = 2800000000;
struct Timer {
i64 start;
Timer(){reset();}
void reset(){start=getCycle();}
inline double get(){return (double)(getCycle()-start)/CYCLES_PER_SEC;}
inline i64 getCycle(){
u32 low,high;
__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
return ((i64)low)|((i64)high<<32);
}
};
Timer timer;
//INSERT ABOVE HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
using ll = long long;
Int n,q;
cin>>n>>q;
using P = pair<Int, ll>;
vector<vector<P>> G(n);
AuxiliaryTree H(n);
for(Int i=1;i<n;i++){
Int a,b;
ll c;
cin>>a>>b>>c;
a--;b--;
G[a].emplace_back(b,c);
G[b].emplace_back(a,c);
H.add_edge(a,b);
}
H.build(0);
vector<ll> dep(n);
MFP([&](auto dfs,Int v,Int p,ll d)->void{
dep[v]=d;
for(auto [u,c]:G[v])
if(u!=p) dfs(u,v,d+c);
})(0,-1,0);
auto [type,vs,ts,ls]=read_tuple<Int, Int, ll, ll>(q);
for(Int &v:vs) v--;
vector<Int> ans(q);
queue<P> que;
que.emplace(0,q);
while(!que.empty()){
if(timer.get()>4.0) break;
auto [L, R]=que.front();que.pop();
if(L+1==R) continue;
Int M=(L+R)>>1;
// cout<<L<<':'<<M<<':'<<R<<endl;
vector<Int> ss(vs.begin()+L,vs.begin()+R);
ss.emplace_back(0);
H.query(ss);
ss=compress(ss);
auto dc=dict(ss);
vector<Int> pos;
for(Int i=L;i<M;i++)
if(type[i]==0) pos.emplace_back(ts[i]+dep[vs[i]]);
for(Int i=M;i<R;i++)
if(type[i]==1) pos.emplace_back(ts[i]+dep[vs[i]]);
pos=compress(pos);
auto rev=dict(pos);
Int m=ss.size();
RangeCount<Int> seg(m);
vector<vector<Int>> B(m);
for(Int i=L;i<M;i++){
if(type[i]!=0) continue;
seg.preupdate(rev[ts[i]+dep[vs[i]]],dep[vs[i]]-ls[i]);
B[dc[vs[i]]].emplace_back(i);
}
seg.build();
auto expand=[&](Int v){
for(Int i:B[v])
seg.update(rev[ts[i]+dep[vs[i]]],dep[vs[i]]-ls[i],+1);
};
auto shrink=[&](Int v){
for(Int i:B[v])
seg.update(rev[ts[i]+dep[vs[i]]],dep[vs[i]]-ls[i],-1);
};
auto query=[&](Int i){
if(type[i]==1)
ans[i]+=seg.query(0,rev[ts[i]+dep[vs[i]]]+1,-1e18,dep[vs[i]]+1);
};
auto reset=[&](Int){};
Sack S(m,expand,shrink,query,reset);
for(Int v:ss)
for(Int u:H.T[v])
if(v<u) S.add_edge(dc[u],dc[v]);
for(Int i=L;i<M;i++)
if(type[i]==0) S.add_query(dc[vs[i]],i);
for(Int i=M;i<R;i++)
if(type[i]==1) S.add_query(dc[vs[i]],i);
S.build(dc[0]);
H.clear(ss);
que.emplace(L,M);
que.emplace(M,R);
}
for(Int i=0;i<q;i++)
if(type[i]==1) cout<<ans[i]<<newl;
return 0;
}
beet