poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 63565 | Accepted: 19546 | |
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
Hint
给出n个数q次操作
C代表把a到b间的数分别加c
Q要求输出和
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=111111;
long long sum[MAXN<<2];
long long lazy[MAXN<<2];
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(m-(m>>1));
sum[rt<<1|1]+=lazy[rt]*(m>>1);
lazy[rt]=0;
}
}
void build(int l,int r,int rt)
{
lazy[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(l>=L&R>=r)
{
lazy[rt]+=c;
sum[rt]+=(long long)c*(r-l+1);
return ;
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)
update(L,R,c,l,mid,rt<<1);
if(R>mid)
update(L,R,c,mid+1,r,rt<<1|1);
pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(l>=L&&R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
long long cnt=0;
if(L<=mid)
cnt=query(L,R,l,mid,rt<<1);
if(R>mid)
cnt+=query(L,R,mid+1,r,rt<<1|1);
return cnt;
}
int main()
{
int n,q;
char op[2];
int a,b,c;
while(scanf("%d %d",&n,&q)!=EOF)
{
build(1,n,1);
//printf("%d\n",sum[1]);
while(q--)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d %d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
else
{
scanf("%d %d %d",&a,&b,&c);
update(a,b,c,1,n,1);
}
}
}
return 0;
}
poj 3468 A 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 ...
- [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(线段树,区间更新,区间求和)
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<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- 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 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向”
Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向” 原因还未知..
- ApplicationCommands 应用程序常见命令
ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c 在WPF中,许多控件都自动集成了固有的命令集.比如文本框TextBox就提供了复制(Copy),粘贴 ...
- pygame --- 可怜的小乌龟
来于----@小甲鱼工作室 import pygame import sys from pygame.locals import * #初始化 pygame.init() size = width,h ...
- Tomcat的类加载机制
一个功能健全的Web服务器,要解决如下几个问题: 部署在同一个服务器上的两个Web应用程序使用的Java 类库可以实现相互隔离.不能要求一个类库在一个服务器中只有一份,服务器应当保证两个应用程序的类 ...
- 平滑部署war包到tomcat-deploy.sh
#!/bin/sh #check war exists echo "check war exists" war_file_path=/data/tomcat8/webapps wa ...
- (1) Flutter android studio安装
Flutter由两部分组成 Flutter引擎和Flutter框架 Flutter引擎由C++编写,在android上通过NDK编译,在ios上通过llvm编译 Flutter框架由dart编写 1. ...
- 求给出第 K个 N位二进制数,该二进制数不得有相邻的“1”
求给出第 K (0 < K < 109) 个 N (0 < N < 44) 位二进制数,该二进制数不得有相邻的"1". 这道题要求给出第 K (0 < ...
- MySQL数据库增删改字段(属性)
MySQL数据库的各种操作今天在这里总结一下: 一.增加 1.在已有的表中添加新的字段: 首先是增加表的字段,比如一张表原本没有字段“ Time ”,现在我们要增加这样一个字段,可以用下面的SQL语句 ...
- UVA 839 Not so Mobile (递归建立二叉树)
题目连接:http://acm.hust.edu.cn/vjudge/problem/19486 给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡. 分析:递归 ...
- 日期 function
SELECT SYSDATE, ADD_MONTHS(SYSDATE,), ADD_MONTHS(SYSDATE,), LAST_DAY(SYSDATE), MONTHS_BETWEEN(SYSDAT ...