PKU 3468
来源:互联网 发布:nginx php 张宴 编辑:程序博客网 时间:2024/05/01 17:21
题目链接:
http://poj.org/problem?id=3468
题解 :
线段树区间更新模版题。
代码:
#include <string>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define lchild rt<<1 ,l, m#define rchild rt<<1|1 ,m+1, rtypedef long long ll;const ll maxn = 1e5+10;ll tree[maxn<<2];ll lazy[maxn<<2];void push_up(ll rt){ tree[rt]=tree[rt << 1]+tree[rt<<1|1];}void push_down(ll rt, ll len){ tree[rt << 1] += lazy[rt] * (len - (len >> 1)); lazy[rt << 1] += lazy[rt]; tree[rt << 1 | 1] += lazy[rt] * (len >> 1); lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0;}void build(ll rt,ll l,ll r){ lazy[rt]=0; if(l==r) { scanf("%lld",&tree[rt]); return; } ll m=(l+r)>>1; build(lchild); build(rchild); push_up(rt);}void update(ll L, ll R, ll delta, ll rt, ll l, ll r ){ if (L <= l && r <= R) { tree[rt] += delta * (r - l + 1); lazy[rt] += delta; return; } if (lazy[rt]) push_down(rt, r - l + 1); ll m = (l + r) >> 1; if (L <= m) update(L, R, delta, lchild); if (R > m) update(L, R, delta, rchild); push_up(rt);}ll query(ll L, ll R, ll rt, ll l, ll r){ if (L <= l && r <= R) return tree[rt]; if (lazy[rt]) push_down(rt, r - l + 1); ll m = (l + r) >> 1, ret = 0; if (L <= m) ret += query(L, R, lchild); if (R > m) ret += query(L, R, rchild); return ret;}int main(){ ll n,q; while(scanf("%lld%lld",&n,&q)!=EOF) { build(1,1,n); while(q--) { char s[10]; scanf("%s",s); if(s[0]=='C') { ll a,b,c; scanf("%lld%lld%lld",&a,&b,&c); update(a,b,c,1,1,n); } if(s[0]=='Q') { ll a,b; scanf("%lld%lld",&a,&b); printf("%lld\n",query(a,b,1,1,n)); } } }}
0 0
- pku 3468
- PKU 3468
- PKU 3468
- PKU 3468 线段树
- pku 3468 A Simple Problem with Integers
- pku 3468 线段树-又是成段修改
- PKU 3468 A Simple Problem with Integers
- PKU 3468 A Simple Problem with Integers
- PKU 3468 A Simple Problem with Integers
- PKU 2777 && PKU 3468 && HDU 1698【线段树:区间修改值】
- pku 1018 && pku 1095
- pku 3345 && pku 3107
- PKU 1161、PKU 2524、 PKU 1308
- [PKU] 3468 A Simple Problem with Integers -- 线段树
- pku -- 3468 A Simple Problem with Integers(线段树)
- pku 3468 A Simple Problem with Integers(线段树)
- PKU 3468 线段树,线段树 第 3 天
- pku 1003
- 【算法和数据结构】1.8--数据结构之图(C++实现)
- 【Codeforces Gym】 100162G Lyndon Words
- Word中公式变量解释时破折号对齐方法
- Java8系列之重新认识HashMap
- 2005-《Anytime Dynamic A*: An Anytime, Replanning Algorithm》
- PKU 3468
- SpringBoot快速入门(一)
- Java网络爬虫--HTML DOM(HTML 基础)
- STM32L4超低功耗特性概述
- 函数:将网页提取的cookie转换成python用来模拟登录的cookie
- OC与JS交互 -- 原生
- 7.环境搭建-flume安装
- hibernate正向生成数据库表以及配置——hibernate.cfg.xml
- Professional C++ 01 A Crash Course in C++ 快速的C++基础知识复习