Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1354    Accepted Submission(s): 496

Problem Description
Mr.
Cui is working off-campus and he misses his girl friend very much.
After a whole night tossing and turning, he decides to get to his girl
friend's city and of course, with well-chosen gifts. He knows neither
too low the price could a gift be since his girl friend won't like it,
nor too high of it since he might consider not worth to do. So he will
only buy gifts whose price is between [a,b].
There are n cities in
the country and (n-1) bi-directional roads. Each city can be reached
from any other city. In the ith city, there is a specialty of price ci
Cui could buy as a gift. Cui buy at most 1 gift in a city. Cui starts
his trip from city s and his girl friend is in city t. As mentioned
above, Cui is so hurry that he will choose the quickest way to his girl
friend(in other words, he won't pass a city twice) and of course, buy as
many as gifts as possible. Now he wants to know, how much money does he
need to prepare for all the gifts?
 
Input
There are multiple cases.

For each case:
The first line contains tow integers n,m(1≤n,m≤10^5), representing the number of cities and the number of situations.
The second line contains n integers c1,c2,...,cn(1≤ci≤10^9), indicating the price of city i's specialty.
Then n-1 lines follows. Each line has two integers x,y(1≤x,y≤n), meaning there is road between city x and city y.
Next
m line follows. In each line there are four integers
s,t,a,b(1≤s,t≤n;1≤a≤b≤10^9), which indicates start city, end city, lower
bound of the price, upper bound of the price, respectively, as the
exact meaning mentioned in the description above

 
Output
Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.
 
Sample Input
5 3
1 2 1 3 2
1 2
2 4
3 1
2 5
4 5 1 3
1 1 1 1
3 5 2 3
Sample Output
7 1 4
 
Source
分析:由于没有修改操作,一个显然的想法是离线处理所有问题
将询问拆成1-x,1-y,1-LCA(x,y),则处理的问题转化为从根到节点的链上的问题。
解决这个问题,我们可以在dfs时向treap插入当前的数,在退出时删除这个数,并且每次维护在该点上的答案。
当然也可以将所有的查询和点权排序,用树链剖分做这个题,在线段树上面插入就ok。
下面给出AC代码:
 #include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
vector<LL> G[];
typedef struct
{
LL id;
LL x, y;
LL l, r;
}Quer;
Quer s[], cnt[];
LL tre[];
LL n, val[], son[], fa[], siz[], dep[];
LL k, top[], rak[], id[], anl[], anr[];
bool comp1(Quer a, Quer b)
{
if(a.l<b.l)
return ;
return ;
}
bool compr(Quer a, Quer b)
{
if(a.r<b.r)
return ;
return ;
}
void Sechs(LL u, LL p)
{
LL v, i;
fa[u] = p;
dep[u] = dep[p]+;
for(i=;i<G[u].size();i++)
{
v = G[u][i];
if(v==p)
continue;
Sechs(v, u);
siz[u] += siz[v]+;
if(son[u]== || siz[v]>siz[son[u]])
son[u] = v;
}
}
void Sechr(LL u, LL p)
{
LL v, i;
top[u] = p;
rak[u] = ++k, id[k] = u;
if(son[u]==)
return;
Sechr(son[u], p);
for(i=;i<G[u].size();i++)
{
v = G[u][i];
if(v==son[u] || v==fa[u])
continue;
Sechr(v, v);
}
} void Update(LL l, LL r, LL x, LL a, LL b)
{
LL m;
if(l==r)
{
tre[x] += b;
return;
}
m = (l+r)/;
if(a<=m)
Update(l, m, x*, a, b);
else
Update(m+, r, x*+, a, b);
tre[x] = tre[x*]+tre[x*+];
}
LL Query(LL l, LL r, LL x, LL a, LL b)
{
LL m, sum = ;
if(l>=a && r<=b)
return tre[x];
m = (l+r)/;
if(a<=m)
sum += Query(l, m, x*, a, b);
if(b>=m+)
sum += Query(m+, r, x*+, a, b);
return sum;
}
LL TreQuery(LL x, LL y)
{
LL sum, p1, p2;
p1 = top[x], p2 = top[y], sum = ;
while(p1!=p2)
{
if(dep[p1]<dep[p2])
swap(p1, p2), swap(x, y);
sum += Query(, n, , rak[p1], rak[x]);
x = fa[p1], p1 = top[x];
}
if(dep[x]>dep[y])
swap(x, y);
sum += Query(, n, , rak[x], rak[y]);
return sum;
} int main(void)
{
LL i, j, x, y, q;
while(scanf("%lld%lld", &n, &q)!=EOF)
{
for(i=;i<=n;i++)
G[i].clear();
for(i=;i<=n;i++)
{
scanf("%lld", &val[i]);
cnt[i].id = i;
cnt[i].r = val[i];
}
sort(cnt+, cnt+n+, compr);
for(i=;i<=n-;i++)
{
scanf("%lld%lld", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
memset(siz, , sizeof(siz));
memset(son, , sizeof(son));
k = ;
Sechs(, );
Sechr(, );
for(i=;i<=q;i++)
{
scanf("%lld%lld%lld%lld", &s[i].x, &s[i].y, &s[i].l, &s[i].r);
s[i].id = i;
} sort(s+, s+q+, comp1);
memset(tre, , sizeof(tre));
j = ;
for(i=;i<=q;i++)
{
while(cnt[j].r<s[i].l && j<=n)
{
Update(, n, , rak[cnt[j].id], cnt[j].r);
j += ;
}
anl[s[i].id] = TreQuery(s[i].x, s[i].y);
} sort(s+, s+q+, compr);
memset(tre, , sizeof(tre));
j = ;
for(i=;i<=q;i++)
{
while(cnt[j].r<=s[i].r && j<=n)
{
Update(, n, , rak[cnt[j].id], cnt[j].r);
j += ;
}
anr[s[i].id] = TreQuery(s[i].x, s[i].y);
} printf("%lld", anr[]-anl[]);
for(i=;i<=q;i++)
printf(" %lld", anr[i]-anl[i]);
printf("\n");
}
return ;
}

2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】的更多相关文章

  1. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  2. 2017 Multi-University Training Contest - Team 1 1002&&hdu 6034

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. 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两节 ...

  4. 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

    Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  6. 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】

    CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  7. 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

随机推荐

  1. 【java】java.lang.Math:public static long round(double a)和public static int round(float a)

    package math; public class TestMath_round { public static void main(String[] args) { System.out.prin ...

  2. CSS图片文字同行居中

    img{ display:inline-block; vertical-align:middle; }

  3. Python学习日记:day2

    1.格式化输出 name = input("请输入你的名字:") age =input("请输入你的年龄:") job =input("请输入你的工作 ...

  4. Paho - MQTT C Cient的实现

    来自我的CSDN博客   在前几天,我大致了解了一下Paho C项目,并对其的一些内容进行了翻译.俗话说,光说不练假把戏,今天就给大家讲一下使用Paho的客户端库文件实现MQTT C Client的过 ...

  5. php-redis 操作类 封装

    <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...

  6. Linux下防火墙配置

    查看防火墙的状态:/etc/init.d/iptables  status  或  service  iptables  status 1) 临时生效,重启后复原 开启: service  iptab ...

  7. 在QComboBox的基础上实现复选功能

    这个是最近的一个项目上需要实现的功能.要求如下: 下拉列表的项目可以多选 显示框不能编辑 所选中的项目在显示框中出现 下面根据网上的提示代码(参照博客 一去二三里),主要实现如下代码(与参照略有不同) ...

  8. Head First设计模式之适配器模式

    一.定义 适配器模式把一个类的接口,变换成客户端所期待的另一种接口,使原本因接口不匹配的两个类能够在一起工作. 二.结构 角色: Client:用户类,使用新接口Target来完成某些特定的需求. T ...

  9. chrome调试工具高级不完整使用指南(基础篇)

    一.前言 本文记录的是作者在工作上面对chrome的一些使用和情况的分析分享,内容仅代表个人的观点.转发请注明出处(http://www.cnblogs.com/st-leslie/),谢谢合作 二. ...

  10. mac pycharm快捷键整理

    转自:http://www.jianshu.com/p/be0bdc02f7da (感谢整理,另外,简书似乎很不错,排版很nice.) Pycharm 快捷键 shift cmd + 展开所有 shi ...