Poj3468-A Simple Problem with Integers(伸展树练练手)
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000. The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000. Each of the next Q lines represents an operation. "C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000. "Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15 题意: 给出N个数,有两种操作
Q a b 查询[a,b]区间的和
C a b c [a,b]区间所有值加上c 解析:成段更新问题,线段树已经可以做,我试着用伸展树做做。 代码:
//这题用线段树就可以做了,拿来用伸展树练练手
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
int A[maxn],cnt; //A数组保存数,cnt是节点标号,我是用数组模拟的
struct treap
{
treap* son[]; //左右儿子
int s;
LL v,add,sum;
treap(){ s=v=add=sum=; son[]=son[]=NULL; }
treap(LL nv);
int rk(){ return son[]->s+; } //排名,第几个数
int cmp(int k) //比较,如果相等返回-1,小于返回0,大于1
{
if(k==rk()) return -;
return k<rk()?:;
}
void Set(LL d)
{
v+=d;
add+=d;
sum+=d*s;
}
void pushup()
{
s=son[]->s+son[]->s+;
sum=son[]->sum+son[]->sum+v;
}
void pushdown() //处理懒惰标记
{
if(add!=)
{
son[]->Set(add);
son[]->Set(add);
add=;
}
}
}null,tr[maxn];
treap::treap(LL nv)
{
sum=v=nv;
s=;
add=;
son[]=son[]=&null;
}
treap* NewNode(LL x)
{
tr[cnt]=treap(x);
return tr+cnt++;
}
struct splaytree
{
int Size;
treap* root;
splaytree(){ Size=; root=&null; }
void Rotate(treap* &t,int d) //翻转操作
{
t->pushdown();
treap* p=t->son[d^];
p->pushdown();
t->son[d^]=p->son[d];
p->son[d]=t;
t->pushup();
t=p;
t->pushup();
}
void Splay(treap* &t,int k) //将第k大的节点伸展到根
{
t->pushdown();
int d=t->cmp(k);
if(d!=-)
{
if(d) Splay(t->son[d],k- t->rk());
else Splay(t->son[d],k);
Rotate(t,d^);
}
}
void Build(treap* &t,int le,int ri) //将N个数建成一棵树
{
if(le>ri) return;
int mid=(le+ri)/;
t=NewNode(A[mid]);
Build(t->son[],le,mid-);
Build(t->son[],mid+,ri);
t->pushup();
}
LL Query(treap* &t,int x,int y)
{
LL ret=;
Splay(t,y);
ret+=t->son[]->sum+t->v;
if(x>)
{
Splay(t,x-);
ret-=t->son[]->sum+t->v;
}
return ret;
}
void Add(treap* &t,int x,int y,int add)
{
Splay(t,y);
t->v+=add; t->sum+=add*y;
t->son[]->Set(add);
if(x>)
{
Splay(t,x-);
t->v-=add; t->sum-=add*(x-);
t->son[]->Set(-add);
}
}
};
int main()
{
int N,Q;
while(scanf("%d%d",&N,&Q)!=EOF)
{
for(int i=;i<=N;i++) scanf("%d",&A[i]);
splaytree spt;
cnt=;
spt.Build(spt.root,,N);
int x,y,d;
char op[];
while(Q--)
{
scanf("%s",op);
if(op[]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",spt.Query(spt.root,x,y));
}
else
{
scanf("%d%d%d",&x,&y,&d);
spt.Add(spt.root,x,y,d);
}
}
}
return ;
}
Poj3468-A Simple Problem with Integers(伸展树练练手)的更多相关文章
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)
伸展数最基本操作的模板,区间求和,区间更新.为了方便理解,特定附上一自己搞的搓图 这是样例中的数据输入后建成的树,其中的1,2是加入的边界顶点,数字代表节点编号,我们如果要对一段区间[l, r]进行操 ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
随机推荐
- hdu 4512 吉哥系列故事——完美队形I_LCIS
题目链接 题意: 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要 求,则就是新 ...
- c语言typedef运用之结构体
#include <stdio.h> #include <stdlib.h> typedef struct stu { ]; int score; }stu_info; int ...
- 基于css制作轮播图的部分效果
在轮播图中,我们可以通过鼠标在特定位置上的滑动来实现元素背景的改变.通常在制作轮播图时,我们首先想到的是js中的交互.可是,如果我们无法使用js,只能单纯的靠css又该如何实现这一效果呢?下面,本人将 ...
- J2EE基础总结(1)——J2EE入门
J2EE诞生的背景 在传统的开发模式(单层应用结构)下.应用普遍存在下面致命缺点: - 数据.页面和业务逻辑在一个逻辑层次中.功能紧密耦合. - 代码重用性极低,可维护性差. - 应用耦合度高,全然没 ...
- [置顶] STM32移植contiki进阶之三(中):timer 中文版
鉴于自己英语水平不高,在这里,将上一篇关于contiki 的timer的文章翻译为中文,让自己在学习的时候,更方便点.文中有许多不是很通顺的地方,将就吧. Timers Contiki系统提供了一套时 ...
- UVA 825 Walking on the Safe Side(记忆化搜索)
Walking on the Safe Side Square City is a very easy place for people to walk around. The two-way ...
- NYOJ128 前缀式计算 【栈】
前缀式计算 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 先说明一下什么是中缀式: 如2+(3+4)*5这样的我们最常见的式子就是中缀式. 而把中缀式按运算顺序加上 ...
- (原)torch7中添加新的层
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6069627.html 参考网址: http://torch.ch/docs/developer-doc ...
- PAT 64.Complete Binary Search Tree
题目链接: http://pat.zju.edu.cn/contests/pat-a-practise/1064 思路分析: 1)先对数组排好序. 2)采用中序遍历的方式,将排好序的元素逐个插入在完全 ...
- memcached介绍及基本使用
一:概念 memcached是LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件.现在已成为mixi,hatena,facebook ...