poj3468 A Simple Problem with Integers 成段更新
来源:互联网 发布:怎么给淘宝店做宣传 编辑:程序博客网 时间:2024/06/10 11:02
跟上一题的成段更新有所不同的是 此处的更新不是直接变成某一个值 而是在原来的基础上加上某一个值
所以 标记的变量也有所改变 是在原来的基础上加上那个值 (一开始 我以为标记变量只是标记用的 具体的加减在sum变量里进行 导致 答案错误)
另外这道题目 数据比较大,超过了int 范围注意到了sum的范围 但是没有注意到标记变量 col 的范围 导致答案错误
#include <iostream>#include<cstdio>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1long long int sum[440000];long long int col[440000];//long long int 型long long int an;void PushUp(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void PushDown(int rt,int m){ if(col[rt]!=0) { col[rt<<1]+=col[rt];//标记变量应该在原来的基础上加上新变化的值 col[rt<<1|1]+=col[rt]; sum[rt<<1]=sum[rt<<1]+(m-(m>>1))*col[rt]; sum[rt<<1|1]=sum[rt<<1|1]+(m>>1)*col[rt]; col[rt]=0; }}void build(int l,int r,int rt){ col[rt]=0; if(l==r) { scanf("%lld",&sum[rt]); return; } int m=(l+r)>>1; build(lson); build(rson); PushUp(rt);}void UpDate(int c,int d,int num,int l,int r,int rt){ if(c<=l&&r<=d) { col[rt]+=num;//在原来的基础上标记变量相应的加上变化的值 sum[rt]+=(long long )num*(r-l+1); return; } PushDown(rt,r-l+1); int m=(l+r)>>1; if(m>=c)UpDate(c,d,num,lson); if(m<d)UpDate(c,d,num,rson); PushUp(rt);}void query(int c,int d,int l,int r,int rt){ if(c<=l&&r<=d) { an+=sum[rt]; return; } PushDown(rt,r-l+1);//每次遇到有延时标记的节点是都应该相应更新它的子节点 int m=(l+r)>>1; if(m>=c)query(c,d,lson); if(m<d)query(c,d,rson);}int main(){ int n,m,i,a,b,c; char s[5]; scanf("%d%d",&n,&m); build(1,n,1); while(m--) { scanf("%s",s); if(s[0]=='Q') { scanf("%d%d",&a,&b); an=0; query(a,b,1,n,1); printf("%lld\n",an); } else if(s[0]=='C') { scanf("%d%d%d",&a,&b,&c); UpDate(a,b,c,1,n,1); } } return 0;}
- poj3468 A Simple Problem with Integers 成段更新
- poj3468 A Simple Problem with Integers,成段更新
- POJ3468--A Simple Problem with Integers(成段更新)
- poj3468 A Simple Problem with Integers(成段更新)
- POJ3468 A Simple Problem with Integers 线段树 区间成段更新+区间求和
- poj3468 A Simple Problem with Integers 线段树,成段更新
- POJ3468 A Simple Problem with Integers 【线段树】+【成段更新】
- POJ3468-A Simple Problem with Integers(线段树 成段更新求和)
- poj3468 A Simple Problem with Integers[线段树成段更新成段查询]
- POJ3468 A simple problem with integers(区间更新)
- poj3468 A Simple Problem with Integers 线段树区间更新
- poj3468 A Simple Problem with Integers(线段树成段更新)
- 成段加值更新 区间求和 poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers 线段树 延时更新
- POJ3468 A Simple Problem with Integers 线段树成段更新
- Poj3468 A Simple Problem with Integers 线段树、区间更新
- POJ3468 A Simple Problem with Integers 线段树成段更新
- POJ3468 A Simple Problem with Integers(区间更新)
- 学习linux分区/dev/shm的应用
- Android 系统广播大全
- RTP 协议
- QMap
- 07-php雇员管理系统-分层模式实现登录,分页
- poj3468 A Simple Problem with Integers 成段更新
- read()和write()函数简介
- Spring Batch Example – Hello World Project
- Oracle分区表
- GCC使用详解
- jQuery.ajax中success 和complete 区别
- Ubuntu下的PHP开发环境架设
- VC中给菜单项增加响应函数
- lua 基本语法