poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://poj.org/problem?id=3468
Description
You need to deal with two kinds of operations. One type of operation is
to add some given number to each number in a given interval. The other
is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
Sample Input
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
55
9
15
HINT
题意
区间加,区间查询和
题解:
线段树,记住懒操作~
代码:
- //qscqesze
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <ctime>
- #include <iostream>
- #include <algorithm>
- #include <set>
- #include <vector>
- #include <sstream>
- #include <queue>
- #include <typeinfo>
- #include <fstream>
- #include <map>
- #include <stack>
- typedef long long ll;
- using namespace std;
- //freopen("D.in","r",stdin);
- //freopen("D.out","w",stdout);
- #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
- #define maxn 200001
- #define mod 10007
- #define eps 1e-9
- int Num;
- char CH[];
- //const int inf=0x7fffffff; //нчоч╢С
- const int inf=0x3f3f3f3f;
- /*
- inline void P(int x)
- {
- Num=0;if(!x){putchar('0');puts("");return;}
- while(x>0)CH[++Num]=x%10,x/=10;
- while(Num)putchar(CH[Num--]+48);
- puts("");
- }
- */
- //**************************************************************************************
- inline ll read()
- {
- ll x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- inline void P(int x)
- {
- Num=;if(!x){putchar('');puts("");return;}
- while(x>)CH[++Num]=x%,x/=;
- while(Num)putchar(CH[Num--]+);
- puts("");
- }
- struct node
- {
- int l,r;
- ll sum,add;
- void fun(ll tmp)
- {
- add+=tmp;
- sum+=(r-l+)*tmp;
- }
- }a[maxn*];
- ll d[maxn];
- void relax(int x)
- {
- if(a[x].add)
- {
- a[x<<].fun(a[x].add);
- a[x<<|].fun(a[x].add);
- a[x].add=;
- }
- }
- void build(int x,int l,int r)
- {
- a[x].l=l,a[x].r=r;
- if(l==r)
- {
- a[x].sum=d[l];
- }
- else
- {
- int mid=(l+r)>>;
- build(x<<,l,mid);
- build(x<<|,mid+,r);
- a[x].sum=a[x<<].sum+a[x<<|].sum;
- }
- }
- void update(int x,int st,int ed,ll c)
- {
- int l=a[x].l,r=a[x].r;
- if(st<=l&&r<=ed)
- a[x].fun(c);
- else
- {
- relax(x);
- int mid=(l+r)>>;
- if(st<=mid)update(x<<,st,ed,c);
- if(ed>mid) update(x<<|,st,ed,c);
- a[x].sum=a[x<<].sum+a[x<<|].sum;
- }
- }
- ll query(int x,int st,int ed)
- {
- int l=a[x].l,r=a[x].r;
- if(st<=l&&r<=ed)
- return a[x].sum;
- else
- {
- relax(x);
- int mid=(l+r)>>;
- ll sum1=,sum2=;
- if(st<=mid)
- sum1=query(x<<,st,ed);
- if(ed>mid)
- sum2=query(x<<|,st,ed);
- return sum1+sum2;
- }
- }
- int main()
- {
- int n=read(),m=read();
- for(int i=;i<=n;i++)
- d[i]=read();
- build(,,n);
- char s[];
- int bb,cc,dd;
- for(int i=;i<m;i++)
- {
- scanf("%s",s);
- if(s[]=='Q')
- {
- bb=read(),cc=read();
- printf("%lld\n",query(,bb,cc));
- }
- else
- {
- bb=read(),cc=read(),dd=read();
- update(,bb,cc,dd);
- }
- }
- }
poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和的更多相关文章
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 A Simple Problem with Integers 线段树区间修改
http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和 C A B ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- python基础之常用的高阶函数
前言 高阶函数指的是能接收函数作为参数的函数或类:python中有一些内置的高阶函数,在某些场合使用可以提高代码的效率. map() map函数可以把一个迭代对象转换成另一个可迭代对象,不过在pyth ...
- 安装Https证书
安装证书 IIS 6 支持PFX格式证书,下载包中包含PFX格式证书和密码文件.以沃通证书为例: 文件说明: 1. 证书文件214083006430955.pem,包含两段内容,请不要删除任何一段内容 ...
- Mathtype公式位置偏上
Mathtype公式位置偏上 部分Mathtype公式与文档文字没有很好的对齐,而是浮起来了,也就是说Mathtype公式的位置比正常文字稍高,这是我写论文时碰到的一个很麻烦的问题.然后就是行距稍微大 ...
- html基础-css-选择器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 渗透常用SQL注入语句合集
1.判断有无注入点; and 1=1 and 1=2 2.猜表一般的表的名称无非是admin adminuser user pass password 等..and 0<>(select ...
- #ifndef的用法
作用:防止头文件的重复包含和编译 定义 #ifndef x #define x ... #endif 这是宏定义的一种,它可以根据是否已经定义了一个变量来进行分支选择,一般用于调试等等.实际上确切的说 ...
- appium+python自动化39-adb shell输入中文(ADBKeyBoard)
前言 上一篇提到"adb shell input textyoyo" 可以通过adb 输入英文的文本,由于不支持unicode编码,所以无法输入中文,github上有个国外的大神写 ...
- 常用对称加密算法(DES/AES)类(PHP)
看注释,啥也不说了,欢迎各种跨平台测试! /** * 常用对称加密算法类 * 支持密钥:64/128/256 bit(字节长度8/16/32) * 支持算法:DES/AES(根据密钥长度自动匹配使用: ...
- webpack 入门总结和实践(按需异步加载,css单独打包,生成多个入口文件)
为什么是webpack webpack一下自己就
- 面试的65个回答技巧-适用于BAT公司
互联网职业群分享的资料,里面大多是BAT公司的人,很多是猎头.这些技巧对于职场人来说,是非常宝贵的. 1.请你自我介绍一下你自己? 回答提示:一般人回答这个问题过于平常,只说姓名.年龄.爱好.工作经验 ...