博客
关于我
POJ 3468 A Simple Problem with Integers(线段树+区间更新)
阅读量:319 次
发布时间:2019-03-04

本文共 2602 字,大约阅读时间需要 8 分钟。

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
The sums may exceed the range of 32-bit integers.

终于有点明白pushdown函数是干嘛的了,它更新的方向和pushup相反。
pushup是在回溯的过程中更新此节点的父节点
pushdown是在父结点有更新的时候,先向下更新直接子结点,然后再有其他的操作。
pushdown函数的判断:如果不为0说明这个节点的字节点需要更新,更新完成后,把它标记为已经更新,不需要更新

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long ll;int n,q;struct node{   	int l,r;	ll val,lazy; 	int len; }tree[100005*4];ll arr[100005];void pushup(int cur){   	tree[cur].val=tree[cur*2].val+tree[cur*2+1].val;}void pushdown(int cur){   	if (tree[cur].lazy!=0){   		tree[cur*2].lazy+=tree[cur].lazy;		tree[cur*2+1].lazy+=tree[cur].lazy;		tree[cur*2].val+=tree[cur*2].len*tree[cur].lazy;		tree[cur*2+1].val+=tree[cur*2+1].len*tree[cur].lazy;		tree[cur].lazy=0;	}}void build(int cur, int l, int r){   	int mid=(l+r)/2;	tree[cur].l=l, tree[cur].r=r;	tree[cur].val=0, tree[cur].len=r-l+1;	tree[cur].lazy=0;	if (l==r)		tree[cur].val=arr[l];	else{   		build(cur*2, l, mid);		build(cur*2+1, mid+1, r);		pushup(cur);	}}void update(int cur, int l, int r, int val){   	if (l<=tree[cur].l && tree[cur].r<=r){   		tree[cur].val+=(tree[cur].r-tree[cur].l+1)*val;		tree[cur].lazy+=val;		return;	}	pushdown(cur);		int mid=(tree[cur].l+tree[cur].r)/2;	if (l<=mid)		update(cur*2, l, r, val);	if (r>mid)		update(cur*2+1, l, r, val);		pushup(cur);} ll query(int cur, int ql, int qr){   	if (ql<=tree[cur].l && tree[cur].r<=qr)		return tree[cur].val;	pushdown(cur);		ll ans=0;	int mid=(tree[cur].l+tree[cur].r)/2;	if (ql<=mid)		ans+=query(cur*2, ql, qr);	if (qr>mid)		ans+=query(cur*2+1, ql, qr);	return ans; } int main(){   	scanf("%d%d", &n, &q);	for(int i=1; i<=n; i++)		scanf("%lld", &arr[i]);	build(1, 1, n); 	while(q--){   		char op[5];		int a,b;		scanf("%s%d%d", op, &a, &b);		if(op[0]=='C'){   			ll c;			scanf("%lld", &c);			update(1, a, b, c);		}		else printf("%lld\n",query(1, a, b));	}	return 0;}

转载地址:http://kvnh.baihongyu.com/

你可能感兴趣的文章
SaaS前世今生:老树开新花
查看>>
微信小程序生命周期 / 页面的生命周期 / 页面的用户行为
查看>>
Maven的配置
查看>>
如何在bilibili上下载学习视频?
查看>>
09-Vue之本地应用v-for指令
查看>>
纪中2020.3.18普及C组模拟赛总结
查看>>
YbtOJ 递推算法课堂过关 例5 平铺方案【递推(简单DP)】
查看>>
YbtOJ hash和hash表课堂过关 例1 字符串哈希【hash】
查看>>
CSUST 2021 周赛 2 题解
查看>>
前后端数据交互之表单
查看>>
剑指offer JZ15 反转链表
查看>>
剑指offer JZ21 栈的压入弹出序列
查看>>
剑指offer JZ31 整数中1出现的次数
查看>>
实现基于scrapy框架的天气预报爬虫hengYangSpaider @572311文
查看>>
maven打包指定名称并去除jar-with-dependencies后缀
查看>>
Netty4服务端入门代码示例
查看>>
操作系统前传第六课--开发中的辅助工具
查看>>
Linux系统编程44 信号 - 信号的响应过程分析!!!
查看>>
VL53L0x TOF激光测距的 stm32 HAL库驱动代码
查看>>
怎么玩LOG4J
查看>>