A Simple Problem with Integers
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

Hint

The sums may exceed the range of 32-bit integers.
 
 
算法:线段树+延迟标记
 
 
#include <iostream>
#include <cstdio> using namespace std; const int maxn = 1e5+; typedef long long ll; struct node {
int l, r;
ll sum;
}tree[maxn << ]; int n, m;
ll arr[maxn];
ll lazy[maxn << ]; //懒惰数组 void pushup(int root) {
tree[root].sum = tree[root << ].sum + tree[root << | ].sum;
} void build(int root, int l ,int r) {
tree[root].l = l;
tree[root].r = r;
lazy[root] = ;
if(l == r) {
tree[root].sum = arr[l];
return;
}
int mid = (l + r) >> ;
build(root << , l, mid);
build(root << | , mid + , r);
pushup(root);
} void spread(int root) {
if(lazy[root]) { //判断延迟标记是否存在
lazy[root << ] += lazy[root]; //左子树下传延迟标记
lazy[root << | ] += lazy[root]; //右子树下传延迟标记
tree[root << ].sum += lazy[root] * (tree[root << ].r - tree[root << ].l + ); //更新左结点信息
tree[root << | ].sum += lazy[root] * (tree[root << | ].r - tree[root << | ].l + ); //更新右结点信息
lazy[root] = ; //清除延迟标记
}
} void update(int root, int x, int y, ll val) {
int l = tree[root].l;
int r = tree[root].r;
if(x <= l && r <= y) {
tree[root].sum += (r - l + ) * val;
lazy[root] += val; //添加延迟标记
return;
}
spread(root); //下传延迟标记
int mid = (l + r) >> ;
if(x <= mid) {
update(root << , x, y, val);
}
if(y > mid) {
update(root << | , x, y, val);
}
pushup(root);
} ll query(int root, int x, int y) {
int l = tree[root].l;
int r = tree[root].r;
if(x <= l && r <= y) { //完全覆盖
return tree[root].sum;
}
spread(root); //下传延迟标记
ll res = ;
int mid = (l + r) >> ;
if(x <= mid) {
res += query(root << , x, y);
}
if(y > mid) {
res += query(root << | , x, y);
}
return res;
} int main() {
while(~scanf("%d %d", &n, &m)) {
for(int i = ; i <= n; i++) {
scanf("%lld", &arr[i]);
}
build(, , n);
while(m--) {
char op[];
int l, r;
scanf("%s", op);
if(op[] == 'Q') {
scanf("%d %d", &l, &r);
printf("%lld\n", query(, l, r));
} else {
ll val;
scanf("%d %d %lld", &l, &r, &val);
update(, l, r, val);
}
}
}
return ;
}

HDU 3468:A Simple Problem with Integers(线段树+延迟标记)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  4. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

  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 ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

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

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

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

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

  9. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  10. poj 3468 A Simple Problem with Integers 线段树加延迟标记

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

随机推荐

  1. NOIP2017普及组比赛总结

    期中考总结&NOIP2017总结 2017年11月11日,我第二次参加NOIP普及组复赛.上一年,我的得分是250分,只拿到了二等奖.我便把目标定为拿到一等奖,考到300分以上. 早上8点多, ...

  2. Linux安全审计

    Client: OMAudit_agent.py #!/usr/bin/env python #coding:utf- import sys import socket import fcntl im ...

  3. 关于echarts中南海诸岛的显示问题

    1.china.js 文件中  echarts.registerMap('china',    //名字要是'china',不能写成'中国'   2.echarts 配置中地图名称 mapName: ...

  4. C# 面向对象1(类和对象的概念)

    1.面向过程-->面向对象 面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作. 2.面向过程的思想: 以上的,需求一更改,会导致不同的方法,一一去更改. 3.面向对象的思想:找个 ...

  5. c++ 递归算法实现排列组合

    通过引用的方式来传值,具体的实现的方法如下 void pc(int m,int n,int &position,int (&a)[100]) { //如果运算得到那个数 if (pos ...

  6. Powershell - 获取服务器启动时间

    www.orangeisland.cn 使用以下命令,直接获取服务器启动时间: 通过以下脚本,批量获取服务器的启动时间: $TextPath = "C:\Users\admin\Deskto ...

  7. 08Response

    1.功能:设置响应消息 1. 设置响应行 1. 格式:HTTP/1.1 200 ok 2. 设置状态码:setStatus(int sc) 2. 设置响应头:setHeader(String name ...

  8. mysql导入redis

    将mysql中数据库指定表导入redis 如何将mysql中某个数据库中的表数据快速导入redis? 以下将演示将本地127.0.0.1中数据库test中的表t_abc导入本地redis中.步骤如下: ...

  9. 牛客第八场 C-counting paths 树形dp计数

    题目地址 题意 给你一颗树 初始点颜色全部为白色 对于每一个满足要求一的点集s f(s)的定义为先把点集内的点染黑 满足要求二的路径集合数量 要求一为两两黑点之间不能出现白色的点 要求二为将这个路径集 ...

  10. 安装驱动模块ko

    1. make install 2. 3.手动加载驱动程序 [root@localhost template]# modprobe usbnet [root@localhost template]# ...