QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#340166 | #3307. Query on a Tree 17 | AFewSuns | RE | 1ms | 3532kb | C++17 | 3.6kb | 2024-02-28 17:11:42 | 2024-02-28 17:11:43 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define ll long long
#define bl bool
ll my_pow(ll a,ll b,ll mod){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll qpow(ll a,ll b){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
#define db double
#define pf printf
#define pc putchar
#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
#define go(u) for(ll i=head[u];i;i=e[i].nxt)
#define enter pc('\n')
#define space pc(' ')
#define fir first
#define sec second
#define MP make_pair
#define il inline
#define inf 8e18
#define random(x) rand()*rand()%(x)
#define inv(a,mod) my_pow((a),(mod-2),(mod))
il ll read(){
ll sum=0,f=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
sum=sum*10+(ch^48);
ch=getchar();
}
return sum*f;
}
il void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9) write(x/10);
pc(x%10+'0');
}
il void writeln(ll x){
write(x);
enter;
}
il void writesp(ll x){
write(x);
space;
}
}
using namespace my_std;
#define LC x<<1
#define RC x<<1|1
ll n,q,head[100010],cnt=0;
ll dfn[100010],st[100010],ed[100010],tot=0;
ll wson[100010],dep[100010],f[100010][17],siz[100010],top[100010];
ll tree[400040],lz[400040];
struct node{
ll nxt,to;
}e[200020];
void add(ll u,ll v){
e[++cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void dfs1(ll fa,ll u){
f[u][0]=fa;
fr(i,1,16) f[u][i]=f[f[u][i-1]][i-1];
dep[u]=dep[fa]+1;
siz[u]=1;
go(u){
ll v=e[i].to;
if(v==fa) continue;
dfs1(u,v);
siz[u]+=siz[v];
if(siz[v]>siz[wson[u]]) wson[u]=v;
}
}
void dfs2(ll u,ll topp){
top[u]=topp;
dfn[++tot]=u;
st[u]=tot;
if(wson[u]) dfs2(wson[u],topp);
go(u){
ll v=e[i].to;
if(v==f[u][0]) continue;
if(v!=wson[u]) dfs2(v,v);
}
ed[u]=tot;
}
il void pushup(ll x){
tree[x]=tree[LC]+tree[RC];
}
il void pushdown(ll x,ll l,ll r){
if(!lz[x]) return;
ll mid=(l+r)>>1;
tree[LC]+=(mid-l+1)*lz[x];
tree[RC]+=(r-mid)*lz[x];
lz[LC]+=lz[x];
lz[RC]+=lz[x];
lz[x]=0;
}
void mdf(ll x,ll l,ll r,ll ql,ll qr){
if(ql<=l&&r<=qr){
tree[x]+=r-l+1;
lz[x]++;
return;
}
ll mid=(l+r)>>1;
pushdown(x,l,r);
if(ql<=mid) mdf(LC,l,mid,ql,qr);
if(mid<qr) mdf(RC,mid+1,r,ql,qr);
pushup(x);
}
ll querysum(ll x,ll l,ll r,ll ql,ll qr){
if(ql<=l&&r<=qr) return tree[x];
ll mid=(l+r)>>1,res=0;
pushdown(x,l,r);
if(ql<=mid) res+=querysum(LC,l,mid,ql,qr);
if(mid<qr) res+=querysum(RC,mid+1,r,ql,qr);
return res;
}
ll query(ll x,ll l,ll r,ll k){
if(l==r) return l;
ll mid=(l+r)>>1;
pushdown(x,l,r);
if(tree[LC]>=k) return query(LC,l,mid,k);
else return query(RC,mid+1,r,k-tree[LC]);
}
il void mdftree(ll x,ll y){
while(top[x]!=top[y]){
if(dep[x]<dep[y]) swap(x,y);
mdf(1,1,n,st[top[x]],st[x]);
x=f[top[x]][0];
}
if(dep[x]>dep[y]) swap(x,y);
mdf(1,1,n,st[x],st[y]);
}
int main(){
n=read();
fr(i,2,n){
ll u=read(),v=read();
add(u,v);
add(v,u);
}
dfs1(0,1);
dfs2(1,1);
q=read();
while(q--){
ll opt=read(),u=read();
if(opt==1) mdf(1,1,n,st[u],ed[u]);
else{
ll v=read();
mdftree(u,v);
}
ll x=dfn[query(1,1,n,tree[1]/2+1)];
pfr(i,16,0){
if(!f[x][i]) continue;
if(querysum(1,1,n,st[f[x][i]],ed[f[x][i]])<=tree[1]/2) x=f[x][i];
}
if(f[x][0]&&querysum(1,1,n,st[x],ed[x])<=tree[1]/2) x=f[x][0];
writeln(x);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3532kb
input:
7 1 6 1 7 7 3 3 2 7 5 5 4 4 1 2 1 4 1 6 2 6 7
output:
2 7 7 1
result:
ok 4 lines
Test #2:
score: -100
Runtime Error
input:
15 1 2 1 3 2 4 3 7 6 2 3 8 4 9 2 5 5 11 7 13 10 4 11 15 12 5 14 7 11 2 6 11 1 6 2 10 9 1 9 2 2 6 1 4 2 7 6 1 2 2 8 13 1 10 2 8 15