题意:有1到n的数组,每次删除第k小的值,并求和

题解:splay基本操作,删除+合并

坑点:由于不会c++指针操作,sb的只删除了头指针导致一直mle

#include<bits/stdc++.h>
#include<ext/rope>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct Node{
Node* ch[];
int v;
int s;
int cmp(int x)const{
int d = x - ch[]->s;
if(d==)return -;
return d<= ? :;
}
void maintain()
{
s = + ch[]->s + ch[]->s;
}
};
Node* null;
void Rotate(Node* &o,int d)
{
Node* k = o->ch[d^];
o->ch[d^] = k->ch[d];
k->ch[d] = o;
o->maintain();k->maintain();
o = k;
}
void splay(Node* &o,int k)
{
int d = o->cmp(k);
if(d==)k -= o->ch[]->s + ;//利用二叉树性质
if(d!=-)
{
Node* p = o->ch[d];
int d2 = p->cmp(k);
int k2 = (d2== ? k:k-p->ch[]->s-);
if(d2!=-)
{
splay(p->ch[d2],k2);
if(d==d2)Rotate(o,d^);
else Rotate(o->ch[d],d);
}
Rotate(o,d^);
}
}
Node* Merge(Node* left,Node* right)
{
splay(left,left->s);//把排名最大的数splay到根
left->ch[] = right;
left->maintain();
return left;
}
void split(Node* o,int k,Node* &left,Node* &right)
{
splay(o,k);//把排名为k的节点splay到根,右侧子树所有节点排名比k大,左侧小
right = o->ch[];
o->ch[] = null;
left = o;
left->maintain();
}
Node *root;
void init(int sz)
{
null=new Node;
null->s=;
root=new Node;
root->v=;
root->ch[]=root->ch[]=null;
root->maintain();
Node* p;
for(int i=;i<=sz;i++)
{
p=new Node;
p->v=i;p->s=;
p->ch[]=root,p->ch[]=null;
root=p;
root->maintain();
}
}
void deletetree(Node* &o)
{
if(o!=null)
{
deletetree(o->ch[]);
deletetree(o->ch[]);
delete o;
}
}
int main()
{
int t,cnt=;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
init(n+);
ll ans=;
while(m--)
{
int a;
scanf("%d",&a);
Node *o,*left,*mid,*right;
split(root,a,left,o);
split(o,,mid,right);
ans+=mid->v-;
root = Merge(Merge(left,right),mid);
}
printf("Case %d: %lld\n",++cnt,ans);
deletetree(root);
delete null;
}
return ;
}
/************ ************/

hdu4217splay的更多相关文章

随机推荐

  1. Linux考试题附答案

    一.选择题 1.在登录Linux时,一个具有唯一进程ID号的shell将被调用,这个ID是什么(B)? A.NID B.PID C.UID D.CID 2.下面哪个目录存放用户密码信息(B) A./b ...

  2. 在windows和linux之间用SecureCRT来上传和下载文件

    SecureCRT可以使用linux下的zmodem协议来快速的传送文件,使用非常方便.具体步骤:一.在使用SecureCRT上传下载之前需要给服务器安装lrzsz:A:CentOS中使用yum安装即 ...

  3. android自定义控件(三)ProgressBar

     1.ProgressBar有两个进度,一个是android:progress,另一个是android:secondaryProgress.比如视频的缓存进度以及播放进度. 在这里缓存的进度就可以是a ...

  4. 001-Spring在代码中获取bean的几种方式

    一.概述 方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类Applicati ...

  5. PyQt4 颜色选择,字体选择代码

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  6. English Phrases

    @1:Phrases requst sth from/of sb 向某人要求某物 a new lease on life   重获新生.焕发生机 state of the art 最先进的 at th ...

  7. SourceTree的基本使用---基本介绍/本地开发

    转载自https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 wind ...

  8. $python正则表达式系列(2)——re模块常用函数

    本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...

  9. shell set 命令

    用set命令可以设置各种shell选项或者列出shell变量.单个选项设置常用的特性.在某些选项之后-o参数将特殊特性打开.在某些选项之后使用+o参数将关闭某些特性,不带任何参数的set命令将显示sh ...

  10. R的基础学习之数据结构

    来源:http://blog.qiubio.com:8080/archives/3753/4 1.atomic vector :一维的,放置同一类型数据的数据类型 1.1创建:由c()函数 ,seq( ...