結果
| 問題 |
No.2342 Triple Tree Query (Hard)
|
| コンテスト | |
| ユーザー |
vjudge1
|
| 提出日時 | 2024-10-15 15:41:06 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 4,415 bytes |
| コンパイル時間 | 2,017 ms |
| コンパイル使用メモリ | 180,592 KB |
| 実行使用メモリ | 814,504 KB |
| 最終ジャッジ日時 | 2024-10-15 15:41:14 |
| 合計ジャッジ時間 | 6,925 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | MLE * 1 -- * 1 |
| other | -- * 36 |
ソースコード
//Linkwish's code
#include<bits/stdc++.h>
#define endl '\n'
#define si inline
#define fi first
#define se second
using namespace std;
typedef long long ll;typedef __int128 li;typedef long double ld;
typedef pair<int,int> pii;typedef pair<ll,ll> pll;
typedef const int ci;typedef const ll cl;ci iinf=INT_MAX;cl linf=LLONG_MAX;
template<typename T>si bool gmax(T &x,const T y){if(x<y)return x=y,1;return 0;}
template<typename T>si bool gmin(T &x,const T y){if(y<x)return x=y,1;return 0;}
namespace LinkWish{
ci N=100005,mod=998244353;
int Case,n,m,a[N];
vector<int> e[N];
int dfn[N],out[N],dep[N],sign;
int son[N],sz[N],top[N],prv[N];
void dfs(int x,int fa){
sz[x]=1,dep[x]=dep[prv[x]=fa]+1;
for(int to:e[x]){
if(to!=fa){
dfs(to,x),sz[x]+=sz[to];
if(!son[x]||sz[to]>sz[son[x]])son[x]=to;
}
}
}
void getdfn(int x,int fa){
dfn[x]=++sign;
if(x==son[fa])top[x]=top[fa];
else top[x]=x;
if(son[x])getdfn(son[x],x);
for(int to:e[x])if(to!=fa&&to!=son[x])getdfn(to,x);
out[x]=sign;
}
struct func{
int k,b;
inline func(int x=1,int y=0){k=x,b=y;}
inline void operator *= (func obj){k=1ll*k*obj.k%mod,b=(1ll*b*obj.k+obj.b)%mod;}
};
struct Hoshino{
int ls,rs,l,r,L,R;
func tag,v;
}t[N];
int rt;
si void update(int x,func v){t[x].tag*=v,t[x].v*=v;}
si void push_down(int x){
if(t[x].tag.k!=1||t[x].tag.b!=0){
if(t[x].ls)update(t[x].ls,t[x].tag);
if(t[x].rs)update(t[x].rs,t[x].tag);
t[x].tag=func();
}
}
void build(int &x,int d,vector<int> &p){
if(p.empty())return ;
int s=p.size();
if(d)nth_element(p.begin(),p.begin()+(s-1)/2,p.end(),[&](int xx,int yy){return dep[xx]<dep[yy];});
else nth_element(p.begin(),p.begin()+(s-1)/2,p.end(),[&](int xx,int yy){return dfn[xx]<dfn[yy];});
x=p[(s-1)/2],t[x].l=t[x].r=dfn[x],t[x].L=t[x].R=dep[x];
if(s==1)return ;
vector<int> lp,rp;
for(int i=0;i<(s-1)/2;i++)lp.push_back(p[i]);
for(int i=(s-1)/2+1;i<s;i++)rp.push_back(p[i]);
build(t[x].ls,d^1,lp),build(t[x].rs,d^1,rp);
if(t[x].ls){
gmin(t[x].l,t[t[x].ls].l),gmin(t[x].L,t[t[x].ls].L);
gmax(t[x].r,t[t[x].ls].r),gmax(t[x].R,t[t[x].ls].R);
}
if(t[x].rs){
gmin(t[x].l,t[t[x].rs].l),gmin(t[x].L,t[t[x].rs].L);
gmax(t[x].r,t[t[x].rs].r),gmax(t[x].R,t[t[x].rs].R);
}
}
void modify(int x,int l,int r,int L,int R,const func k){
if(!x||t[x].r<l||t[x].l>r||t[x].R<L||t[x].L>R)return;
if(t[x].l>=l&&t[x].r<=r&&t[x].L>=L&&t[x].R<=R)return update(x,k),void();
if(l<=dfn[x]&&r>=dfn[x]&&L<=dep[x]&&R>=dep[x])t[x].v*=k;
push_down(x);
modify(t[x].ls,l,r,L,R,k),modify(t[x].rs,l,r,L,R,k);
}
int ask(int x,int goal){
if(!x||t[x].l>dfn[goal]||t[x].r<dfn[goal]||t[x].L>dep[goal]||t[x].R<dep[goal])return -1;
if(x==goal)return t[x].v.b;
push_down(x);
int res=ask(t[x].ls,goal);
if(res==-1)res=ask(t[x].rs,goal);
return res;
}
si int lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=prv[top[x]];
}
if(dep[x]<dep[y])return x;
return y;
}
si int dis(int x,int y){
return dep[x]+dep[y]-2*dep[lca(x,y)];
}
si void changesub(int x,func k){
modify(rt,dfn[x],out[x],1,n,k);
}
si void changeline(int x,int y,func k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
modify(rt,dfn[top[x]],dfn[x],1,n,k);
x=prv[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
modify(rt,dfn[x],dfn[y],1,n,k);
}
si void changeD(int x,int d,func k){
modify(rt,dfn[x],out[x],dep[x],dep[x]+d,k);
for(int las=x,cur=prv[x];cur&&~(--d);las=cur,cur=prv[cur]){
modify(rt,dfn[cur],dfn[las]-1,dep[cur],dep[cur]+d,k);
if(out[las]!=out[cur])modify(rt,out[las]+1,out[cur],dep[cur],dep[cur]+d,k);
}
}
si int query(int x){
return ask(rt,x);
}
void mian(){
cin>>Case>>n>>m;
for(int i=1,x,y;i<n;i++)cin>>x>>y,e[x].push_back(y),e[y].push_back(x);
for(int i=1;i<=n;i++)cin>>t[i].v.b;
dfs(1,0),getdfn(1,0);
vector<int> tmp(n);
iota(tmp.begin(),tmp.end(),1);
build(rt,0,tmp);
int op,x,y,k,b;
while(m--){
cin>>op;
if(op==1)cin>>x,cout<<query(x)<<endl;
else if(op==4)cin>>x>>y>>k>>b,changeline(x,y,func(k,b));
else if(op==3)cin>>x>>k>>b,changesub(x,func(k,b));
else cin>>x>>y>>k>>b,changeD(x,y,func(k,b));
}
}
}
signed main(){
#ifndef ONLINE_JUDGE
assert(freopen("tour.in","r",stdin));
assert(freopen("tour.out","w",stdout));
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
LinkWish::mian();
return 0;
}
vjudge1