poj3468A Simple Problem with Integers
来源:互联网 发布:fifa17 淘宝 编辑:程序博客网 时间:2024/05/19 03:21
题目大意:
区间更新,区间查找。
解题思路:
注意数据范围,然后线段树lazy标记就行了,很基础,没什么技巧。
#include<stdio.h>#define N 100005#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define node rt,l,r__int64 value[N];struct Node{ __int64 k; __int64 v;};Node tree[N*4];void up(int rt){ tree[rt].v=tree[rt<<1].v+tree[rt<<1|1].v;}void build(int rt,int l,int r){ tree[rt].k=0; if(l==r) { tree[rt].v=value[l]; return ; } int mid=(l+r)>>1; build(lson); build(rson); up(rt);}void down(int rt,int l,int r){ long long k=tree[rt].k;//这个地方没有用64错了好多次。。。 if(k!=0) { tree[rt<<1].k+=k;tree[rt<<1|1].k+=k;///不能可能前面待更新的不止一个 int mid=(l+r)>>1; tree[rt<<1].v+=(mid-l+1)*k; tree[rt<<1|1].v+=(r-mid)*k; tree[rt].k=0; }}__int64 Query(int rt, int l, int r, int a, int b){ if(a<=l&&r<=b) { return tree[rt].v; } down(node); int mid=(l+r)>>1; if(b<=mid) return Query(lson,a,b); else if(a>mid) return Query(rson,a,b); else { return Query(lson,a,b)+Query(rson,a,b); } up(rt);}void add(int rt,int l,int r,int a,int b,__int64 c){ if(l>=a&&r<=b) { tree[rt].k+=c; tree[rt].v+=(r-l+1)*c; return ; } down(node); int mid=(l+r)>>1; if(b<=mid) add(lson,a,b,c); else if(a>mid) add(rson,a,b,c); else{ add(lson,a,b,c); add(rson,a,b,c); } up(rt);}int main(){ int n,m,i,a,b; __int64 c,ans; char op[10]; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%I64d",&value[i]); build(1,1,n); while(m--){ scanf("%s",&op); if(op[0]=='Q'){ scanf("%d%d",&a,&b); ans=Query(1,1,n,a,b); printf("%I64d\n",ans); }else{ scanf("%d%d%I64d",&a,&b,&c); add(1,1,n,a,b,c); } } return 0;}
0 0
- poj3468A Simple Problem with Integers
- poj3468A Simple Problem with Integers
- POJ3468A Simple Problem with Integers
- poj3468A Simple Problem with Integers
- poj3468A Simple Problem with Integers Splay版
- poj3468A Simple Problem with Integers 线段树
- POJ3468A Simple Problem with Integers线段树
- poj3468A Simple Problem with Integers(线段树成段更新)
- POJ3468A Simple Problem with Integers(线段树成段更新--Lazy标记)
- 【线段树入门】poj3468A Simple Problem with Integers
- poj3468A Simple Problem with Integers(线段树,在段更新时要注意)
- poj3468A Simple Problem with Integers(线段树+成段更新)
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- poj2346_A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- opencv 遍历指定路径下所有文件的内容
- 04_android入门_pull解析
- 设计模式C++实现——策略模式
- 三层架构-------理论篇
- 苹果Swift编程语言入门教程【中文版】3
- poj3468A Simple Problem with Integers
- Mantis 安装方法
- 重启nginx后丢失nginx.pid,如何重新启动nginx
- uva 590 Always on the run
- 安装xcode6 beta 后调试出现Unable to boot the iOS Simulator以及编译苹果官方Swift的demo报错failed with exit code 1的解决办法
- 图片局部链接的写法
- Linux内核开发之Helloworld
- System.getProperties中的所有属性
- 怎么让自己平静下来!