POJ 3468 A Simple Problem with Integers
来源:互联网 发布:怎么寻找心理医生网络 编辑:程序博客网 时间:2024/05/16 08:25
题目链接:http://poj.org/problem?id=3468
题意:有n个数,将一个区间上的数都加上一个值,或者查询一个区间的和。
思路:线段树或者树状数组模板。
树状数组专题链接:http://blog.csdn.net/chy20142109/article/details/50673749
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>using namespace std;#define LL long long#define rep(i,j,k) for(int i = j; i <= k; i++ )#define Rrep(i,j,k) for(int i = j; i >= k; i-- )#define Clean(x,y) memset(x,y,sizeof(x))const int maxn = 100009;int n,q;LL sum[maxn];LL c[maxn];int lowbit(int x){ return x&(-x);}LL query(int x,LL a[]){ LL ans = 0; while(x) { ans+=a[x]; x-=lowbit(x); } return ans;}void add(int x,int k,LL a[]){ while(x<=n) { a[x]+=k; x+=lowbit(x); }}int main(){ Clean(sum,0); Clean(c,0); cin>>n>>q; int temp; rep(i,1,n) { scanf("%d",&temp); add(i,temp,sum); } char op; int tx,ty,tz; while(q--) { getchar(); op = getchar(); scanf("%d%d",&tx,&ty); if ( op == 'C' ) { scanf("%d",&tz); add(tx,tz,c); add(ty+1,-tz,c); add(tx, (1-tx)*tz,sum); add(ty+1,ty*tz,sum); } else printf("%I64d\n", query(ty,sum) + ty*query(ty,c) - query(tx-1,sum) - (tx-1)*query(tx-1,c) ); } return 0;}
0 0
- POJ 3468 A Simple Problem With Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ-3468-A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 - A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- STM32软件仿真问题
- error:Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and op
- HIVE 0.14 Cost Based Optimizer (CBO) Technical Overview
- android的listView如何添加一个ViewPager
- iOS开发-文件管理
- POJ 3468 A Simple Problem with Integers
- Linux学习笔记--mv命令(剪切或改名)
- 前端程序员经常忽视的一个JavaScript面试题
- NFS安装配置篇
- Xcode the identifier is not valid
- CNTK从入门到深入研究(2) - 研究CNTK配置文件
- Objective-C基础语法(一)
- 百度地图黑屏问题
- Nginx安装配置篇(Session共享)