Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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
分析:离线按点权排序,然后树剖;
   或者离散化点权主席树维护有序区间;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls rt<<1
#define rs rt<<1|1
#define all(x) x.begin(),x.end()
const int maxn=1e5+;
const int N=5e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=f*p%mo;p=p*p%mo;q>>=;}return f;}
int n,m,k,t,a[maxn],fa[maxn],dep[maxn],son[maxn],pos[maxn],bl[maxn],id[maxn],dfs_cl,tot,head[maxn],cnt;
ll ret[maxn],sum[maxn<<];
struct node
{
int l,r,id,x;
bool operator<(const node&p)const
{
return x<p.x;
}
}qu[maxn<<];
struct node1
{
int to,nxt;
}e[maxn<<];
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].nxt=head[x];
head[x]=cnt++;
}
bool cmp(int x,int y)
{
return a[x]<a[y];
}
void dfs1(int x,int y)
{
dep[x]=dep[y]+;
fa[x]=y;
son[x]=;
for(int i=head[x];i!=-;i=e[i].nxt)
{
int z=e[i].to;
if(z==y)continue;
dfs1(z,x);
son[x]+=son[z];
}
}
void dfs2(int x,int y,int ch)
{
int ma=;
pos[x]=++dfs_cl;
bl[x]=ch;
for(int i=head[x];i!=-;i=e[i].nxt)
{
int z=e[i].to;
if(z==y)continue;
if(son[z]>son[ma])ma=z;
}
if(ma!=)dfs2(ma,x,ch);
for(int i=head[x];i!=-;i=e[i].nxt)
{
int z=e[i].to;
if(z==y||z==ma)continue;
dfs2(z,x,z);
}
}
void build(int l,int r,int rt)
{
sum[rt]=;
if(l==r)return;
int mid=l+r>>;
build(l,mid,ls);
build(mid+,r,rs);
}
void pup(int rt)
{
sum[rt]=sum[ls]+sum[rs];
}
void upd(int x,int y,int l,int r,int rt)
{
if(x==l&&x==r)
{
sum[rt]+=y;
return;
}
int mid=l+r>>;
if(x<=mid)upd(x,y,l,mid,ls);
else upd(x,y,mid+,r,rs);
pup(rt);
}
ll Query(int L,int R,int l,int r,int rt)
{
if(L==l&&R==r)return sum[rt];
int mid=l+r>>;
if(R<=mid)return Query(L,R,l,mid,ls);
else if(L>mid)return Query(L,R,mid+,r,rs);
else return Query(L,mid,l,mid,ls)+Query(mid+,R,mid+,r,rs);
}
ll gao(int x,int y)
{
ll ret=;
while(bl[x]!=bl[y])
{
if(dep[bl[x]]<dep[bl[y]])swap(x,y);
ret+=Query(pos[bl[x]],pos[x],,n,);
x=fa[bl[x]];
}
if(pos[x]>pos[y])swap(x,y);
ret+=Query(pos[x],pos[y],,n,);
return ret;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
rep(i,,n)scanf("%d",&a[i]),id[i]=i,head[i]=-;
cnt=;
rep(i,,n-)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
tot=;
rep(i,,m)
{
int x,y,z,w;
ret[i]=;
scanf("%d%d%d%d",&x,&y,&z,&w);
qu[++tot]=node{x,y,-i,z-};
qu[++tot]=node{x,y,i,w};
}
dfs_cl=;
dfs1(,);
dfs2(,,);
sort(qu+,qu+tot+);
sort(id+,id+n+,cmp);
build(,n,);
int now=;
rep(i,,tot)
{
while(now<=n&&a[id[now]]<=qu[i].x)
{
upd(pos[id[now]],a[id[now]],,n,);
++now;
}
if(qu[i].id<)ret[-qu[i].id]-=gao(qu[i].l,qu[i].r);
else ret[qu[i].id]+=gao(qu[i].l,qu[i].r);
}
rep(i,,m)printf("%lld%c",ret[i],i==m?'\n':' ');
}
return ;
}

最新文章

  1. js array push 添加内容
  2. android 缓存Bitmap 使用内存缓存
  3. 模板短信接口调用java,pythoy版(一) 网易云信
  4. 接入多家ERP厂商,美团点评餐饮高速路开启
  5. 脊柱外科病人资料管理系统的界面设计分析(2)--JOA评分记录的实现
  6. chrome 修改标签页
  7. php验证手机号码
  8. SQlSERVER生成唯一编号
  9. iOS 使用SBJSON创建和解析JSON
  10. Ajax 实现无刷新页面
  11. pycharm远程调试服务器
  12. volatile与synchronized有什么区别?
  13. python+selenium页面自动化 元素定位实际遇到的各种问题(持续更新)
  14. vsCode 设置vue 保存自动格式化代码
  15. RabbitMQ 在 web 页面 创建 exchange, queue, routing key
  16. ACM-ICPC 2018 焦作赛区网络预赛 K Transport Ship (多重背包)
  17. BZOJ.3631.[JLOI2014]松鼠的新家(树上差分)
  18. JodaTimeUtil日期处理工具类(简单、实用)
  19. [Hbase]Hbase技术方案
  20. MVC源码分析 - ModelBinder绑定 / 自定义数据绑定

热门文章

  1. AAC的AudioSpecificConfig细节
  2. 使用centos 5.x 32位系统安装astgo 2014 v7.0教程(含全套安装文件)
  3. PID204特种部队
  4. Centos 7 安装google 浏览器(yum 方式)
  5. String,StringBuffer和StringBuilder
  6. Android源码下载方法
  7. Linux命令(002) -- free
  8. 322 Coin Change 零钱兑换
  9. Laravel5.1学习笔记13 系统架构5 Contract
  10. css文本背景样式