社区讨论
TLE90pts球条,玄关
P6626[省选联考 2020 B 卷] 消息传递参与者 2已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mmafla3v
- 此快照首次捕获于
- 2026/03/03 17:54 上周
- 此快照最后确认于
- 2026/03/06 19:35 4 天前
rt
CPP#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
namespace lsy{
namespace IO{
#ifndef LOCAL
constexpr auto maxn=1<<20;
char in[maxn],out[maxn],*p1=in,*p2=in,*p3=out;
#define getchar() (p1==p2&&(p2=(p1=in)+fread(in,1,maxn,stdin),p1==p2)?EOF:*p1++)
#define flush() (fwrite(out,1,p3-out,stdout))
#define putchar(x) (p3==out+maxn&&(flush(),p3=out),*p3++=(x))
class Flush{public:~Flush(){flush();}}_;
#endif
namespace usr{
template<typename type>
inline type read(type &x){
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return flag?x=-x:x;
}
template<typename type>
inline void write(type x){
x<0?x=-x,putchar('-'):0;
static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
}
inline char read(char &x){do x=getchar();while(isspace(x));return x;}
inline char write(const char &x){return putchar(x);}
inline void read(char *x){static char ch;read(ch);do *(x++)=ch;while(!isspace(ch=getchar())&&~ch);}
template<typename type>inline void write(type *x){while(*x)putchar(*(x++));}
inline void read(string &x){static char ch;read(ch),x.clear();do x+=ch;while(!isspace(ch=getchar())&&~ch);}
inline void write(const string &x){for(int i=0,len=x.length();i<len;++i)putchar(x[i]);}
template<typename type,typename...T>inline void read(type &x,T&...y){read(x),read(y...);}
template<typename type,typename...T>
inline void write(const type &x,const T&...y){write(x),putchar(' '),write(y...),sizeof...(y)^1?0:putchar('\n');}
template<typename type>
inline void put(const type &x,bool flag=1){write(x),flag?putchar('\n'):putchar(' ');}
}
#ifndef LOCAL
#undef getchar
#undef flush
#undef putchar
#endif
}using namespace IO::usr;
}using namespace lsy::IO::usr;
const int maxn=100010;
int n,m;
struct PII{
int first,second;
bool operator<(const PII t)const{return t.first==first?second<t.second:first<t.first;}
};
vector<int>e[maxn];
int yy(int u){return u;}
int fa(int u){return u+n;}
class Segment_Tree{
private:
__gnu_pbds::gp_hash_table<int,int>mp[maxn*2];
public:
void clear(){for(int i=1;i<=n+n;i++) mp[i].clear();}
void update(int id,int d,int z){mp[id][d]+=z;}
int query(int id,int d){return mp[id][d];}
}sgt;
PII array_for_ST[maxn];
class ST{
private:
PII st[20][maxn];
public:
void build(){
for(int i=1;i<=n;i++) st[0][i]=array_for_ST[i];
for(int i=1;i<=17;i++) for(int j=1;j<=n;j++){
int k=j+(1<<i-1);
if(k>n) break;
st[i][j]=min(st[i-1][j],st[i-1][k]);
}
}
PII query(int l,int r){
int lg=__lg(r-l+1);
return min(st[lg][l],st[lg][r-(1<<lg)+1]);
}
};
class Original_Tree{
private:
int fa[maxn],deep[maxn],dfn[maxn],cnt;
ST st;
void dfs(int u,int father=0){
fa[u]=father,dfn[u]=++cnt;
deep[u]=deep[father]+1;
for(int v:e[u]){
if(v==father) continue;
dfs(v,u);
}
}
int lca(int u,int v){
if(u==v) return u;
int d=st.query(min(dfn[u],dfn[v])+1,max(dfn[u],dfn[v])).second;
return fa[d];
}
public:
void build(){
dfs(1);
for(int i=1;i<=n;i++) array_for_ST[dfn[i]]={deep[i],i};
st.build();
}
void clear(){cnt=0;}
int get_dis(int u,int v){return deep[u]+deep[v]-2*deep[lca(u,v)];}
}OT;
class Centroid_Tree{
private:
int sz[maxn],son[maxn],d,rt,root,fa[maxn],dis[maxn][20];
bool flag[maxn];
void get_root(int u,int fa=0){
sz[u]=1,son[u]=0;
for(int v:e[u]){
if(v==fa||flag[v]) continue;
get_root(v,u);
son[u]=max(son[u],sz[v]),sz[u]+=sz[v];
}
son[u]=max(son[u],d-sz[u]);
if(son[u]<=son[rt]) rt=u;
}
void dfs(int u){
flag[u]=1;
for(int v:e[u]){
if(flag[v]) continue;
rt=0,d=sz[v];
get_root(v);
fa[rt]=u;
dfs(rt);
}
}
void update(int u){
int lt=0,y=u,ls=0;
while(u){
int dis=OT.get_dis(y,u);
this->dis[y][ls]=dis;
if(lt)sgt.update(::fa(lt),dis,1);
sgt.update(yy(u),dis,1);
u=fa[lt=u],ls++;
}
}
public:
void build(){
d=son[0]=n;
rt=0;
get_root(1);
dfs(root=rt);
for(int i=1;i<=n;i++) update(i);
}
void clear(){memset(fa,0,sizeof(fa));memset(flag,0,sizeof(flag));}
int query(int u,int k){
int lt=0,ans=0,y=u,ls=0;
while(u){
int dis=this->dis[y][ls];
if(lt) ans-=sgt.query(::fa(lt),k-dis);
ans+=sgt.query(yy(u),k-dis);
u=fa[lt=u],ls++;
}
return ans;
}
}CT;
void solve(){
OT.clear();
CT.clear();
sgt.clear();
for(int i=1;i<=n;i++) e[i].clear();
read(n,m);
for(int i=1;i<n;i++){
int u,v;read(u,v);
e[u].push_back(v),e[v].push_back(u);
}
OT.build(),CT.build();
for(int i=1;i<=m;i++){
int x,k;read(x,k);
write(CT.query(x,k));
write("\n");
}
}
signed main(){
int T;read(T);
while(T--) solve();
return 0;
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...