之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网址细细品味):

依照他的思路附上我的代码:

 #include<cstdio>
#include<cstring>
#define lowbit(x) ((x)&-(x))
typedef long long LL;
const int maxn= ;
LL org[maxn+]; struct tree{
LL c[maxn+];
void clear() { memset(c,,sizeof(c)); }
LL sum(int x) const {
LL res= ;
while(x){
res+= c[x];
x-= lowbit(x);
}
return res;
}
void add(int x, LL d){
while(x<=maxn){
c[x]+= d;
x+= lowbit(x);
}
}
} d1,d2; inline LL sum(int x) {
return org[x]+(x+)*d1.sum(x)-d2.sum(x);
} int main(){
int n,q,a,b;
LL x,c;
while(~scanf("%d%d",&n,&q)){
memset(org,,sizeof(org));
d1.clear();
d2.clear();
for(int i=; i<=n; ++i){
scanf("%lld",&x);
org[i]= org[i-]+x;
}
while(q--){
getchar();
if(getchar()=='Q'){
scanf("%d%d",&a,&b);
printf("%lld\n",sum(b)-sum(a-));
}
else {
scanf("%d%d%lld",&a,&b,&c);
d1.add(a,c);
d1.add(b+,-c);
d2.add(a,c*a);
d2.add(b+,-c*(b+));
}
}
}
return ;
}

  提交后发现比线段树要快一点,再加上代码的精简性,树状数组,果然够强大!

POJ 3468(树状数组的威力)的更多相关文章

  1. POJ 3321 树状数组(+dfs+重新建树)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27092   Accepted: 8033 Descr ...

  2. POJ 2352Stars 树状数组

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42898   Accepted: 18664 Descripti ...

  3. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

  4. poj 3928 树状数组

    题目中只n个人,每个人有一个ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判的ID和技能值都在两个选手之间的时候才能进行一场比赛,现在问一共能组织多少场比赛. 由于排完序之后,先插入的一定 ...

  5. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  6. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  7. poj 2182 树状数组

    这题对于O(n^2)的算法有很多,我这随便贴一个烂的,跑了375ms. #include<iostream> #include<algorithm> using namespa ...

  8. POJ 2352 树状数组

    学习自:链接以及百度百科 以及:https://www.bilibili.com/video/av18735440?from=search&seid=363548948825132979 理解 ...

  9. POJ 2299树状数组求逆序对

    求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...

随机推荐

  1. 7.进度条(ProgressBar)

    默认为圆形,类似加载的样子,如果想要设置为下载的样式,可以选择它的样式为横向. style="?android:attr/progressBarStyleHorizontal" 顺 ...

  2. 【转】西门子数控系统中MMC、PCU、NCU、CCU简略介绍

    转载地址:http://cyj221.blog.163.com/blog/static/34194117201093005526170/ 2010-10-30 01:06:09|  分类: 机械制造 ...

  3. c#之线程池优先级

    using System; using System.Threading; namespace ConsoleApplication1 { class Program { static void Ma ...

  4. c#之xml

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  5. Stars(树状数组或线段树)

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37323 Accepted: 16278 Description A ...

  6. Github for Windows安装

    下载软件:https://desktop.github.com/ 安装之前要求系统先要有安装.net framework 4.5,不然软件安装的时候会自动上网下载安装,这软件下载起来非常慢. 第一次操 ...

  7. Java中通过JDBC远程连接Oracle数据库

    通过jdbc连接数据库,拢共分三步: 第一步:下载一个JDBC的驱动,然后把jar包扔到项目里并add to build path: 第二步:去本地oracle文件夹下找到“TNSNAMES.ORA” ...

  8. python学习笔记一 python入门(基础篇)

    简单介绍一下python2.x和3.5的区别   print   在python3.5中print 变为print() Old: print * New: print( * ) 如果想要不换行,之前的 ...

  9. 5-JS函数

    函数 定义函数 JS中有3种定义函数的方法: 函数声明 用函数声明定义函数的方式如下: function abs(x) { if (x >= 0) { return x; } else { re ...

  10. [UVa1213]Sum of Different Primes(递推,01背包)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...