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<< ...
随机推荐
- PAT 1052 卖个萌 (20)(代码+思路)
1052 卖个萌 (20)(20 分) 萌萌哒表情符号通常由"手"."眼"."口"三个主要部分组成.简单起见,我们假设一个表情符号是按下列格 ...
- linux 操作笔记
1.设置防火墙,允许用户使用http访问本机 [root@localhost geoserver]# systemctl enable httpdCreated symlink from /etc/s ...
- array_column()提取二维数组中某个值
<?php $multipleCommodity = array( =>array(), =>array() ); $arr1=array_column($multipleCommo ...
- 摹客项目在2018年工信部"创客中国"名列10强并荣获二等奖
2018“创客中国”互联网+大数据创新创业大赛(暨2018创客中国产业投资峰会)8月19日在厦门进行了总决赛.大赛由国家工业和信息化部.厦门市人民政府主办,厦门文广集团等承办.工信部信息中心领导.厦门 ...
- 《JavaScript高级程序设计》笔记
1. 当在函数内部定义了其他函数时,就创建了闭包.闭包有权访问包含函数内部的所有变量. 2. 闭包可以分隔变量空间,不会占用全局空间而造成相互间的干拢.使用闭包可以在JavaScript中模仿块级作用 ...
- DevExpress VCL 13.1.2 发布
DevExpress VCL 的2013 第一个公开版发布, 基本上就是一些维护,没有大的变化,也没有FM 的支持. What's New in DevExpress VCL 13.1.2 Rel ...
- 46 What Is Real Happiness ? 什么是真正的幸福 ?
46 What Is Real Happiness ? 什么是真正的幸福 ? ①The way people hold to the belief that a fun-filled, pain-fr ...
- hbase使用MapReduce操作3(实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中)
Runner类 实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中. package com.yjsj.hbase_mr; import org.apache.hadoo ...
- hdu 4888 最大流慢板
http://acm.hdu.edu.cn/showproblem.php?pid=4888 添加一个源点与汇点,建图如下: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对 ...
- The MATLAB Profiler
function a = myFunc(a,b,c,d,e) : a = a-b + c^d*log(e); end end >> profile on; myFunc(a,b,c,d,e ...