https://scut.online/p/337

这个东西是个阶梯状的。那么可以考虑存两棵树,一棵树是阶梯的,另一棵树的平的,随便一减就是需要的阶梯。

优化之后貌似速度比树状数组还惊人。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; inline int read() {
int x=0;
int f=0;
char c;
do {
c=getchar();
if(c=='-')
f=1;
} while(c<'0'||c>'9');
do {
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
} while(c>='0'&&c<='9');
return f?-x:x;
} inline void _write(int x) {
if(x>9)
_write(x/10);
putchar(x%10+'0');
} inline void write(int x) {
if(x<0) {
putchar('-');
x=-x;
}
_write(x);
putchar('\n');
} void TestCase(int ti); int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
//freopen("Yinku.out","w",stdout);
#endif // Yinku
int T=1;
for(int ti=1; ti<=T; ti++)
TestCase(ti);
} /*--- ---*/ const int MAXM=100000;
int a[MAXM+5];
int st[(MAXM<<2)+5];
int st2[(MAXM<<2)+5]; const int mod=1000000007; inline int add(const int &a,const int &b){
int res=a+b;
return res>=mod?res-mod:res;
} inline int sub(const int &a,const int &b){
int res=a-b;
return res<0?res+mod:res;
} inline int mul(const int &a,const int &b){
ll res=1ll*a*b;
return res>=mod?res%mod:res;
} inline void push_up(int o) {
st[o]=add(st[o<<1],st[o<<1|1]);
st2[o]=add(st2[o<<1],st2[o<<1|1]);
} void build(int o,int l,int r) {
if(l==r){
st[o]=a[l];
st2[o]=mul(l,a[l]);
}
else {
int m=(l+r)>>1;
build(o<<1,l,m);
build(o<<1|1,m+1,r);
push_up(o);
}
} void update(int o,int l,int r,int x,int v) {
if(l==r) {
st[o]=v;
st2[o]=mul(x,v);
return;
} else {
int m=(l+r)>>1;
if(x<=m)
update(o<<1,l,m,x,v);
else if(x>=m+1)
update(o<<1|1,m+1,r,x,v);
push_up(o);
}
} int query1(int o,int l,int r,int a,int b) {
if(a<=l&&r<=b) {
return st[o];
} else {
int m=(l+r)>>1;
int ans=0;
if(a<=m)
ans=query1(o<<1,l,m,a,b);
if(b>=m+1)
ans=add(ans,query1(o<<1|1,m+1,r,a,b));
return ans;
}
} int query2(int o,int l,int r,int a,int b) {
if(a<=l&&r<=b) {
return st2[o];
} else {
int m=(l+r)>>1;
int ans=0;
if(a<=m)
ans=query2(o<<1,l,m,a,b);
if(b>=m+1)
ans=add(ans,query2(o<<1|1,m+1,r,a,b));
return ans;
}
} inline void TestCase(int ti) {
int n,m;
while(~scanf("%d",&n)) {
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
build(1,1,n);
scanf("%d",&m);
for(int i=1; i<=m; i++) {
char a[2];
int b,c;
scanf("%s%d%d",a,&b,&c);
if(a[0]=='Q') {
printf("%d\n",sub(query2(1,1,n,b,c),mul(b-1,query1(1,1,n,b,c))));
} else {
update(1,1,n,b,c);
}
}
}
}

这个树状数组就不太好懂了,不过空间是线段树的1/4,速度是其两倍。

单点改值就把差值update上去就可以了。然后记得把原始值也顺手改了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; inline int read() {
int x=0;
int f=0;
char c;
do {
c=getchar();
if(c=='-')
f=1;
} while(c<'0'||c>'9');
do {
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
} while(c>='0'&&c<='9');
return f?-x:x;
} inline void _write(int x) {
if(x>9)
_write(x/10);
putchar(x%10+'0');
} inline void write(int x) {
if(x<0) {
putchar('-');
x=-x;
}
_write(x);
putchar('\n');
} void TestCase(int ti); int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
//freopen("Yinku.out","w",stdout);
#endif // Yinku
int T=1;
for(int ti=1; ti<=T; ti++)
TestCase(ti);
} /*--- ---*/ const int MAXM=100000;
int a[MAXM+5];
int bit[MAXM+5];
int bit2[MAXM+5]; int n; const int mod=1000000007; inline int add(const int &a,const int &b) {
int res=a+b;
return res>=mod?res-mod:res;
} inline int sub(const int &a,const int &b) {
int res=a-b;
return res<0?res+mod:res;
} inline int mul(const int &a,const int &b) {
ll res=1ll*a*b;
return res>=mod?res%mod:res;
} inline int sum(int x,int bit[]) {
int res=0;
while(x) {
res=add(res,bit[x]);
x-=x&-x;
}
return res;
} inline void update(int x,int v,int bit[]) {
while(x<=n) {
bit[x]=add(bit[x],v);
x+=x&-x;
}
} inline int range_sum(int x,int y,int bit[]) {
return sub(sum(y,bit),sum(x-1,bit));
} inline void TestCase(int ti) {
int m;
while(~scanf("%d",&n)) {
memset(bit,0,sizeof(bit));
memset(bit2,0,sizeof(bit2));
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
update(i,a[i],bit);
update(i,mul(i,a[i]),bit2);
}
scanf("%d",&m);
for(int i=1; i<=m; i++) {
char s[2];
int b,c;
scanf("%s%d%d",s,&b,&c);
if(s[0]=='Q') {
printf("%d\n",sub(range_sum(b,c,bit2),mul(b-1,range_sum(b,c,bit))));
} else {
int delta=sub(c,a[b]);
update(b,delta,bit);
update(b,mul(b,delta),bit2);
a[b]=c;
}
}
}
}

SCUT - 337 - 岩殿居蟹 - 线段树 - 树状数组的更多相关文章

  1. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  2. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  3. HDU 1556 线段树或树状数组,插段求点

    1.HDU 1556  Color the ball   区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...

  4. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  5. 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings

    谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...

  6. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  7. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  8. Turing Tree_线段树&树状数组

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

  9. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

随机推荐

  1. Git_学习_05_ 解决冲突

    二.参考资料 1.使用git pull文件时和本地文件冲突怎么办? 2.git 冲突解决

  2. Mybatis异常_01_Invalid bound statement (not found)

    异常信息:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.taotao.map ...

  3. Struts2 - 配置文件详解

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...

  4. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

  5. Linux下几种RTP协议实现的比较和JRTPLIB编程讲解

    流媒体指的是在网络中使用流技术传输的连续时基媒体,其特点是在播放前不需要下载整个文件,而是采用边下载边播放的方式,它是视频会议. IP电话等应用场合的技术基础.RTP是进行实时流媒体传输的标准协议和关 ...

  6. Arc073_F Many Moves

    传送门 题目大意 有$n$个格子从左到右依次挨着,一开始有两枚棋子分布在$A,B$某一个或两个格子里,有$m$个操作,第$i$次操作要求你把其中一个棋子移到$X_i$上,移动一个棋子的代价是两个格子之 ...

  7. 洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers

    题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. ...

  8. spring IOC 注解@Required

    @Required注解适用于bean属性的setter方法,使用@Required的方法必须在xml中填充,负责报错 例如下面的例子中,student中的setAge和setName有@Require ...

  9. SQL中replace函数

    string sql1 = "select price from dbo.eazy_farm where REPLACE(title,' ','')='" + cainame + ...

  10. Framework配置错误

    转自:http://blog.csdn.net/ked/article/details/25052955 VS2012命令提示符无法使用的解决方法 打开VS2012命令提示符时报错“ERROR: Ca ...