QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#406283 | #1163. Another Tree Queries Problem | Doqe | WA | 204ms | 7756kb | C++23 | 3.1kb | 2024-05-07 08:43:40 | 2024-05-07 08:43:40 |
Judging History
answer
//just try
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
vector<int>to[N];
int dfn[N],dep[N],sz[N],top[N],son[N],fa[N],tt;
void dfs(int u,int f)
{
if(f)to[u].erase(find(to[u].begin(),to[u].end(),f));
dep[u]=dep[fa[u]=f]+1;
sz[u]=1;
for(int v:to[u])if(v!=f)
{
dfs(v,u);sz[u]+=sz[v];
if(sz[son[u]]<sz[v])son[u]=v;
}
}
void dfs1(int u,int f)
{
top[u]=f;dfn[u]=++tt;
if(son[u])dfs1(son[u],f);
for(int v:to[u])if(v!=fa[u]&&v!=son[u])dfs1(v,v);
}
struct tag
{
long long t;void TG(const tag&a){t+=a.t;}
bool any()const{return t;}
void clr(){t=0;}
}tg[N<<2];
struct node
{
long long s;int len;
void TG(const tag&a){s+=a.t*len;}
node operator+(const node&a)const{return {s+a.s,len+a.len};}
}tr[N<<2];
inline void TG(int p,const tag&v){tr[p].TG(v),tg[p].TG(v);}
void PD(int p){if(tg[p].any())TG(p<<1,tg[p]),TG(p<<1|1,tg[p]),tg[p].clr();}
void build(int p,int l,int r){tr[p].len=r-l+1;if(l==r)return;int mid=l+r>>1;build(p<<1,l,mid),build(p<<1|1,mid+1,r);}
void mdf(int p,int l,int r,int L,int R,const tag&v)
{
if(L>R)return;
if(L<=l&&r<=R)return TG(p,v);
int mid=l+r>>1;PD(p);
if(L<=mid)mdf(p<<1,l,mid,L,R,v);
if(R> mid)mdf(p<<1|1,mid+1,r,L,R,v);
tr[p]=tr[p<<1]+tr[p<<1|1];
}
node qry(int p,int l,int r,int L,int R)
{
if(L<=l&&r<=R)return tr[p];
int mid=l+r>>1;PD(p);
if(R<=mid)return qry(p<<1,l,mid,L,R);
if(L> mid)return qry(p<<1|1,mid+1,r,L,R);
return qry(p<<1,l,mid,L,R)+qry(p<<1|1,mid+1,r,L,R);
}
inline int LCA(int u,int v)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]])swap(u,v);
u=fa[top[u]];
}
return dep[u]<dep[v]?u:v;
}
long long rsu[N];
long long dep_val;//\sum dep[i]*A_i
int main()
{
int n;cin>>n;
for(int i=1,u,v;i<n;++i)
cin>>u>>v,to[u].push_back(v),to[v].push_back(u);
dfs(1,0);dfs1(1,1);
for(int i=1;i<=n;++i)sort(to[i].begin(),to[i].end(),[=](int x,int y){return dfn[x]<dfn[y];});
build(1,1,n);
for(int i=1;i<=n;++i)rsu[dfn[i]]=dep[i];
for(int i=1;i<=n;++i)rsu[i]+=rsu[i-1];
int q;cin>>q;
while(q--)
{
int o,u,v;cin>>o>>u;
if(o==1)
{
cin>>v;
if(u==v)
{
mdf(1,1,n,1,n,{1});
dep_val+=rsu[n];
}
else if(dfn[u]<dfn[v]||dfn[u]>=dfn[v]+sz[v])
{
mdf(1,1,n,dfn[v],dfn[v]+sz[v]-1,{1});
dep_val+=rsu[dfn[v]+sz[v]-1]-rsu[dfn[v]-1];
}
else
{
int w=*lower_bound(to[v].begin(),to[v].end(),u,[=](int x,int y){return dfn[x]<dfn[y];});
mdf(1,1,n,1,dfn[w]-1,{1});mdf(1,1,n,dfn[w]+sz[w],n,{1});
dep_val+=rsu[dfn[w]-1]+(rsu[n]-rsu[dfn[w]+sz[w]-1]);
}
}
else if(o==2)
{
cin>>v;
dep_val+=dep[u]*(dep[u]+1ll)/2;
dep_val+=dep[v]*(dep[v]+1ll)/2;
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]])swap(u,v);
mdf(1,1,n,dfn[top[u]],dfn[u],{1}),u=fa[top[u]];
}
if(dfn[u]>dep[v])swap(u,v);
mdf(1,1,n,dfn[u],dfn[v],{1});
dep_val-=dep[u]*(dep[u]+1ll)/2;
dep_val-=dep[u]*(dep[u]-1ll)/2;
}
else
{
long long x1=dep[u]*qry(1,1,n,1,n).s,x2=dep_val,x3=0;
int v=u;
while(v)x3+=qry(1,1,n,dfn[v],dfn[v]+sz[v]-1).s,v=fa[v];
cerr<<x1<<","<<x2<<","<<x3<<endl;
cout<<x1+x2-x3-x3<<endl;
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 7676kb
input:
5 4 2 2 5 1 5 1 3 5 2 2 4 3 4 2 1 5 2 5 5 3 2
output:
1 5
result:
ok 2 number(s): "1 5"
Test #2:
score: -100
Wrong Answer
time: 204ms
memory: 7756kb
input:
200 171 114 50 183 28 68 67 152 139 125 67 55 50 98 106 71 46 42 157 165 42 49 113 12 81 145 105 13 38 96 34 156 24 17 21 191 135 54 174 116 177 157 123 71 95 130 135 193 150 129 25 190 96 93 188 173 90 160 86 187 20 132 199 75 59 195 189 24 40 68 163 83 25 13 73 33 59 50 154 19 146 21 151 67 89 69 ...
output:
787 903 768 1748 2103 1189 2817 2278 4615 3804 3931 3619 5578 4395 5916 6885 4085 7351 6820 9125 7080 5520 10510 6473 12255 14325 8765 8603 8118 10587 17848 21531 15752 17106 10640 13837 11710 18072 12033 17687 24138 21004 25032 19969 14144 22442 13321 19734 13479 25235 19362 30125 19860 33435 17589...
result:
wrong answer 1st numbers differ - expected: '826', found: '787'