博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【poj3468】A Simple Problem with Integers
阅读量:4663 次
发布时间:2019-06-09

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

题面

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.

题解

线段树模板

#include
#include
using namespace std;const int maxn = 100010;typedef long long LL;LL a[maxn];struct node{ int l, r; LL val, addmark;}sgt[maxn<<2];void build(int p, int l, int r){ sgt[p].l = l, sgt[p].r = r; if(l == r){ sgt[p].val = a[l]; }else{ int m = (l+r)/2; build(p*2,l,m); build(p*2+1,m+1,r); sgt[p].val = sgt[p*2].val+sgt[p*2+1].val; }}void pushdown(int p){ if(sgt[p].addmark != 0){ LL t = sgt[p].addmark; sgt[p].addmark = 0; sgt[p*2].addmark += t; sgt[p*2+1].addmark += t; sgt[p*2].val += (sgt[p*2].r-sgt[p*2].l+1)*t; sgt[p*2+1].val += (sgt[p*2+1].r-sgt[p*2+1].l+1)*t; }}void add(int p, int l, int r, LL v){ if(l <= sgt[p].l && sgt[p].r <= r){ sgt[p].val += (sgt[p].r-sgt[p].l+1)*v; sgt[p].addmark += v; return ; } pushdown(p); int m = (sgt[p].l+sgt[p].r)/2; if(l <= m)add(p*2,l,r,v); if(r > m)add(p*2+1,l,r,v); sgt[p].val = sgt[p*2].val+sgt[p*2+1].val;}LL query(int p, int l, int r){ if(l <= sgt[p].l && sgt[p].r <= r)return sgt[p].val; pushdown(p); //pushdown LL m = (sgt[p].l+sgt[p].r)/2, ans = 0; if(l <= m)ans += query(p*2,l,r); if(r > m)ans += query(p*2+1,l,r); return ans;}int main(){ ios::sync_with_stdio(false); int n, m; cin>>n>>m; for(int i = 1; i <= n; i++)cin>>a[i]; build(1,1,n); for(int i = 1; i <= m; i++){ char ch; cin>>ch; if(ch == 'Q'){ LL x, y; cin>>x>>y; cout<
<<"\n"; }else{ LL x, y, z; cin>>x>>y>>z; add(1,x,y,z); } } return 0;}

关于线段树2*n空间表示法。

推荐博客

转载于:https://www.cnblogs.com/gwj1314/p/9444871.html

你可能感兴趣的文章
有关js中能否使用equals来判断相等的问题
查看>>
(十八)多线程
查看>>
bzoj4580: [Usaco2016 Open]248
查看>>
HTML5 VS. Flash&Flex? – 浅谈Flash/Flex/HTML 5技术选型
查看>>
响应者链条
查看>>
基于定位的社交应用Foursquare开源网址(wp7)
查看>>
机电传动控制读书笔记二(书本内容)
查看>>
Address already in use: JVM_Bind<null>:8080错误的解决办法
查看>>
Vue子组件监听事件中传递参数的方法
查看>>
面向对象的几种方法详解(后)
查看>>
年龄问题
查看>>
winform自动更新并实现文件的批量异步下载
查看>>
UVA 301 Transportation
查看>>
MYSQL的常用命令和增删改查语句和数据类型!
查看>>
再回首数据结构—红黑树(一)
查看>>
界面设计规范(转)
查看>>
js与jquery混用问题
查看>>
可空类型 Nullable<T>
查看>>
python之封装
查看>>
iOS开发常用快捷键
查看>>