luogu P3899 [湖南集训]谈笑风生 线段树合并
Description
Input
Output
输出 q 行,每行对应一个询问,代表询问的答案。
题解:对于第二种情况,直接用子树大小计算一下就行了.
对于第三种情况,用树上线段树合并,计算出每个点所维护的深度的线段树就行了.
至于怎么计算,用线段树合并即可.
#include<bits/stdc++.h>
#define maxn 300002
#define ll long long
using namespace std;
void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
namespace tr
{
#define lson t[x].l
#define rson t[x].r
#define mid ((l+r)>>1)
int cnt;
int newnode(){ return ++cnt; }
struct Node
{
int l,r;
ll val, sumv;
}t[maxn*60];
void insert(int &x,int l,int r,int k,ll delta)
{
if(!x) x = newnode();
t[x].sumv += delta;
if(l==r) { t[x].val=delta; return; }
if(k <= mid) insert(lson, l, mid, k, delta);
else insert(rson, mid + 1, r, k, delta);
}
int merge(int u,int v)
{
if(!u||!v) return u + v;
int x = newnode();
t[x].sumv = t[u].sumv + t[v].sumv;
lson = merge(t[u].l,t[v].l);
rson = merge(t[u].r,t[v].r);
return x;
}
ll query(int x,int l,int r,int L,int R)
{
if(!x) return 0;
if(l>=L&&r<=R) return t[x].sumv;
ll tmp=0;
if(L<=mid) tmp+=query(lson,l,mid,L,R);
if(R>mid) tmp+=query(rson,mid+1,r,L,R);
return tmp;
}
};
int n,Q,edges;
int hd[maxn],to[maxn<<1],nex[maxn<<1],fa[maxn],siz[maxn],dep[maxn],rt[maxn];
void add(int u,int v)
{
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;
}
void get(int u,int ff)
{
int i,v;
siz[u]=1, dep[u] = dep[ff] + 1;
for(i=hd[u];i;i=nex[i])
{
v=to[i];
if(v==ff) continue;
get(v,u), siz[u]+=siz[v];
}
}
void DFS(int u,int ff)
{
int i,v;
tr::insert(rt[u],0,n,dep[u],1ll*(siz[u]-1));
for(i=hd[u];i;i=nex[i])
{
v=to[i];
if(v==ff) continue;
DFS(v, u);
rt[u] = tr::merge(rt[v], rt[u]);
}
}
int main()
{
// setIO("input");
int i,j,x,y;
ll ans=0;
scanf("%d%d",&n,&Q);
for(i=1;i<n;++i)
{
scanf("%d%d",&x,&y);
add(x,y), add(y,x);
}
dep[0] = -1, get(1,0), DFS(1,0);
while(Q--)
{
scanf("%d%d",&x,&y);
ans = 0;
ans += 1ll*(siz[x]-1) * min(dep[x] , y);
ans += 1ll*tr::query(rt[x], 0, n, dep[x] + 1, dep[x] + y);
printf("%lld\n",ans);
}
return 0;
}
luogu P3899 [湖南集训]谈笑风生 线段树合并的更多相关文章
- 洛谷P3899 [湖南集训]谈笑风生(线段树合并)
题意 题目链接 Sol 线段树合并板子题,目前我看到两种写法,分别是这样的. 前一种每次需要新建一个节点,空间是\(O(4nlogn)\) 后者不需要新建,空间是\(O(nlogn)\)(面向数据算空 ...
- [Luogu P3899] [湖南集训]谈笑风生 (主席树)
题面 传送门:https://www.luogu.org/problemnew/show/P3899 Solution 你们搞的这道题啊,excited! 这题真的很有意思. 首先,我们可以先理解一下 ...
- 主席树 || 可持久化线段树 || BZOJ 3653: 谈笑风生 || Luogu P3899 [湖南集训]谈笑风生
题面:P3899 [湖南集训]谈笑风生 题解: 我很喜欢这道题. 因为A是给定的,所以实质是求二元组的个数.我们以A(即给定的P)作为基点寻找答案,那么情况分两类.一种是B为A的父亲,另一种是A为B的 ...
- luogu P3899 [湖南集训]谈笑风生
传送门 nmyzd,mgdhls,bnmbzdgdnlql,a,wgttxfs 对于一个点\(a\),点\(b\)只有可能是他的祖先或者在\(a\)子树里 如果点\(b\)是\(a\)祖先,那么答案为 ...
- P3899 [湖南集训]谈笑风生 主席树
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- P3899 [湖南集训]谈笑风生
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3653 https://www.luogu.org/problemnew/show/P38 ...
- 【Luogu】P3521ROT-Tree Rotations(线段树合并)
题目链接 神奇的线段树合并qwq 不过就思路而言很好想…… 观察到一棵树无论怎么交换两棵左右子树,子树内部的最优逆序对并没影响……决策只影响左右子树之间的逆序对…… 于是线段树合并直接乱搞就好啦 ...
- luogu P4775 [NOI2018]情报中心 线段树合并 虚树 树的直径trick
LINK:情报中心 神题! 写了一下午 写到肚子疼. 调了一晚上 调到ex 用的是网上dalao的方法 跑的挺快的. 对于链的暴力 我不太会kk. 直接说正解吧: 分类讨论两种情况: 1 答案的两条链 ...
- luogu P3180 [HAOI2016]地图 仙人掌 线段树合并 圆方树
LINK:地图 考虑如果是一棵树怎么做 权值可以离散 那么可以直接利用dsu on tree+树状数组解决. 当然 也可以使用莫队 不过前缀和比较难以维护 外面套个树状数组又带了个log 套分块然后就 ...
随机推荐
- document.getElementsByClassName("head")[0].parentElement
document.getElementsByClassName("head")[0].parentElement
- Email-ext plugin
https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin General This plugin extends Jenkins built i ...
- YTU 2633: P3 数钱是件愉快的事
2633: P3 数钱是件愉快的事 时间限制: 1 Sec 内存限制: 128 MB 提交: 387 解决: 215 题目描述 超市收银员的钱盒里,各种钞票总是按照面额分类整理,这样做可以提高效率 ...
- cassandra的全文检索插件
https://github.com/Stratio/cassandra-lucene-index Stratio’s Cassandra Lucene Index Stratio’s Cassand ...
- Recyclerview 顶部悬停 stick
activity布局 ll_top代表要悬停的部分 这里面我放了 图片和文本 1 <?xml version="1.0" encoding="utf-8&qu ...
- hive时间
Hive中日期函数总结:1.时间戳函数日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数select unix_timestamp(); --获得当前时区的UNIX时间戳s ...
- bzoj 4337 树的同构
4337: BJOI2015 树的同构 Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树 ...
- 【BZOJ 5165】 树上倍增
[题目链接] 点击打开链接 [算法] 树上倍增,时间复杂度 : O(qklog(n)) [代码] #include<bits/stdc++.h> using namespace std; ...
- 洛谷 P2296 寻找道路 —— bfs
题目:https://www.luogu.org/problemnew/show/P2296 第一次用 Emacs 对拍,写了半天: 注意那个 is 赋值的地方很容易错,千万别反复赋值: 一道水题写了 ...
- Vsftpd软件包的获取与安装
11.2 Vsftpd简介 Vsftpd是一种在GPL许可下开放源代码的FTP服务器,用于多种UNIX系统和Linux系统.Vsftpd也称为Very Secure FTP Daemon,它是一种安 ...