【poj3468】A Simple Problem with Integers
题面
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<iostream>
#include<algorithm>
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); //pushdownLL 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<<query(1,x,y)<<"\n";}else{LL x, y, z; cin>>x>>y>>z;add(1,x,y,z);}}return 0;
}
关于线段树2*n空间表示法。
推荐博客http://www.cppblog.com/MatoNo1/archive/2015/05/05/195857.html
【poj3468】A Simple Problem with Integers相关推荐
- 【POJ - 3468 】 A Simple Problem with Integers (线段树模板 区间更新 + 区间和查询)(不能树状数组或差分数组)
题干: You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type o ...
- 【线段树】【模板】讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值)
[线段树][模板]讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值) ...
- 【BZOJ3489】A simple rmq problem kd-tree
[BZOJ3489]A simple rmq problem Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过 ...
- 论文阅读 【CVPR-2022】 A Simple Multi-Modality Transfer Learning Baseline for Sign Language Translation
论文阅读 [CVPR-2022] A Simple Multi-Modality Transfer Learning Baseline for Sign Language Translation st ...
- 洛谷 P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
题目链接:P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 题意 给定两个 \(n\) 位 \(10\) 进制整数 \(x\) 和 \(y\),求 \(x*y\). 思路 FFT ...
- 洛谷 P1919 【模板】A*B Problem升级版 【快速傅里叶变换 FFT】
[洛谷 P1919] [模板]A*B Problem升级版 题目大意 给你两个超大整数 a,ba,ba,b,问 a∗ba*ba∗b 其中 a,b≤101000000a,b\leq10^{1000000 ...
- [P1919 【模板】A*B Problem升级版(FFT快速傅里叶)(高精乘板子,FFT板子)
P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 分析: 为什么高精乘可以用 FFT 优化??? 众所周知 FFT 是解决多项式之间的乘法运算,将乘数的每一位看成多项式的系 ...
- 【Git】SSL certificate problem: unable to get local issuer certificate错误的解决办法
问题描述 unable to access 'https://*****.git/': SSL certificate problem: unable to get local issuer cert ...
- poj 3243:A Simple Problem with Integers
3243:A Simple Problem with Integers 查看 提交 统计 提示 提问 总时间限制: 5000ms 单个测试点时间限制: 2000ms 内存限制: 131072 ...
最新文章
- Python中的过滤器
- Redis的7000字笔记总结,超详细!
- ABP-JavaScript API
- 基于区块链的健康链系统设计与实现(5)区块链性能优化
- 请简要说明一下CyclicBarrier和CountDownLatch的区别?
- hashmap另一种初始化
- SpringBoot启动流程解析
- 新旧 iPhone 比速度,iOS 12 公开测试版发布!
- ug链轮设计软件_同为三维设计软件,solidworks与ug有何不同
- python 学习第十二课
- Linux网络编程:使用select函数实现socket 收发数据
- 转载:SQL入门教程
- 销售数据可视化图表excel模板_两份办公学习资源,可视化EXCEL模板和Excel函数课程...
- MATLAB全局变量
- NiFi 一键自动升级Nar包
- Python第八课:函数(def)
- 简单的收支记账软件的实现详细解释(不连接数据库)
- 悉尼大学SOFT2201课业解析
- java技术--SpringContextUtil类的作用
- [emerg]: unknown directive “”
热门文章
- 异或算法在算法求解中的妙用
- ceil() 与 floor() 与 round()
- python编程案例教程-quot;怎样编写一个教学案例quot;python编程案例教程电子教案...
- python刚出来多少薪资-Python最低薪资在北上深是多少你们知道吗?我已经整理好了哦...
- 专科python应届生工资多少-Python这么火热,本科应届生薪资这么高?
- python刚出来多少薪资-作为Python程序员,薪资一般是多少?
- python与java前景-Python与Java相比,其优势在哪?
- python自学网站-自学python有什么网站
- 自学python需要安装什么-学习python需要什么基础吗?老男孩Python
- python必背入门代码-python学习必备知识汇总