A Simple Problem with Integers

Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d
& %I64u

Appoint description: 
System Crawler  (2014-11-12)

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.

Each of the next Q lines represents an operation.

"C abc" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.

"Q ab" means querying the sum of AaAa+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

Hint

The sums may exceed the range of 32-bit integers.

这题应该算是线段树区间入目题目,只是还能够用Splay来做,用Splay来维护序列。用到了平衡二叉树的一个重要的性质那就是中序遍历是有序的。人生第一道Splay(感人TAT。QAQ)

代码例如以下:

/*************************************************************************
> File Name: Spaly.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年11月16日 星期日 00时14分26秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 100;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
LL add[maxn],sum[maxn];
int ch[maxn][2],siz[maxn],key[maxn],pre[maxn],A[maxn];
int root,tot;
void newnode(int &x,int fa,int Key)//新建节点
{
x=++tot;
pre[x]=fa;
siz[x]=1;
key[x]=sum[x]=Key;
ch[x][0]=ch[x][1]=add[x]=0;
}
void Modify(int x,int val){//区间更新
if(!x)return;
add[x]+=val;
key[x]+=val;
sum[x]+=(LL)val*siz[x];
}
void push_down(int x){//下传标记
if(!add[x])return ;
Modify(ch[x][0],add[x]);
Modify(ch[x][1],add[x]);
add[x]=0;
}
void push_up(int x){//更新节点
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+key[x];
}
void built(int &x,int L,int R,int fa){
if(L>R)return;
int M=(L+R)>>1;
newnode(x,fa,A[M]);
built(ch[x][0],L,M-1,x);
built(ch[x][1],M+1,R,x);
push_up(x);
}
void Init(int n)//初始化Spaly。加入了两个虚拟节点。便于提取区间,避免讨论
{
root=tot=0;
newnode(root,0,0);
newnode(ch[root][1],root,0);
for(int i=1;i<=n;i++)scanf("%d",A+i);
built(ch[ch[root][1]][0],1,n,ch[root][1]);
push_up(ch[root][1]);
push_up(root);
}
void print(int x){
if(!x)return;
print(ch[x][0]);
printf("%d ",key[x]);
print(ch[x][1]);
}
void Rotate(int x,bool kind){//旋转,true右旋,false左旋
int y=pre[x];
push_down(y);//下传标记
push_down(x); ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
ch[x][kind]=y; if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;//若y的父节点存在将其孩子指针指向x
pre[x]=pre[y];
pre[y]=x;
push_up(y);//更新回来,须要注意的是。要先更新孩子
push_up(x);
}
void Spaly(int x,int goal){//伸展操作。将x旋转到goal以下
push_down(x);
while(pre[x]!=goal){
if(pre[pre[x]]==goal)Rotate(x,ch[pre[x]][0]==x);
else{
int y=pre[x];
bool kind=(ch[pre[y]][0]==y);
if(ch[y][kind]==x){
Rotate(x,!kind);
Rotate(x,kind);
}else{
Rotate(y,kind);
Rotate(x,kind);
}
}
}
push_up(x);
if(goal==0)root=x;//假设goal是0说明已经将x旋转到了根,所以要更新root
}
int Get_kth(int x,int k){//序列中的第k个值
int t=siz[ch[x][0]]+1;
if(t==k)return x;
if(t>k)return Get_kth(ch[x][0],k);
return Get_kth(ch[x][1],k-t);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
siz[0]=sum[0]=0;//不存在的节点初始化为0避免讨论
int n,q,l,r,x;
scanf("%d%d",&n,&q);
Init(n);
char cmd[5];
while(q--){
scanf("%s%d%d",cmd,&l,&r);
Spaly(Get_kth(root,l),0);
Spaly(Get_kth(root,r+2),root);
if(cmd[0]=='Q'){
printf("%lld\n",sum[ch[ch[root][1]][0]]);
}else{
int Add;
scanf("%d",&Add);
Modify(ch[ch[root][1]][0],Add);
push_up(ch[root][1]);
push_up(root);
}
}
return 0;
}

Splay POJ3468(老题新做)的更多相关文章

  1. 【NOI2007】项链工厂 ——老题新做.jpg

    第一次是用 ODT 过的...(虽说跑得飞慢但它就是能过) 而且还写了发题解... 第二次是在考场上碰到了这道题,然后居然打了线段树,各种 bug 直接让代码爆零 但还是补好了代码重新交了一发,发现跑 ...

  2. 旧题新做:从idy的视角看数据结构

    “今天你不写总结……!!!” 额…… 还是讲我的吧.这些考试都是idy出的题. 20170121:DFS序. ST表.线段树练习 这是第一次考数据结构. Problem 1. setsum 1 sec ...

  3. 贪心/构造/DP 杂题选做

    本博客将会收录一些贪心/构造的我认为较有价值的题目,这样可以有效的避免日后碰到 P7115 或者 P7915 这样的题就束手无策进而垫底的情况/dk 某些题目虽然跟贪心关系不大,但是在 CF 上有个 ...

  4. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  5. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

  6. 老梗新玩「GitHub 热点速览 v.22.34」

    作者:HelloGitHub-小鱼干 不知道你是否和我有一样的烦恼,最近的流行梗当自己要用拿来造词时,就陷入了不知道咋"换壳"的尴尬地步.sao-gen-gen 大大减少了你老梗新 ...

  7. (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)

    刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...

  8. Atcoder 水题选做

    为什么是水题选做呢?因为我只会水题啊 ( 为什么是$Atcoder$呢?因为暑假学长来讲课的时候讲了三件事:不要用洛谷,不要用dev-c++,不要用单步调试.$bzoj$太难了,$Topcoder$整 ...

  9. LCT裸题泛做

    ①洞穴勘测 bzoj2049 题意:由若干个操作,每次加入/删除两点间的一条边,询问某两点是否连通.保证任意时刻图都是一个森林.(两点之间至多只有一条路径) 这就是个link+cut+find roo ...

随机推荐

  1. Chosen 基本使用

    点击下载Chosen 引入文件 chosen.css jquery-1.7.1.min.js chosen.jquery.js 绑定数据: for (var i = 0; i < data.le ...

  2. c++面试知识点

    static #include<stdio.h> #include<iostream> #include<assert.h> using namespace std ...

  3. 怎样实现IOS开发中的数据存储方式

    iOS 开发中,一般有如下几种数据存储方式.需要根据具体的业务场景,选择 合适的数据存储方式. (1)  用户默认设置 – 这种情况通常不需要用户干预,如游戏通关信息,Video 播放记录,或者 Ap ...

  4. ajaxFileUpload SyntaxError: syntax error

    在使用ajaxFileUpload上传文件时,Chrome没问题,IE和Firefox出错,Firefox报SyntaxError: syntax error错误 JS代码例如以下: $.ajaxFi ...

  5. XCode Could not launch &quot;&quot; failed to get the task for process

    在Xcode下编译project正常,在模拟器下执行正常,最后在真机上执行的时候出现了例如以下错误: Could not launch "FeedMeWorms" failed t ...

  6. [HeadFirst-HTMLCSS入门][第九章盒模式]

    新属性 line-height 行间距 line-height: 1.6em; border 边框 属性值 solid 实线 double 双实线 groove 一个槽 outset 外凸 inset ...

  7. TreeView中右击直接获取节点的方法

    在TreeView中无法直接右击得到一个节点,因为当你选中其中一个右击时(不能是第一个)他会默认跳到第一个. 有时我们要想直接右击得到选中的节点,又时我们又想选中直接右击跳出一个快捷菜单怎么办了! 在 ...

  8. PHP学习笔记二十二【静态方法二】

    <?PHP class Student{ public static $fee; public $name; //构造函数 function __construct($name) { $this ...

  9. Unity-视图

    Project(项目)视图:显示项目所包含的全部资源,每个Unity项目文件夹都会包含一个Assets文件夹,Assets文件夹是用来存放用户所创建的对象和导入的资源. Hierarchy(层级)视图 ...

  10. HDU-简单计算器-1237

    这道题我做了一天,把中缀表达式转化为后缀表达式,但遇到了储存的问题,考虑了好久,写出后又调试,弄了一天,下面说一下中缀表达式转换后缀表达式: 算法: 中缀表达式转后缀表达式的方法: 1.遇到操作数:直 ...