POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 112228 | Accepted: 34905 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. 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
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
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
4
55
9
15 题意就是如题,对线段树的区间进行更新,可增可减可修改,并查询区间和,此题就是增,具体实现看代码吧.
AC代码:
Source Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define maxsize 100005
#define LL long long
LL num[maxsize];
struct node
{
LL l,r;
LL maxn,add;
int mid()
{
return (l+r)/;
}
} tree[maxsize<<];
void Build_Tree(LL root,LL l,LL r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].add=;
if(l==r)
{
tree[root].maxn=num[l];
return ;
}
Build_Tree(root*,l,tree[root].mid());
Build_Tree(root*+,tree[root].mid()+,r);
tree[root].maxn=tree[root*].maxn+tree[root*+].maxn;
}
void Update(LL root,LL l,LL r,LL czp)
{
if(tree[root].l==l&&tree[root].r==r)
{
tree[root].add+=czp;
return ;
}
tree[root].maxn+=((r-l+)*czp);
if(tree[root].mid()>=r) Update(root<<,l,r,czp);
else if(tree[root].mid()<l) Update(root<<|,l,r,czp);
else
{
Update(root<<,l,tree[root].mid(),czp);
Update(root<<|,tree[root].mid()+,r,czp);
}
}
long long Query(LL root,LL l,LL r)
{
if(tree[root].l==l&&tree[root].r==r)
{
return tree[root].maxn+(r-l+)*tree[root].add;
}
tree[root].maxn+=(tree[root].r-tree[root].l+)*tree[root].add;
Update(root<<,tree[root].l,tree[root].mid(),tree[root].add);
Update(root<<|,tree[root].mid()+,tree[root].r,tree[root].add);
tree[root].add=;
if(tree[root].mid()>=r) return Query(root*,l,r);
else if(tree[root].mid()<l) return Query(root*+,l,r);
else
{
LL Lans=Query(root*,l,tree[root].mid());
LL Rans=Query(root*+,tree[root].mid()+,r);
return Lans+Rans;
}
}
int main()
{
/*ios::sync_with_stdio(false);*/
char str[];
LL m,n,a,b,c;
while(scanf("%lld%lld",&m,&n)!=EOF)
{
for(LL i=; i<=m; i++) scanf("%I64d",&num[i]);
Build_Tree(,,m);
for(LL i=; i<=n; i++)
{
scanf("%s%I64d%I64d",str,&a,&b);
if(str[]=='Q') printf("%I64d\n",Query(,a,b));
else if(str[]=='C')
{
scanf("%I64d",&c);
Update(,a,b,c);
}
}
}
return ;
}
POJ 3468A Simple Problem with Integers(线段树区间更新)的更多相关文章
- 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 , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [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 线段树区间更新
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(线段树区间更新)
题目地址: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<< ...
随机推荐
- 03 解析库之Beautifulsoup模块
Beautifulsoup模块 一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...
- MYSQL 问题小总结
mysql 问题小总结 1.MySQL远程连接ERROR 2003(HY000):Can't connect to MySQL server on ‘ip’(111)的问题 通常是mysql配置文件中 ...
- 20155232 2016-2017-3 《Java程序设计》第9周学习总结
20155232 2016-2017-3 <Java程序设计>第9周学习总结 教材学习内容总结 第16章 JDBC(Java DataBase Connectivity)即java数据库连 ...
- SQLInjection 靶场配置
对于渗透,太小型的网站没有太大价值,而大型网站(比如各种电商平台)对于代码审计往往非常严格,新手基本找不到漏洞,而一些比较容易搞掉的站点(政府.gov.各种教育网站.edu或者很多商业中型站点)渗透又 ...
- C语言程序设计50例(一)(经典收藏)
[程序1]题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. # ...
- Struts has detected an unhandled exception
这个问题是struts和jsp页面的配置之间产生了问题,就是struts里边的名字和jsp里用的名字不是同一个名字,所以无法识别,所以检查一下里边的命名.
- HDU 3361 ASCII (水题,不说什么了)
题意:给你n个十进制数,让你输出相应的ASCII. 析:无,没说的,直接输出就好了. 代码如下: #include <iostream> #include <cstdio> # ...
- RESTful架构概念
本文转载自:http://www.ruanyifeng.com/blog/2011/09/restful.html 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软 ...
- windows下命令提示符中有空格路径的解决方法
1)用缩写.比如c:\Program Files 缩写为c:\Progra~1 再来刨根问底查查这个命名是否有规则,于是找到: 文件夹(sub-directry)名称,以前是不允许带空白的,后来允许带 ...
- Codeforces807 B. T-Shirt Hunt 2017-05-08 23:23 175人阅读 评论(0) 收藏
B. T-Shirt Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...