CF 295E Yaroslav and Points(Splay)

题目大意:

两个操作

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),码迷,mamicode.com

时间: 2024-08-01 11:32:23

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

hdu 4717 The Moving Points(三分)

http://acm.hdu.edu.cn/showproblem.php?pid=4717 大致题意:给出每个点的坐标以及每个点移动的速度和方向.问在那一时刻点集中最远的距离在所有时刻的最远距离中最小. 比赛时一直以为是计算几何,和线段相交什么的有关.赛后队友说这是道三分,仔细想了想确实是三分,试着画画图发现它是一个凸性函数,存在一个最短距离.然后三分时间就可以了. #include <stdio.h> #include <iostream> #include <map&g

zoj 3612 Median (splay)

题目大意: 添加和删除一个数,然后输出中位数. 简单的Splay   维护Splay上有多少个节点就可以了 #include <cstdio> #include <iostream> #define inf 1LL<<60 #define maxn 222222 #define keyTree (ch[ch[root][1]][0]) using namespace std; typedef long long LL; int S[maxn],que[maxn],ch[

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

P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 输入输出格式 输入格式: 第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2, \cdots n-1,n)(1,2,?n−1,n) m表示翻转操作次数 接下来m行每行两个数 [l,r][l,

【BZOJ3506】排序机械臂(Splay)

[BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标记的问题 我只在第K大放会鬼.. 所以在Splay里面也放了一次(和LCT一样的) 然而我每次都把排到了正确位置的元素直接给删掉了... 所以跑的很慢很慢... #include<iostream> #include<cstdio> #include<cstdlib> #i

BZOJ 1251 序列终结者(Splay)

题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思.这道题目 就叫序列终结者吧.[问题描述] 给定一个长度为N的序列,每个序列的元素是一个整数(废话).要支持以下三种操作: 1. 将 [L, R] 这个区间内的所有数加上 V. 2. 将 [

HDU 4441 Queue Sequence(splay)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:一个数列,三种操作:(1)插入:找到没在当前数列中的最小的正整数i,将其插在位置p之后,并将-i插入某个位置使得满足先进先出(i表示进,-i表示出),这个位置尽量靠右:(2)删除:删掉数字i以及-i:(3)询问:查询i和-i之间的数字的和. 思路:对于没在数列中的数字可以用一个set直接维护.i的插入是正常的splay操作.对于-i的插入,我们首先找到i之前有几个正数,比如有x个,那么-

【BZOJ】1251: 序列终结者(splay)

http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint... #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #inc

平衡树(splay)

求区间最大值模板 1 struct node{ 2 int f[maxn];//父亲结点 3 int ch[maxn][2];//左右孩子 4 int key[maxn];//结点i代表的数字 5 int cnt[maxn];//结点i出现的次数,也可以为全值.平衡树没有相同值的结点,所以如果出现了相同值时,cnt[i]++,或者cnt[i] += key[i] 6 int siz[maxn];//包括i的子树的大小 7 int val[maxn]; 8 int Max[maxn]; 9 int

HYSBZ 1588 营业额统计 (Splay)

题意:给出一个公司每一天的营业额,求每天的最小波动值之和.该天的最小波动值= min { 绝对值| 该天以前某一天的营业额-该天的营业额 | }.第一天的最小波动值就是其自己. 思路:Splay伸展树的入门题,仅有splay,insert,rotate这三个主要的函数而已. 将一个数字(营业额)插入树中,并把其splay到根位置,然后其前驱和后继中离它较近的一个用来求最小波动值.注意可能没有前驱/后继.对第一个数特别处理. 注意:此题的数据有缺陷,读入营业额之前先将变量清零. 1 #includ