hdu 5861 (线段树,区间更新,单点查询)
来源:互联网 发布:java 回文字符串 编辑:程序博客网 时间:2024/04/20 11:10
There are n villages along a high way, and divided the high way into n-1 segments. Each segment would charge a certain amount of money for being open for one day, and you can open or close an arbitrary segment in an arbitrary day, but you can open or close the segment for just one time, because the workers would be angry if you told them to work multiple period.
We know the transport plan in the next m days, each day there is one cargo need to transport from village aiai to village bibi, and you need to guarantee that the segments between aiai and bibi are open in the i-th day. Your boss wants to minimize the total cost of the next m days, and you need to tell him the charge for each day.
(At the beginning, all the segments are closed.)
Input
Multiple test case. For each test case, begins with two integers n, m(1<=n,m<=200000), next line contains n-1 integers. The i-th integer wiwi(1<=wiwi<=1000) indicates the charge for the segment between village i and village i+1 being open for one day. Next m lines, each line contains two integers ai,bi(1≤ai,bi<=n,ai!=bi)ai,bi(1≤ai,bi<=n,ai!=bi).
Output
For each test case, output m lines, each line contains the charge for the i-th day.
Sample Input
4 3
1 2 3
1 3
3 4
2 4
Sample Output
3
5
5
知道是线段树后自己做出来的,哈哈哈,好开心呀,还有个蜜汁坑点= =,我这都找出来了。。出题人坑这个真无聊了 询问的L还有可能比R大
算一个自己以后用的板子吧
就是先统计出每一段路会出现在哪几天,保留最大最小值;
就是建两课线段树,第一棵是为了求线段树的最早出现和最晚出现
第二棵是下标是哪一天的id,更新就用 每一段路来进行更新,无敌优美
#include <bits/stdc++.h>using namespace std;const int N = 200010;int a[N],sum[N*4];int laz[N*4];int minn[N*4],maxx[N*4];void push_down(int rt,int m){ sum[rt<<1]+=laz[rt]*(m-(m>>1)); sum[rt<<1|1]+=laz[rt]*(m>>1); laz[rt<<1]+=laz[rt]; laz[rt<<1|1]+=laz[rt]; laz[rt]=0;}int n,m;void push_down2(int rt){ maxx[rt<<1]=max(maxx[rt<<1],maxx[rt]); minn[rt<<1]=min(minn[rt<<1],minn[rt]); maxx[rt<<1|1]=max(maxx[rt<<1|1],maxx[rt]); minn[rt<<1|1]=min(minn[rt<<1|1],minn[rt]);}void update(int L,int R,int l,int r,int rt,int add){ if(L<=l&&R>=r){ sum[rt]+=(r-l+1)*add; laz[rt]+=add; return ; } if(laz[rt]) push_down(rt,r-l+1); int mid=(l+r)>>1; if(L<=mid) update(L,R,l,mid,rt<<1,add); if(R>mid) update(L,R,mid+1,r,rt<<1|1,add);}void build(int l,int r,int rt){ if(l==r) { if(minn[rt]<=maxx[rt]) update(minn[rt],maxx[rt],1,m,1,a[l]); return ; } push_down2(rt); int mid=(l+r)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1);}void update2(int l,int r,int L,int R,int rt,int id){ if(L<=l&&R>=r){ maxx[rt]=max(maxx[rt],id); minn[rt]=min(minn[rt],id); return ; } push_down2(rt); int mid=(l+r)>>1; if(L<=mid) update2(l,mid,L,R,rt<<1,id); if(R>mid) update2(mid+1,r,L,R,rt<<1|1,id);}int query(int l,int r,int d,int rt){ if(l==r&&l==d) return sum[rt]; if(laz[rt]) push_down(rt,r-l+1); int mid=(l+r)>>1; if(d<=mid) return query(l,mid,d,rt<<1); else return query(mid+1,r,d,rt<<1|1);}int main(){ while(~scanf("%d%d",&n,&m)) { memset(sum,0,sizeof(sum)); memset(minn,0x3f,sizeof(minn)); memset(maxx,0,sizeof(maxx)); memset(laz,0,sizeof(laz)); n--; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=m;i++) { int L,R; scanf("%d%d",&L,&R); if(L>R) swap(L,R); update2(1,n,L,R-1,1,i); } build(1,n,1); for(int i=1;i<=m;i++) printf("%d\n",query(1,m,i,1) ); }}
- hdu 5861 (线段树,区间更新,单点查询)
- hdu 4046 Panda (线段树 单点更新 区间查询)
- HDU 1754 (线段树+单点更新+区间查询)
- HDU 1540——Tunnel Warfare(线段树,区间合并+单点更新+单点查询)
- HDU 2795 线段树(单点更新 区间查询)
- HDU 4819:单点更新,区间查询的二维线段树
- 线段树 单点更新查询 区间最大值 hdu 2795 Billboard
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
- zoj (单点更新区间查询:线段树)
- 线段树单点更新和区间查询
- hdu1166(线段树单点更新区间查询)
- hdu 4893 Wow! Such Sequence! 线段树单点更新+区间更新+区间查询
- HDU 4893 Wow! Such Sequence! (线段树单点更新+区间更新+区间查询+二分)
- HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)
- HDU 1166 敌兵布阵-线段树-(单点更新,区间查询)
- HDU 1556 给连续个球涂色-线段树-(区间更新,单点查询)
- hdu 1556 Color the ball(线段树 区间更新单点查询)
- HDU 1754 I Hate It(线段树单点更新区间最值查询)
- 【JavaScript】随笔2
- leetcode 107. Binary Tree Level Order Traversal II
- hadoop 伪分布式 web浏览器访问不到yarn
- 有序链表的插入
- 程序人生的四个象限和两条主线
- hdu 5861 (线段树,区间更新,单点查询)
- 【推荐】打造独一无二的电子商务平台-鸿鹄云商b2b b2c o2o
- JavaScript 获取到的Json数据传递给另一个JS函数
- wait, notify 和 notifyAll区别
- 自动无限轮播
- Ubuntu 16.04 fabric
- angular自定义过滤器
- CCF认证201703-1 分蛋糕
- 可靠事件系统Tarzan的设计与实现