POJ3468 A Simple Problem with Integers (树状数组 | 线段树)
来源:互联网 发布:q动态头像软件 编辑:程序博客网 时间:2024/04/29 15:25
题目链接:http://poj.org/problem?id=3468
方法一:树状数组 区间修改区间查询问题。
讲一下关于树状数组区间修改和区间查询的问题的解决方法。
设a[i]为原数组,d[i] = a[i] - a[i-1],其中a[0] = 0;
递推一下: d[2] = a[2] - a[1]
d[3] = a[3] - a[2]
d[4] = a[4] - a[3]
.......
.......
......
.......
d[x] = a[x] - a[x-1]
通过上面递推式可以发现,a[x] = d[x]+a[x-1],其中a[x-1] = d[x-1] + a[x-2],,所以不难发现 a[x] = (d[1]+d[2]+d[3}+......+d[x]),即a[x] = segma(d[i]),其中1<=i<=x;
所以 segma(a[i]) (1<=i<=x) = segma(segma(d[j], 1 <= j <= i)); 观察式子可知,其中一共有x个d[1]相加,x-1个d[2]相加等等,由此可以推出规律:
segma(a[i] 1 <= i <= x) = segma((x-i+1) * d[i]); 即: segma(a[i] 1 <= i <= x) = (x+1) * segma(d[i] 1<= i <= x) - segma(d[i] * i 1 <= i <= x);
其中d[i]数组存储的是每次的增量, 再设一个变量sum[i],表示前i个原数组a[]中的和,ans[i]表示前i项的总和。
则d[i]对ans[x]的贡献值就是 d[i] (x - i + 1).
AC代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define inf 0x3f3f3f3f#define mod 10000000007 #define ll long long#define ld long double#define ls rt << 1#define rs rt << 1 | 1#define pi acos(-1.0)using namespace std;const int maxn = 1e5 + 5;ll c1[maxn],c2[maxn],a[maxn],sum[maxn];int n,Q;int lowbit(int k){return k & -k;}void update(ll *array, int k, int num){while(k <= n){array[k] += num;k += lowbit(k);}}ll query(ll *array, int k){ll ans = 0;while(k){ans += array[k];k -= lowbit(k);}return ans;}int main(){while(~scanf("%d%d",&n,&Q)){memset(c2,0,sizeof(c2));memset(c1,0,sizeof(c1)); memset(sum,0,sizeof(sum));for(int i = 1; i <= n; i ++){scanf("%lld",&a[i]);}for(int i = 1; i <= n; i ++){sum[i] = sum[i-1] + a[i];} char str[10];while(Q --){scanf("%s",str);int b,c,d;if(str[0] == 'Q'){scanf("%d%d",&b,&c);ll ans = sum[c] - sum[b-1];ans += (c+1) * query(c1,c) - query(c2,c);ans -= (b-1+1) * query(c1,b-1) - query(c2,b-1); printf("%lld\n",ans);}else{scanf("%d%d%d",&b,&c,&d);update(c1,b,d);update(c2,b,b*d);update(c1,c+1,-d); update(c2,c+1,(c+1) * (-d));}}}return 0;}
线段树代码:区间更新求值就行,注意下推标记更新子节点。
AC代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define inf 0x3f3f3f3f#define ll long long#define ls rt << 1#define rs rt << 1 | 1#define pi acos(-1.0)using namespace std;const int maxn = 1e5 + 5;ll sum[maxn << 2],add[maxn << 2],d[maxn];int N,Q;void PushUp(int rt){sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}void PushDown(int rt, int ln, int rn){if(add[rt]){add[ls] += add[rt];add[rs] += add[rt];sum[ls] += add[rt] * ln; sum[rs] += add[rt] * rn;add[rt] = 0;}}void Build(int l, int r, int rt){if(l == r){sum[rt] = d[l];return;}int m = (l + r) >> 1; // printf("2333\n");Build(l,m,rt << 1);Build(m+1,r,rt << 1 | 1);PushUp(rt);}void Update(int L, int R, int C, int l, int r, int rt){if(L <= l && r <= R){sum[rt] += (r - l + 1) * C;add[rt] += C;return ;}int m = (l + r) >> 1;PushDown(rt,m-l+1,r-m);if(L <= m) Update(L,R,C,l,m,ls);if(R > m) Update(L,R,C,m+1,r,rs);PushUp(rt);}ll Query(int L, int R, int l, int r, int rt){if(L <= l && r <= R) return sum[rt];int m = (l + r) >> 1;PushDown(rt,m-l+1,r-m); ll ans = 0;if(L <= m) ans += Query(L,R,l,m,ls);if(R > m) ans += Query(L,R,m+1,r,rs);return ans;}int main(){while(~scanf("%d%d",&N,&Q)){for(int i = 1; i <= N; i ++)scanf("%lld",&d[i]);memset(sum,0,sizeof(sum));memset(add,0,sizeof(add));Build(1,N,1);char str[10];while(Q --){scanf("%s",str);int a,b,c;if(str[0] == 'C'){scanf("%d%d%d",&a,&b,&c);Update(a,b,c,1,N,1);}else{scanf("%d%d",&a,&b);ll ans = Query(a,b,1,N,1);printf("%lld\n",ans);}}}return 0;}
- POJ3468 A Simple Problem with Integers (树状数组 | 线段树)
- POJ3468 A Simple Problem with Integers 线段树|树状数组BIT(区间增减,求和)
- poj3468 A Simple Problem with Integers 基础线段树or树状数组
- poj3468 A Simple Problem with Integers(用线段树和树状数组实现)
- poj3468--A Simple Problem with Integers(树状数组解法)
- [树状数组] poj3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers(树状数组->改段求点)
- A Simple Problem with Integers(线段树-树状数组)
- poj3468 A Simple Problem with Integers 线段树的扩展
- poj3468- A Simple Problem with Integers-解题报告-线段树
- poj3468 A Simple Problem with Integers(spaly&&线段树)
- poj3468 A Simple Problem with Integers(线段树)
- poj3468 A Simple Problem with Integers 线段树区间更新
- poj3468(A Simple Problem with Integers)线段树实现
- POJ3468---线段树模版--A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers 【线段树+lazy】
- [线段树] poj3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers(线段树)
- [UVa 1572] 自组合(Self-Assembly)
- 170803 逆向-RouterScan(7)
- 在spring,hibernate,struts2框架整合中出现的no session问题
- 解决ScrollView嵌套RecyclerView(横向)或ListView(横向)时,横向滑动不顺畅的问题。
- 高精加、乘
- POJ3468 A Simple Problem with Integers (树状数组 | 线段树)
- Android Studio:Preference/Settings :Plugins:Icon Creator
- 扩增子分析QIIME2. 7 实验设计和统计结果元数据Metadata
- LaTeX制作PPT实例
- java String 中 intern方法的概念
- 居中布局:水平居中 + 垂直居中 + 水平垂直居中
- Linux 内核的I2C设备
- 【杭电 oj】1863畅通工程 (最小生成数 模板)
- arm-none-linux-gnueabi-gcc下载