题目大意:

两个操作

1 id op  把id的位置+op

2 id op  查询在【id。op】之间的全部的数的差

思路:

关键是pushup函数。

自己退一下会发现。跟区间的总和,区间的节点个数有关。

比方假设左区间是 1 2 的话

右区间来一个 9

那么

就要加上

9-1+9-2

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#define inf 0x3f3f3f3f
#define maxn 222222
#define keyTree (ch[ch[root][1]][0])
//当把l-1放在根节点 r+1放在根节点的右子树
//那么根节点的右子树的左子树就是[l,r] 这个区间的全部值
using namespace std;
typedef long long LL;
int S[maxn],que[maxn],ch[maxn][2],pre[maxn],siz[maxn];
int root,top1,top2; LL ans[maxn],val[maxn],a[maxn],b[maxn];
LL sum[maxn];
set <LL> tab;
void Treaval(int x)
{
if(x)
{
Treaval(ch[x][0]);
printf("%I64d ",val[x]);
Treaval(ch[x][1]);
}
}
void debug()
{
// printf("root=%d\n",root);
Treaval(root);
puts("");
}
void New(int &x,int PRE,LL v)
{
if(top2)x=S[--top2];
else x=++top1; ch[x][0]=ch[x][1]=0;
siz[x]=1;
pre[x]=PRE;
/*special*/
sum[x]=v;
ans[x]=0;
val[x]=v;
}
void pushup(int x)/*special*/
{
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1; sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x];
ans[x]=ans[ch[x][0]]+ans[ch[x][1]]+siz[ch[x][0]]*val[x]-sum[ch[x][0]]+sum[ch[x][1]]-val[x]*siz[ch[x][1]]+siz[ch[x][0]]*sum[ch[x][1]]-siz[ch[x][1]]*sum[ch[x][0]];
}
void pushdown(int x)/*special*/
{ }
void build(int &x,int s,int e,int f)
{
if(s>e)return;
int mid=(s+e)>>1; New(x,f,a[mid]); if(s<mid)build(ch[x][0],s,mid-1,x);
if(e>mid)build(ch[x][1],mid+1,e,x);
pushup(x);
} void Rotate(int x,int kind)
{
int y=pre[x];
pushdown(x);
pushdown(y);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
pushup(y);
} void Splay(int x,int goal)
{
pushdown(x);
while(pre[x]!=goal)
{
if(pre[pre[x]]==goal)
Rotate(x,ch[pre[x]][0]==x);
else
{
int y=pre[x];
int kind=ch[pre[y]][0]==y;
if(ch[y][kind]==x){
Rotate(x,!kind);
Rotate(x,kind);
}
else {
Rotate(y,kind);
Rotate(x,kind);
}
}
}
pushup(x);
if(goal==0)root=x;
} void RotateTo(int k,int goal)
{
int r=root;
pushdown(r);
while(siz[ch[r][0]]!=k)
{
if(k<siz[ch[r][0]])
{
r=ch[r][0];
}
else
{
k-=siz[ch[r][0]]+1;
r=ch[r][1];
}
pushdown(r);
}
Splay(r,goal);
} void erase(int x)
{
int y=pre[x];
int head=0,tail=0;
for(que[tail++]=x;head<tail;head++)
{
S[top2++]=que[head];
if(ch[que[head]][0])que[tail++]=ch[que[head]][0];
if(ch[que[head]][1])que[tail++]=ch[que[head]][1];
}
ch[y][ch[y][1]==x]=0;
pushup(y);
} void init(int n)
{
root=top1=top2=0;
ch[0][0]=ch[0][1]=siz[0]=pre[0]=0;
ans[0]=sum[0]=val[0]=0; New(root,0,-inf);
New(ch[root][1],root,inf); siz[root]=2; for(int i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
tab.insert(a[i]);
b[i]=a[i];
}
tab.insert(-inf);tab.insert(inf);
sort(a,a+n);
build(keyTree,0,n-1,ch[root][1]);
pushup(ch[root][1]);
pushup(root);
} int find(LL x,int t)
{
if(x==val[t])return t;
else if(x>val[t])return find(x,ch[t][1]);
else return find(x,ch[t][0]);
} set<LL>::iterator it; int add(int x,LL Num,int pos)
{
if(Num<=val[x])
{
if(ch[x][0]==0)
{
Splay(x,0);
int S=siz[ch[root][0]];
RotateTo(S-1,0);
RotateTo(S,root);
New(keyTree,ch[root][1],Num);
pushup(ch[root][1]);
pushup(root);
}
else add(ch[x][0],Num,pos);
}
else
{
if(ch[x][1]==0)
{
Splay(x,0);
int S=siz[ch[root][0]];
RotateTo(S,0);
RotateTo(S+1,root);
New(keyTree,ch[root][1],Num);
pushup(ch[root][1]);
pushup(root);
}
else add(ch[x][1],Num,siz[ch[x][0]]+1+pos);
}
}
int main()
{
tab.clear();
int n;
scanf("%d",&n);
init(n);
//debug();
int m;
scanf("%d",&m); while(m--)
{
int op;
int l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
it=tab.lower_bound(b[l-1]); //printf("*it = %I64d\n",*it); int pos=find(*it,root);
//printf("---%I64d\n",val[pos]);
Splay(pos,0); int Spos=siz[ch[root][0]];
RotateTo(Spos-1,0);
RotateTo(Spos+1,root); //erase(keyTree);
erase(keyTree);
pushup(ch[root][1]);
pushup(root); tab.erase(it); b[l-1]+=r; tab.insert(b[l-1]); add(root,b[l-1],0);
}
else
{
it=tab.lower_bound(l);
it--;
//printf("%I64d\n",*it);
//printf("%I64d\n",val[]);
Splay(find(*it,root),0);
it=tab.upper_bound(r);
Splay(find(*it,root),root);
printf("%I64d\n",ans[keyTree]);
}
//debug();
}
return 0;
}

CF 295E Yaroslav and Points(Splay)的更多相关文章

  1. 【BZOJ3506】排序机械臂(Splay)

    [BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...

  2. 【BZOJ1500】【NOI2005】维修数列(Splay)

    [BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...

  3. 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)

    [BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题

  4. 【BZOJ1056】[HAOI2008]排名系统(Splay)

    [BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...

  5. 【BZOJ2329】括号修复(Splay)

    [BZOJ2329]括号修复(Splay) 题面 BZOJ 洛谷 题解 本来想着用线段树来写 但是有一个区间翻转 所以不能用线段树了,就只能用平衡树 然后直接\(Splay\)就好了 注意一下几个标记 ...

  6. CF 984C Finite or not? (数论)

    CF 984C Finite or not? (数论) 给定T(T<=1e5)组数据,每组数据给出十进制表示下的整数p,q,b,求问p/q在b进制意义下是否是有限小数. 首先我们先把p/q约分一 ...

  7. P3391 【模板】文艺平衡树(Splay)新板子

    P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...

  8. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  9. BZOJ 1251 序列终结者(Splay)

    题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...

随机推荐

  1. Queue学习

    Queue在Python中可以算作是一种容器,但是他和list,set,dict不一样. 1. Queue不是Python内置类型.它在Queue模块中定义. 2. 它不是iterator容器,他不能 ...

  2. Cocos2dx开发(3)——Cocos2dx打包成APK,ANT环境搭建

    前面cocos2dx的运行环境(Android SDK,JDK,),最后Cocos2dx的APK的打包环境,最运行环境上再加ANT环境就好了 1.ANT下载配置 官网下载:http://ant.apa ...

  3. ecshop分页问题1

    点解下一页时弹出 查找原因: json返回 分页查询之后返回的 filter 数据为空 问题在这: $deliveryInfo['fliter']  $deliveryInfo['page_count ...

  4. PHP 字符串常用方法

    implode(“”,“”)-->字符串分割方法,第一个参数以什么样的形式分割,第二个参数需要分割的字符串 数组操作 is_array(),判断这个数是否是一个数组

  5. [转载]windows下安装Python虚拟环境virtualenvwrapper-win

    1 前言 由于Python的版本众多,还有Python2和Python3的争论,因此有些软件包或第三方库就容易出现版本不兼容的问题. 通过 virtualenv 这个工具,就可以构建一系列 虚拟的Py ...

  6. 省队集训Day3 light

    [问题描述] “若是万一琪露诺(俗称 rhl)进行攻击,什么都好,冷静地回答她的问题来吸引她.对方表现出兴趣的话,那就慢慢地反问.在她考虑答案的时候,趁机逃吧.就算是很简单的问题,她一定也答不上来.” ...

  7. operation 多线程

    2.Cocoa Operation 优点:不需要关心线程管理,数据同步的事情.Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue.NSOperati ...

  8. ASP.NET 安全认证

    一. 新建一个测试项目 新建一个测试项目,包含三张页面(Default.aspx.Login.aspx.UserInfo.aspx).   二. 修改 Web.config 1.把<authen ...

  9. python 单元测试

    http://blog.csdn.net/five3/article/details/7104466

  10. java 反射 动态代理

    在上一篇文章中介绍Java注解的时候,多次提到了Java的反射API.与javax.lang.model不同的是,通过反射API可以获取程序在运行时刻的内部结构.反射API中提供的动态代理也是非常强大 ...