1 solutions
-
-1
//又来发题解了 // Problem: P3899 [湖南集训]谈笑风生 // Contest: Luogu // URL: https://www.luogu.com.cn/problem/P3899 // Memory Limit: 500 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)
// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template inline void read(T &x) { T f=1;x=0; char ch=getchar(); while(0isdigit(ch)){if(ch'-')f=-1;ch=getchar();} while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar(); x*=f; } template inline void write(T x) { if(x<0){x=~(x-1);putch if(x>9)write(x/10); putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; struct Node { int l,r; LL sum; }tree[N20]; vectornode[N]; int root[N],cnt; int deep[N],L[N],R[N],sz[N],dfn,n; void update(int &k,int pos,int val,int l,int r) { tree+]=tree[k]; k=cnt-1; tree[k].sum+=val; if(lr) return; int mid=(l+r)>>1; if(pos<=mid) update(tree[k].l,pos,val,l,mid); else update(tree[k].r,pos,val,mid+1,r); } LL query(int i,int j,int l,int r,int L,int R) { if(l>return 0; } if(L>r||R<l) { return 0; } if(L>=l&&R<=r) { return tree[j].sum-tree[i].sum; } int mid=(L+R)>>1; return query(tree[i].l,tree[j].l,l,r,L,mid)+query(tree[i].r,tree[j].r,l,r,mid+1,R); } void dfs1(int u,int fa,int dep) { L[u]=++dfn; sz[u]=1; deep[u]=dep; for(auto v:node[u]) { if(vfa) { continue; } u,dep+1); sz[u]+=sz[v]; } R[u]=dfn; } void dfs2(int u,int fa) { root[L[u]]=root[L[u]-1]; update(root[L[u]],deep[u],sz[u]-1,1,n); for(auto v:node[u]) { if(v==fa) { continue; } dfs2(v,u); } } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false); int m; read(n),read(m); for(int i=1;i<n;i++) { int u,v; read(u),read(v); node[u].push_back(v); node[v].push_back(u); } dfs1(1,0,1); dfs2(1,0); while(m--) { int x,k; read(x),read(k); LL ans=1LLmin(deep[x]-1,k)*(sz[x]-1); ans+=query(root[L[x]-1],root[R[x]],deep[x]+1,min(deepn),1,n); printf("%lld\n",ans); } return 0; //有删改(坏笑)
- 1
Information
- ID
- 2842
- Time
- 2000ms
- Memory
- 512MiB
- Difficulty
- 6
- Tags
- # Submissions
- 1
- Accepted
- 1
- Uploaded By