2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162
题意:给出一棵树的链接方法,每个点都有一个数字,询问U-》V节点经过所有路径中l < = x < = r的数字和
解法:主席树维护区间和,树剖查询,复杂度nloglog。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
const int maxm = 40*maxn;
typedef long long LL;
int T[maxn];
int lson[maxm],rson[maxm];
LL sum[maxm];
int clk,M;
int newnode(){
clk++;
lson[clk]=rson[clk]=sum[clk]=0;
return clk;
}
void update(int &now, int pre, int L, int R, int pos, int val)
{
now = newnode();
lson[now] = lson[pre];
rson[now] = rson[pre];
sum[now] = sum[pre] + val;
if(L!=R){
int mid=(L+R)>>1;
if(pos<=mid) update(lson[now],lson[pre],L,mid,pos,val);
else update(rson[now],rson[pre],mid+1,R,pos,val);
}
}
LL query(int rt, int L, int R, int l, int r){
if(l<=L&&R<=r){
return sum[rt];
}
else{
int mid = (L+R)>>1;
LL ret = 0;
if(l <= mid) ret += query(lson[rt], L, mid, l, r);
if(mid < r) ret += query(rson[rt], mid+1, R, l, r);
return ret;
}
}
struct edge{
int to,next;
}E[maxn*2];
int head[maxn],edgecnt,tim;
int siz[maxn],top[maxn],son[maxn],dep[maxn];
int fa[maxn],tid[maxn],Rank[maxn],val[maxn];
void init(){
edgecnt=tim=0;
memset(head,-1,sizeof(head));
memset(son,-1,sizeof(son));
clk=M=0;
lson[clk]=rson[clk]=sum[clk]=0;
}
void add(int u, int v){
E[edgecnt].to=v,E[edgecnt].next=head[u],head[u]=edgecnt++;
}
void dfs1(int u, int pre, int d){
dep[u]=d;
fa[u]=pre;
siz[u]=1;
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=pre){
dfs1(v,u,d+1);
siz[u]+=siz[v];
if(son[u]==-1||siz[v]>siz[son[u]]) son[u]=v;
}
}
}
void dfs2(int u, int tp){
top[u]=tp;
tid[u]=++tim;
Rank[tid[u]]=u;
if(son[u]==-1) return;
dfs2(son[u],tp);
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=son[u]&&v!=fa[u]){
dfs2(v,v);
}
}
}
int LCA(int u, int v)
{
int ret;
while(true)
{
if(top[u] == top[v])
{
ret = dep[u] < dep[v] ? u : v;
break;
}
else if(dep[top[u]] > dep[top[v]])
u = fa[top[u]];
else v = fa[top[v]];
}
return ret;
}
int t[maxn];
LL query(int u, int v, int a, int b)
{
a = lower_bound(t + 1, t + 1 + M, a) - t - 1;
b = upper_bound(t + 1, t + 1 + M, b) - t - 1;
if(b == 0)
return 0;
LL ret = 0;
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]])
swap(u, v);
ret += query(T[tid[u]], 1, M, 1, b);
ret -= query(T[tid[top[u]] - 1], 1, M, 1, b);
if(a)
{
ret -= query(T[tid[u]], 1, M, 1, a);
ret += query(T[tid[top[u]] - 1], 1, M, 1, a);
}
u = fa[top[u]];
}
if(dep[u] < dep[v])
swap(u, v);
ret += query(T[tid[u]], 1, M, 1, b);
ret -= query(T[tid[v] - 1], 1, M, 1, b);
if(a)
{
ret -= query(T[tid[u]], 1, M, 1, a);
ret += query(T[tid[v] - 1], 1, M, 1, a);
}
return ret;
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=1; i<=n; i++){
scanf("%d", &val[i]);
t[i]=val[i];
}
for(int i=1; i<n; i++){
int u,v;
scanf("%d %d", &u,&v);
add(u, v);
add(v, u);
}
dfs1(1,0,0);
dfs2(1,1);
sort(t+1,t+n+1);
M = unique(t+1, t+n+1)-t-1;
for(int i=1; i<=n; i++) val[i] = lower_bound(t+1, t+1+M, val[i])-t;
for(int i=1; i<=n; i++){
T[i] = T[i-1];
update(T[i],T[i],1,M,val[Rank[i]],t[val[Rank[i]]]);
}
for(int i=1; i<=m; i++){
int u,v,x,y;
scanf("%d %d %d %d", &u,&v,&x,&y);
LL ret = query(u,v,x,y);
printf("%lld", ret);
if(i == m){
printf("\n");
}
else{
printf(" ");
}
}
}
return 0;
}
2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树的更多相关文章
- 2017多校第9场 HDU 6161 Big binary tree 思维,类似字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161 题意: 题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号, ...
- HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9
/* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...
- 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 2017多校第9场 HDU 6170 Two strings DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6170 题意:给了2个字符串,其中第2个字符串包含.和*两种特别字符,问第二个字符串能否和第一个匹配. ...
- 2017多校第9场 HDU 6169 Senior PanⅡ 数论,DP,爆搜
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6169 题意:给了区间L,R,求[L,R]区间所有满足其最小质数因子为k的数的和. 解法: 我看了这篇b ...
- 2017多校第10场 HDU 6181 Two Paths 次短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181 题意:给一个图,求出次短路. 解法:我之前的模板不能解决这种图,就是最短路和次短路相等的情况,证 ...
- 2017多校第10场 HDU 6180 Schedule 贪心,multiset
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6180 题意:给了一些任务的开始时间和终止时间,现在让我们安排k台及机器,让这些任务在k太机器上最小,并 ...
- 2017多校第10场 HDU 6178 Monkeys 贪心,或者DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:给出一棵有n个节点的树,现在需要你把k只猴子放在节点上,每个节点最多放一只猴子,且要求每只 ...
- 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...
随机推荐
- HTML5 应用程序缓存
使用HTML5,通过创建 cache manifest 文件,可以轻松创建web应用的离线缓存. 什么事应用程序缓存? HTML5引入了应用程序缓存,这意味着 web 应用可进行缓存,并在没有因特 ...
- BZOJ1023:[SHOI2008]仙人掌图——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1023 Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple ...
- 洛谷 P2272 [ZJOI2007]最大半连通子图 解题报告
P2272 [ZJOI2007]最大半连通子图 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v \in V\),满 ...
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
- HDOJ(HDU).2044-2049 递推专题
HDOJ(HDU).2044-2049 递推专题 点我挑战题目 HDU.2044 题意分析 先考虑递推关系:从1到第n个格子的时候由多少种走法? 如图,当n为下方格子的时候,由于只能向右走,所以有2中 ...
- 细谈select函数(C语言)
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect.accept.recv或recvfrom这样的阻塞程序( ...
- 洛谷P4198 楼房重建 (分块)
洛谷P4198 楼房重建 题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题, ...
- Google新出品的数据格式:Protocol Buffer
转:http://blog.csdn.net/carson_ho/article/details/70037693
- squid总结
squid可以完成的工作: 代理服务器 反向代理服务器 防火墙 缓存功能 透明代理 squid和varnish的对比,以及squid的优缺点说明: 缓存到硬盘,容易遇到I/O瓶颈 V3.2以下不支持多 ...
- SDUT3926 kmp
bLue的二叉树 Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Problem Description Keke 是一个喜爱种树的 ...