树的区间查询与更新(线段树)
来源:互联网 发布:listview的优化方案 编辑:程序博客网 时间:2024/05/13 15:24
原题:http://oj.51isoft.com/bnuoj/problem_show.php?pid=14223
#include <iostream>#include <stdio.h>#include <string.h>#include <map>#include <math.h>#include <set>#include <vector>#include <algorithm>using namespace std;#define CLR(x) memset(x,0,sizeof(x))#define LL long long#define typec double#define lson l , m , rt << 1#define rson m+1 , r , rt << 1 | 1const int N = 100000;LL add[N<<2];LL sum[N<<2];void PushUp(int rt){ sum[rt]=sum[rt << 1] + sum[rt<<1 | 1];}void PushDown(int rt,int m){ if (add[rt]){ add[rt<<1]+=add[rt]; add[rt<<1 | 1]+=add[rt]; sum[rt<<1] += (add[rt] * (m-(m>>1))); sum[rt<<1 | 1]+=add[rt]*(m>>1); add[rt]=0; }}void build(int l,int r,int rt){ add[rt]=0; if (l==r){ sum[rt]=0; return; } int m=(l + r)>>1; build(lson); build(rson); PushUp(rt);}LL ans[N];void output(int l,int r,int rt){ if (l==r){ ans[l]=sum[rt]; return; } PushDown(rt,r-l+1); int m=(l+r)>>1; output(lson); output(rson); PushUp(rt);}void update(int L ,int R,LL c,int l,int r,int rt){ if (L<=l && r<=R){ add[rt]+=c; sum[rt]+=c*(r-l+1LL); return; } PushDown(rt,r-l+1); int m=(l + r)>>1; if (L<=m) update(L,R,c,lson); if (m<R) update(L,R,c,rson); PushUp(rt);}LL query(int L ,int R ,int l ,int r ,int rt){ if (L<=l && r<=R) return sum[rt]; PushDown(rt,r-l+1); int m=(l + r)>>1; LL ret=0; if (L<=m) ret+=query(L,R,lson); if (m<R) ret+=query(L,R,rson); return ret;}LL pay[N];int n,m;char str[100];int head[N],tot;int order[N][2],o;struct E{ int to,next;}edge[N];void addedge(int a,int b){ edge[tot].to=b; edge[tot].next=head[a]; head[a]=tot++;}int dfs(int x){ order[x][0]=++o;order[x][1]=o; for (int i=head[x];i!=-1;i=edge[i].next){ int temp=dfs(edge[i].to); order[x][1]=temp; } return order[x][1];}void solve(){ memset(head,-1,sizeof(head)); tot=0; for (int i=1;i<n;++i){ int temp; scanf("%d%lld",&temp,&pay[i]); addedge(temp,i); } o=0; dfs(0); build(1,n,1); for (int i=0;i<n;++i) update(order[i][0],order[i][0],pay[i],1,n,1); while(m--){ int a;LL b,c; scanf("%s%d%lld%lld",str,&a,&b,&c); if (str[0]=='e'){ LL res=query(order[a][0],order[a][0],1,n,1); if (res<b) update(order[a][0],order[a][0],c,1,n,1); } else{ LL res=query(order[a][0],order[a][1],1,n,1); if (res<b*(order[a][1]-order[a][0]+1LL)) update(order[a][0],order[a][1],c,1,n,1); } } output(1,n,1); for (int i=0;i<n;++i) printf("%lld\n",ans[order[i][0]]);}int main(){ // freopen("in","r",stdin); while (~scanf("%d%d%lld",&n,&m,&pay[0])) solve();}
- 树的区间查询与更新(线段树)
- 线段树--区间更新区间查询--hdu4027
- 线段树区间查询区间更新
- POJ-3468(线段树区间更新区间查询)
- hdu 5023(线段树区间更新+区间查询)
- codevs 4919 线段树:区间更新,区间查询(余数)
- 线段树(区间更新与区间查询)——Just a Hook ( HDU 1698 )
- [模板]线段树的建树、查询、单点更新、区间更新
- HDU 3577 Fast Arrangement(线段树功能:区间更新,查询区间的最大覆盖次数)
- zoj (单点更新区间查询:线段树)
- HDU1698线段树区间更新,一次查询
- 线段树单点更新和区间查询
- hdu1166(线段树单点更新区间查询)
- HDU 4819:单点更新,区间查询的二维线段树
- HDU1166线段树的点更新区间查询
- UESTC 1425 求任意区间的LIS 线段树区间更新区间查询
- 线段树的区间更新
- 线段树的区间更新
- 微软亚洲研究院新推英库拼音输入法
- nyoj 287 radar (区间选点)
- ACM训练表
- 腾讯笔试题
- 数据库之宏观总结
- 树的区间查询与更新(线段树)
- 对fl2440的时钟,看门狗,内存等等初始化汇编程序的理解
- MBA,程序员的转机
- C++多核高级编程 - 09 并发模型之 五 黑板 Blackboard
- JAVA JNDI简介
- 使用GDAL获取网络数据
- 将一个文件夹下的所有文件复制到另一个文件夹下
- 将任一个数字进行拆解
- adb issues