POJ 3468 A Simple Problem with Integers(线段树)
来源:互联网 发布:淘宝手机页面图片尺寸 编辑:程序博客网 时间:2024/06/05 17:06
题目地址:http://poj.org/problem?id=3468
思路:updata()区间增减,query()区间求和,会超int用long long
AC代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x3f3f3f3f;//1061109567typedef long long ll;const int maxn = 100010;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;ll add[maxn<<2];ll sum[maxn<<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] += (m -(m>>1)) * add[rt]; sum[rt<<1|1] += (m>>1) * add[rt]; add[rt] = 0; }}void build(int l,int r,int rt){ add[rt] = 0; if(l == r) { scanf("%I64d",&sum[rt]); return; } int m = (l + r) >> 1; build(lson); build(rson); pushup(rt);}void updata(int L,int R,int c,int l,int r,int rt){ if(l >= L && r <= R) { sum[rt] += (r-l+1) * c; add[rt] += c; return; } pushdown(rt,r-l+1);//查询或者更改的时候传递增量 int m = (l + r) >> 1; if(L <= m) updata(L,R,c,lson); if(R > m) updata(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 ans = 0; if(L <= m) ans += query(L,R,lson); if(R > m) ans += query(L,R,rson); return ans;}int main(){ int n,q; while(scanf("%d%d",&n,&q) != EOF) { build(1,n,1); char op[2]; for(int i=0; i<q; i++) { int a,b,c; scanf("%s",op); if(op[0] == 'C') { scanf("%d%d%d",&a,&b,&c); updata(a,b,c,1,n,1); } else { scanf("%d%d",&a,&b); printf("%I64d\n",query(a,b,1,n,1)); } } } return 0;}
0 0
- POJ 3468 A SIMPLE PROBLEM WITH INTEGERS(线段树)
- poj 3468 A Simple Problem with Integers ( 线段树 )
- POJ 3468 A Simple Problem with Integers (线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- poj 3468 A Simple Problem with Integers(线段树)
- POJ 3468-A Simple Problem with Integers(线段树)
- poj 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- poj 3468(线段树) A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers (线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers-(线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers (线段树)
- poj 3468 A Simple Problem with Integers 基础线段树
- POJ 3468 A Simple Problem with Integers 线段树
- 测试协同CS模式下的客户端操作通知时间 采用协议TCP
- dwz 弹出框分页实现方法
- hdoj 2073 无限的路【数学】
- #342Div2-625D Babaei and Birthday Cake线段树优化DP
- 开发优化--老板优化
- POJ 3468 A Simple Problem with Integers(线段树)
- linux有效修改max open files/ulimit -n
- 对多线程下载文件的理解和使用ksoap2-android调用WebService的操作步骤
- Hadoop学习笔记:MapReduce框架详解
- 外观模式
- 基于执行视图解析ELF
- ORACLE循环
- tcp的连接与关闭
- Canvas和Paint的那点事(1)