HDU 6162 Ch’s gift (树剖 + 离线线段树)
Ch’s gift
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 662 Accepted Submission(s): 229
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?
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
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
【题意】给你一棵树,每个节点有一个权值,M次询问,给出u,v,a,b,求权值在区间[a,b]中的和。
【分析】数据实在是太水了,暴力都能过,这里说一下正确的树剖,离线往线段树插值得写法。将每个询问分成两部分,[1,b]-[1,a-1],放进一个 集合排序,从小到大取出,将节点权值同样 排序,若当前权值<=询问中的权值,则插入线段树。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N];
int num,n,m;
ll ans[N];
vector<int>edg[N];
vector<pii>vec;
struct query{
int u,v,x,id;
bool operator <(const query &d)const{
return x<d.x;
}
}q[N*];
void dfs1(int u, int f, int d) {
dep[u] = d;
siz[u] = ;
son[u] = ;
fa[u] = f;
for (int v : edg[u]) {
if (v == f) continue;
dfs1(v, u, d + );
siz[u] += siz[v];
if (siz[son[u]] < siz[v])
son[u] = v;
}
}
void dfs2(int u, int tp) {
top[u] = tp;
id[u] = ++num;
if (son[u]) dfs2(son[u], tp);
for (int v : edg[u]) {
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} struct Tree {
int l,r;
ll sum;
};
Tree tree[*N];
void pushup(int x) {
tree[x].sum=tree[lson(x)].sum+tree[rson(x)].sum;
}
void build(int l,int r,int v) {
tree[v].l=l;
tree[v].r=r;
if(l==r) {
tree[v].sum=;
return ;
}
int mid=(l+r)>>;
build(l,mid,v*);
build(mid+,r,v*+);
pushup(v);
}
void update(int o,int v,int val) {
if(tree[o].l==tree[o].r) {
tree[o].sum= val;
return ;
}
int mid = (tree[o].l+tree[o].r)/;
if(v<=mid)
update(o*,v,val);
else
update(o*+,v,val);
pushup(o);
}
ll querySum(int x,int l,int r) {
if (tree[x].l >= l && tree[x].r <= r) {
return tree[x].sum;
}
int mid = (tree[x].l + tree[x].r) / ;
ll ans = ;
if (l <= mid) ans += querySum(lson(x),l,r);
if (r > mid) ans += querySum(rson(x),l,r);
return ans;
}
ll Qsum(int u,int v) {
int tp1 = top[u], tp2 = top[v];
ll ans = ;
while (tp1 != tp2) {
if (dep[tp1] < dep[tp2]) {
swap(tp1, tp2);
swap(u, v);
}
ans +=querySum(,id[tp1], id[u]);
u = fa[tp1];
tp1 = top[u];
}
if (dep[u] > dep[v])swap(u, v);
ans +=querySum(,id[u], id[v]);
return ans;
}
void init(){
for(int i=;i<N;i++)edg[i].clear();
met(tree,);met(son,);vec.clear();met(ans,);
}
int main() {
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=,x; i<=n; i++)scanf("%d",&x),vec.pb(mp(x,i));
for(int i=,u,v; i<n; i++) {
scanf("%d%d",&u,&v);
edg[u].pb(v);edg[v].pb(u);
}
num = ;
dfs1(,,);
dfs2(,);
sort(vec.begin(),vec.end());
for(int i=;i<=m;i++){
int x,y,a,b;
scanf("%d%d%d%d",&x,&y,&a,&b);
q[i]=query{x,y,a-,-i};
q[i+m]=query{x,y,b,i};
}
sort(q+,q+*m+);
int now=;
build(,num,);
for(int i=;i<=*m;i++){
while(now<n&&vec[now].first<=q[i].x){
update(,id[vec[now].second],vec[now].first);
now++;
}
ans[abs(q[i].id)]+=Qsum(q[i].u,q[i].v)*(q[i].id>?:-);
}
for(int i=;i<=m;i++)printf("%lld%c",ans[i],i==m?'\n':' ');
}
return ;
}
HDU 6162 Ch’s gift (树剖 + 离线线段树)的更多相关文章
- BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)
BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...
- 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两节 ...
- 【小技巧】树剖套线段树优化建图如何做到 O(nlogn)
前提:用树剖套线段树优化树链连边.例题:bzoj4699 我们说树剖的时间复杂度是 $O(n\times log(n))$,是因为访问一条链时需要经过 $log(n)$ 级别条重链,对于每条重链还需要 ...
- HDU 6162 Ch’s gift
Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing a ...
- 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...
- 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 ...
- HDU 6162 Ch’s gift (线段树+树链剖分)
题意:给定上一棵树,每个树的结点有一个权值,有 m 个询问,每次询问 s, t , a, b,问你从 s 到 t 这条路上,权值在 a 和 b 之间的和.(闭区间). 析:很明显的树链剖分,但是要用 ...
- HDU 6162 Ch's gift(树链剖分+线段树)
题意: 已知树上的每个节点的值和节点之间的关系建成了一棵树,现在查询节点u到节点v的最短路径上的节点值在l到r之间的节点值的和. 思路: 用树链剖分将树映射到线段树上,线段树上维护3个值,max,mi ...
- BZOJ 3626: [LNOI2014]LCA(树剖+差分+线段树)
传送门 解题思路 比较有意思的一道题.首先要把求\(\sum\limits_{i=l}^r dep[lca(i,z)]\)这个公式变一下.就是考虑每一个点的贡献,做出贡献的点一定在\(z\)到根节点的 ...
随机推荐
- 选择排序Selection sort
顾名思意,就是直接从待排序数组里选择一个最小(或最大)的数字,每次都拿一个最小数字出来, 顺序放入新数组,直到全部拿完 再简单点,对着一群数组说,你们谁最小出列,站到最后边 然后继续对剩余的无序数组说 ...
- go通过名称来调用对应的方法
仅仅是为了学习go语言中的反射. package main import ( "errors" "fmt" "reflect" ) func ...
- Redis 模糊匹配 SearchKeys
语法:KEYS pattern说明:返回与指定模式相匹配的所用的keys.该命令所支持的匹配模式如下:(1)?:用于匹配单个字符.例如,h?llo可以匹配hello.hallo和hxllo等:(2)* ...
- CentOS7 搭建Apache环境
一.安装 yum -y install httpd 二.配置 主路径:/etc/httpd/ Apache目录说明 1 2 3 4 5 6 drwxr-xr-x 2 root root 4096 Ma ...
- 限制printk打印频率函数printk_ratelimit【转】
转自:http://blog.csdn.net/lkkey80/article/details/45190095 版权声明:博文地址 http://blog.csdn.net/lkkey80?view ...
- 使用makecontext实现用户线程【转】
转自:http://blog.csdn.net/cyberlabs/article/details/6920138 使用makecontext实现用户线程 现代Unix系统都在ucontext.h中提 ...
- mount/umount命令【转】
转自:http://www.cnblogs.com/qq78292959/archive/2012/03/06/2382334.html 如果想在运行的Linux下访问其它文件系统中的资源的话,就要用 ...
- vsftp 服务的启动与问题
一般系统用户是可以直接登入的如果不可以可能是selinux的原因 执行一下: 更改selinux的配置文件将其设为disable,可我不想重启服务器,有以下解决办法:执行命令:setenforce 0 ...
- 一文看懂IC芯片生产流程:从设计到制造与封装
http://blog.csdn.net/yazhouren/article/details/50810114 芯片制造的过程就如同用乐高盖房子一样,先有晶圆作为地基,再层层往上叠的芯片制造流程后,就 ...
- Educational Codeforces Round 23 补题小结
昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...