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, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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(线段树区间更新)的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  2. (简单) 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 ...

  3. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  4. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  5. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  6. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  7. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. Java中弹出对话框中的几种方式

    1.显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", " ...

  2. 嵌入式的SQL程序设计

    嵌入式的SQL程序设计 sql语句大全之嵌入式SQL 2017-01-18 16:00 来源:未知   嵌入式SQL 为了更好的理解嵌入式SQL,本节利用一个具体例子来说明.嵌入式SQL允许程序连接数 ...

  3. BZOJ4407 于神之怒加强版 - 莫比乌斯反演

    题解 非常裸的莫比乌斯反演. 但是反演完还需要快速计算一个积性函数(我直接用$nlogn$卷积被TLE了 推荐一个博客 我也不想再写一遍了 代码 #include<cstring> #in ...

  4. Vue.js 2.0 跨域请求数据

    Vuejs由1.0更新到了2.0版本.HTTP请求官方也从推荐使用Vue-Resoure变为了 axios .接下来我们来简单地用axios进行一下异步请求.(阅读本文作者默认读者具有使用npm命令的 ...

  5. [远程] windows 2008 server设置了共享文件夹,并且共享给了everyone,但是还是无法访问,怎么解决呢?

    还需要设置另外一个地方,将用户加到MSAppAccess这个组里去

  6. linux下 C程序 参数和内存

    #include <stdio.h> int main(argc, argv) int argc;char *argv[]; {     printf("argc=%d \n&q ...

  7. 替换SQL执行计划

    Switching two different SQL Plan with SQL Profile in Oracle... 当SQL是业务系统动态生成的,或者是第三方系统产生的,在数据库层面分析发现 ...

  8. Devexpress VCL Build v2014 vol 14.2.1 beta发布

    已经快到2015 年了. 14.2.1 beta 才出来了. 还好,有一些新东西. 官网地址 VCL Gauge Control Designed to clearly convey informat ...

  9. 【C#】VS2015开发环境的安装和配置(转)

    出处: http://www.cnblogs.com/rainmj/p/5636518.html http://www.cnblogs.com/rainmj/p/5636529.html http:/ ...

  10. 程序员面试50题(1)—查找最小的k个元素[算法]

    题目:输入n个整数,输出其中最小的k个.例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4. 分析:这道题最简单的思路莫过于把输入的n个整数排序,这样排在最前面的k个数 ...