区间更新求和

主要用来练习splay树区间更新问题

//splay树的题解

// File Name: 3468-splay.cpp
// Author: Zlbing
// Created Time: 2013年08月09日 星期五 16时30分32秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--) #define L ch[x][0]
#define R ch[x][1]
#define KT (ch[ch[rt][1]][0])
const int MAXN=2e5+;
struct SplayTree{
int ch[MAXN][];
int pre[MAXN],sz[MAXN],val[MAXN];
int rt,top;
void Rotate(int x,int f)
{
int y=pre[x];
down(y);down(x);
ch[y][!f]=ch[x][f];
pre[ch[x][f]]=y;
pre[x]=pre[y];
if(pre[x])
ch[pre[y]][ch[pre[y]][]==y]=x;
ch[x][f]=y;
pre[y]=x;
up(y);
}
void Splay(int x,int goal)
{
down(x);
while(pre[x]!=goal)
{
down(pre[pre[x]]);
down(pre[x]);
down(x);
if(pre[pre[x]]==goal)
Rotate(x,ch[pre[x]][]==x);
else
{
int y=pre[x],z=pre[y];
int f=(ch[z][]==y);
if(ch[y][f]==x)
Rotate(x,!f),Rotate(x,f);
else Rotate(y,f),Rotate(x,f);
}
}
up(x);
if(goal==)rt=x;
}
void RTO(int k,int goal)
{
int x=rt;
down(x);
while(sz[L]+!=k)
{
if(k<sz[L]+)x=L;
else
{
k-=(sz[L]+);
x=R;
}
down(x);
}
Splay(x,goal);
}
void vist(int x)
{
if(x)
{
printf("结点%2d : 左儿子 %2d 右儿子 %2d val: %2d sum=%lld\n",x,ch[x][],ch[x][],val[x],sum[x]);
vist(L);
vist(R);
}
}
void debug()
{
puts(""); vist(rt); puts("");
}
void up(int x)
{
sz[x]=+sz[L]+sz[R];
sum[x]=val[x]+sum[L]+sum[R];
}
void down(int x)
{
if(add[x])
{
val[L]+=add[x];
val[R]+=add[x];
add[L]+=add[x];
add[R]+=add[x];
sum[L]+=(LL)add[x]*sz[L];
sum[R]+=(LL)add[x]*sz[R];
add[x]=;
}
}
void Newnode(int &x,int c,int f)
{
x=++top;
L=R=; sz[x]=; pre[x]=f;
val[x]=sum[x]=c;
add[x]=;
}
void build(int &x,int l,int r,int f)
{
if(l>r)return;
int m=(l+r)>>;
Newnode(x,num[m],f);
build(L,l,m-,x);
build(R,m+,r,x);
up(x);
}
void init(int n)
{
ch[][]=ch[][]=pre[]=;
sz[]=rt=top=; add[]=sum[]=;
Newnode(rt,-,);
Newnode(ch[rt][],-,rt);
sz[rt]=;
for(int i=;i<=n;i++)
scanf("%d",&num[i]); build(KT,,n,ch[rt][]);
up(ch[rt][]);up(rt);
}
void update()
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
RTO(l,);
RTO(r+,rt);
add[KT]+=c;
val[KT]+=c;
sum[KT]+=(LL)c*sz[KT];
}
void query()
{
int l,r;
scanf("%d%d",&l,&r);
RTO(l,);
RTO(r+,rt);
printf("%lld\n",sum[KT]);
}
LL sum[MAXN];
int add[MAXN];
int num[MAXN];
}spt;
int main()
{
int m,n;
char op[];
while(~scanf("%d%d",&n,&m))
{
spt.init(n);
while(m--)
{
scanf("%s",op);
if(op[]=='Q')spt.query();
else spt.update();
}
}
return ;
}

线段树的题解

// File Name: /home/neuacm/ACM/POJ/3468.cpp
// Author: Zlbing
// Created Time: 2013年08月09日 星期五 14时35分11秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MAXN=1e5+;
LL col[MAXN<<];
LL sum[MAXN<<];
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int l,int r)
{
if(col[rt])
{
col[rt<<]+=col[rt];
col[rt<<|]+=col[rt];
int m=(l+r)>>;
sum[rt<<]+=(m-l+)*col[rt];
sum[rt<<|]+=(r-m)*col[rt];
col[rt]=;
}
}
void build(int l,int r,int rt)
{
col[rt]=;
if(l==r)
{
//scanf("%I64d",&sum[rt]);
cin>>sum[rt];
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
pushdown(rt,l,r);
int m=(l+r)>>;
LL ret=;
if(L<=m)ret+=query(L,R,lson);
if(R>m)ret+=query(L,R,rson);
return ret;
}
void update(int L,int R,int p,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
col[rt]+=p;
sum[rt]+=(r-l+)*p;
return;
}
pushdown(rt,l,r);
int m=(l+r)>>;
if(L<=m)update(L,R,p,lson);
if(R>m)update(L,R,p,rson);
pushup(rt);
}
void debug(int l,int r,int rt)
{
if(l==r)
{
printf("l==%d r==%d rt=%d sum[rt]=%lld col[rt]=%lld\n",l,r,rt,sum[rt],col[rt]);
return ;
}
printf("l==%d r==%d rt=%d sum[rt]=%lld col[rt]=%lld\n",l,r,rt,sum[rt],col[rt]);
int m=(l+r)>>;
debug(lson);
debug(rson);
}
int main()
{
int n,m;
//std::ios::sync_with_stdio(false);
while(~scanf("%d%d",&n,&m))
{
build(,n,);
char ch[];
int a,b,c;
REP(i,,m)
{
scanf("%s",ch);
if(ch[]=='Q')
{
scanf("%d%d",&a,&b);
LL ans=query(a,b,,n,);
//debug(1,n,1);
cout<<ans<<endl;
}
else if(ch[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,,n,);
//debug(1,n,1);
}
}
}
return ;
}

POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  5. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  6. POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  8. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  10. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

随机推荐

  1. tcmalloc资料

    1. 确定dylib在max os是可以成功的. http://lists.apple.com/archives/perfoptimization-dev/2008/Dec/msg00002.html ...

  2. Android推送技术研究

    前言 最近研究Android推送的实现, 研究了两天一夜, 有了一点收获, 写下来既为了分享, 也为了吐槽. 需要说明的是有些东西偏底层硬件和通信行业, 我对这些一窍不通, 只能说说自己的理解. 为什 ...

  3. 关于javascript dom扩展:Selector API

    众多javascript库中最常用的一项功能,就是根据css选择符选择与某个模式匹配的DOM元素.之前由于对javascript的认识较低,对javascript对DOM操作还停留在getElemen ...

  4. RMQ 与 LCA-ST算法

    RMQ算法 区间求最值的算法,用区间动态规划(nlogn)预处理,查询O(1) http://blog.csdn.net/y990041769/article/details/38405063 (PO ...

  5. oracle中创建表时添加注释

    CREATE TABLE t1(id varchar2(32) primary key,name VARCHAR2(32) ,age VARCHAR2(32) );comment on column ...

  6. ASP.NET中如何生成图形验证码

    通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3 ...

  7. Android layout的横竖屏处理

    一.layout-land和layout-prot的区别与使用 默认情况下,创建的Android项目里只有一个layout文件夹,尽管这样也可以横竖屏切换用,但是某些布局横屏过后闲的格外的丑,如下图 ...

  8. Spring MVC返回json数据给Android端

    原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...

  9. Initializer block.

    Ref: Initializing Fields Instance initializers are permitted to refer to the current object via the ...

  10. 解决c#处理excel时故障 找不到可安装的 isam

    直接拷贝的以前代码,但因软件版本,系统环境的变化,导致提示“找不到可安装的 isam”. 我目前新的软件环境:win8.1+office2010+vs2013 解决办法是修改连接字符串: 处理exce ...