poj 3468A Simple Problem with Integers
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
Hint
#include<stdio.h>
#include<string.h>
#define maxn 100005
#define ll long long
char str[10];
ll a[maxn];
struct node
{
ll l,r,sum,ans;
}b[4*maxn];
void build(ll l,ll r,ll i)
{
ll mid;
b[i].l=l;
b[i].r=r;
b[i].ans=0;
if(b[i].l==b[i].r)
{
b[i].sum=a[l];
return;
}
mid=(l+r)/2;
build(l,mid,2*i);
build(mid+1,r,i*2+1);
b[i].sum=b[i*2].sum+b[i*2+1].sum;
}
void pushdown(int i)
{
if(b[i].ans){
b[i*2].ans+=b[i].ans;
b[i*2+1].ans+=b[i].ans;
b[i*2].sum+=b[i].ans*(b[i*2].r-b[i*2].l+1);
b[i*2+1].sum+=b[i].ans*(b[i*2+1].r-b[i*2+1].l+1);
b[i].ans=0;
}
}
void add(ll l,ll r,ll value,ll i)
{
ll mid;
if(b[i].l==l && b[i].r==r)
{
b[i].ans=b[i].ans+value;
b[i].sum+=(b[i].r-b[i].l+1)*value;
return;
}
pushdown(i);
b[i].sum+=(r-l+1)*value; //这一句写了,下面第二句就不用写了,是同一个意思
mid=(b[i].l+b[i].r)/2;
if(l>mid)
add(l,r,value,i*2+1);
else if(r<=mid)
add(l,r,value,i*2);
else
{
add(l,mid,value,i*2);
add(mid+1,r,value,i*2+1);
}
//b[i].sum=b[i*2].sum+b[i*2+1].sum; ---2
}
ll question(ll l,ll r,ll i)
{
ll mid;
if(b[i].l==l && b[i].r==r)
{
return b[i].sum;
}
pushdown(i);
mid=(b[i].l+b[i].r)/2;
if(l>mid)
return question(l,r,i*2+1);
else if(r<=mid)
return question(l,r,i*2);
else if(l<=mid && r>mid)
return question(l,mid,i*2)+question(mid+1,r,i*2+1);
}
int main()
{
ll n,m,c,d,e,i,j;
while(scanf("%lld%lld",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
while(m--)
{
scanf("%s",str);
if(str[0]=='Q')
{
scanf("%lld%lld",&c,&d);
printf("%lld\n",question(c,d,1));
}
else if(str[0]=='C')
{
scanf("%lld%lld%lld",&c,&d,&e);
add(c,d,e,1);
}
}
}
return 0;
}
poj 3468A Simple Problem with Integers的更多相关文章
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- POJ - 3468A Simple Problem with Integers (线段树区间更新,区间查询和)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
- 3468-A Simple Problem with Integers 线段树(区间增减,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 110077 ...
- POJ A Simple Problem with Integers | 线段树基础练习
#include<cstdio> #include<algorithm> #include<cstring> typedef long long ll; #defi ...
- POJ 3468_A Simple Problem with Integers(树状数组)
完全不知道该怎么用,看书稍微懂了点. 题意: 给定序列及操作,求区间和. 分析: 树状数组可以高效的求出连续一段元素之和或更新单个元素的值.但是无法高效的给某一个区间的所有元素同时加个值. 不能直接用 ...
- POJ 3468_A Simple Problem with Integers(线段树)
题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
随机推荐
- redis存json数据时选择string还是hash
redis存json数据时选择string还是hash 我们在缓存json数据到redis时经常会面临是选择string类型还是选择hash类型去存储.接下来我从占用空间和IO两方面来分析这两种类型的 ...
- 原生javascript制作省市区三级联动详细教程
多级联动下拉菜单是前端常见的效果,省市区三级联动又属于其中最典型的案例.多级联动一般都是与数据相关联的,根据数据来生成和修改联动的下拉菜单.完成一个多级联动效果,有助于增强对数据处理的能力. 本实例以 ...
- 【Linux】dd命令进行磁盘备份
运用dd命令,将/dev/sdb磁盘中所有的数据全部备份到/dev/sdc磁盘上,需要的命令如下 dd if=/dev/sdb of=/dev/sdc bs=1024k 说明,if是需要备份的磁盘 ...
- Electron实用技巧-开机启动时隐藏主窗口,只显示系统托盘
# 1 在桌面软件中,开机自启动是很常见的功能,在electron中也提供了很好的支持,以下是主要代码: //应用是否打包if (app.isPackaged) { //设置开机启动 app.se ...
- ALV中layout布局控制详解
参数的结构为SLIS_LAYOUT_ALV.结构中比较常用的字段如下: no_colhead 隐藏列标题 值为X或空 no_hotspot headings不作为热 ...
- 08--Docker安装Mysql
1.在hub.docker.com中查找5.7版本 2.拉取mysql docker pull mysql:5.7 3.启动mysql镜像 docker run -p 3306:3306 --name ...
- Zabbix监控虚拟机服务-告警与自动恢复
今天稍微空闲,使用下zabbix的5.0版本,目前生产环境是4.x版本 今天就只实现一个目的:监控任意一个服务(示例中监控的是docker.service),如果服务挂了,自动给恢复,先看一个动图 搭 ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- PWN_ret2text,ret2syscall,ret2shellcode
首先了解下Linux中的保护机制(具体的绕过等后续再说) 1.canary(栈保护) 在函数开始时就随机产生一个值,将这个值CANARY放到栈上紧挨ebp的上一个位置,当攻击者想通过缓冲区溢出覆盖eb ...
- 如何配置 Slf4j
一,前言 日常开发中经常需要在控制台输出一些信息,如果这些东西不加管理,那么很容易就被输出信息淹没.幸好,我们有日志相关的库来帮助我们格式化控制台的输出. 这篇文章将介绍如何配置 Slf4j 及其具体 ...