这个题就是动态偏序对,每次操作做两个删除两个插入就好了。

#include<cstdio>
#include<iostream>
#include<cstring>
#define MAXN 100010
using namespace std;
typedef long long LL;
typedef double D;
const D a=0.756;
LL ans;
struct ScapeGoat_Tree
{
ScapeGoat_Tree *ch[];
int key,size,cover,ex;
bool bad()
{
return cover*a+<ch[]->cover||cover*a+<ch[]->cover;
}
void pushup()
{
size=ch[]->size+ch[]->size+ex;
cover=ch[]->cover+ch[]->cover+;
}
}*null,*stack[(MAXN<<)+],pool[(MAXN<<)+],*lst[(MAXN<<)+];
int top,len;
inline void Init()
{
null=pool;
null->ch[]=null->ch[]=null;
null->key=null->size=null->cover=null->ex=;
for(int i=;i<(MAXN<<);i++)stack[++top]=pool+i;
}
inline ScapeGoat_Tree *New(int key)
{
ScapeGoat_Tree *p=stack[top--];
p->ch[]=p->ch[]=null;
p->key=key;
p->size=p->ex=p->cover=;
return p;
}
struct Tree
{
Tree *ch[];
int mid,l,r;
ScapeGoat_Tree *root;
Tree(){ch[]=ch[]=NULL;mid=l=r=;root=null;}
void* operator new(size_t size);
}*root,*C,*mempool;
void* Tree :: operator new(size_t size)
{
if(C==mempool)
{
C=new Tree[(<<)+];
mempool=C+(<<)+;
}
C->root=null;
return C++;
}
void travel(ScapeGoat_Tree *p)
{
if(p==null)return;
travel(p->ch[]);
if(p->ex)lst[++len]=p;
else stack[++top]=p;
travel(p->ch[]);
}
ScapeGoat_Tree *divide(int l,int r)
{
if(l>r)return null;
int mid=(l+r)>>;
lst[mid]->ch[]=divide(l,mid-);
lst[mid]->ch[]=divide(mid+,r);
lst[mid]->pushup();
return lst[mid];
}
inline void rebuild(ScapeGoat_Tree *&p)
{
len=;
travel(p);
p=divide(,len);
}
ScapeGoat_Tree **insert(ScapeGoat_Tree *&p,int key)
{
if(p==null )
{
p=New(key);
return &null;
}
p->size++;
p->cover++;
ScapeGoat_Tree **ret=insert(p->ch[p->key<=key],key);
if(p->bad())ret=&p;
return ret;
}
inline void Insert(ScapeGoat_Tree *&Root,int key)
{
ScapeGoat_Tree **p=insert(Root,key);
if(*p!=null )rebuild(*p);
}
inline int Rank_Max(ScapeGoat_Tree *Root,int key)
{
ScapeGoat_Tree *now=Root;
int ret=;
while(now!=null )
if(now->key<=key)
now=now->ch[];
else
ret+=now->ch[]->size+now->ex,now=now->ch[];
return ret;
}
inline int Rank_Min(ScapeGoat_Tree *Root,int key)
{
ScapeGoat_Tree *now=Root;
int ret=;
while(now!=null )
if(now->key>=key)
now=now->ch[];
else
ret+=now->ch[]->size+now->ex,now=now->ch[];
return ret;
}
void del(ScapeGoat_Tree *p,int k)
{
p->size--;
if(p->ex&&p->ch[]->size+==k)
{
p->ex=;
return;
}
if(p->ch[]->size>=k) del(p->ch[],k);
else del(p->ch[],k-p->ch[]->size-p->ex);
}
inline void Del(ScapeGoat_Tree *&Root,int key)
{
del(Root,Rank_Min(Root,key)+);
if(Root->size<Root->cover*a)rebuild(Root);
}
int n,m,pos[MAXN];
void build(Tree *p)
{
p->mid=(p->l+p->r)>>;
if(p->l==p->r)return;
p->ch[]=new Tree;
p->ch[]->l=p->l;
p->ch[]->r=p->mid;
p->ch[]=new Tree;
p->ch[]->l=p->mid+;
p->ch[]->r=p->r;
build(p->ch[]);
build(p->ch[]);
}
void Ins(Tree *p,int key,int aim)
{
Insert(p->root,key);
if(p->l==p->r)return;
Ins(p->ch[p->mid<aim],key,aim);
}
int query_Max(int l,int r,int key,Tree *p)
{
if(l<=p->l&&p->r<=r)
return Rank_Max(p->root,key);
int tmp=;
if(l<=p->mid)tmp+=query_Max(l,r,key,p->ch[]);
if(p->mid<r)tmp+=query_Max(l,r,key,p->ch[]);
return tmp;
}
int query_Min(int l,int r,int key,Tree *p)
{
if(l<=p->l&&p->r<=r)
return Rank_Min(p->root,key);
int tmp=;
if(l<=p->mid)tmp+=query_Min(l,r,key,p->ch[]);
if(p->mid<r)tmp+=query_Min(l,r,key,p->ch[]);
return tmp;
}
void Delete(Tree *p,int key,int aim)
{
Del(p->root,key);
if(p->l==p->r)return;
Delete(p->ch[p->mid<aim],key,aim);
}
int main()
{
//freopen("nt2011_queue.in","r",stdin);
//freopen("nt2011_queue.out","w",stdout);
Init();
scanf("%d",&n);
root=new Tree;
root->l=;
root->r=n;
build(root);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
pos[i]=x;
Ins(root,x,i);
if(i!=)ans+=query_Max(,i-,x,root);
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
printf("%lld\n",ans);
int x,y;
scanf("%d%d",&x,&y);
if(x!=) ans-=query_Max(,x-,pos[x],root);
if(x!=n) ans-=query_Min(x+,n,pos[x],root);
Delete(root,pos[x],x);
if(y!=) ans-=query_Max(,y-,pos[y],root);
if(y!=n) ans-=query_Min(y+,n,pos[y],root);
Delete(root,pos[y],y);
swap(pos[x],pos[y]);
Ins(root,pos[x],x);
if(x!=) ans+=query_Max(,x-,pos[x],root);
if(x!=n) ans+=query_Min(x+,n,pos[x],root);
Ins(root,pos[y],y);
if(y!=) ans+=query_Max(,y-,pos[y],root);
if(y!=n) ans+=query_Min(y+,n,pos[y],root);
}
printf("%lld\n",ans);
return ;
}

【bzoj2141】排队 [国家集训队2011]排队(魏铭) 树套树 线段树套替罪羊树的更多相关文章

  1. 【bzoj2141】排队 [国家集训队2011]排队(树套树)

    题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...

  2. COGS1871 [国家集训队2011]排队(魏铭)

    bzoj:http://www.lydsy.com/JudgeOnline/problem.php?id=2141 cogs:http://cogs.pro:8080/cogs/problem/pro ...

  3. AC日记——[国家集训队2011]旅游(宋方睿) cogs 1867

    [国家集训队2011]旅游(宋方睿) 思路: 树链剖分,边权转点权: 线段树维护三个东西,sum,max,min: 当一个区间变成相反数时,sum=-sum,max=-min,min=-max: 来, ...

  4. cogs 1901. [国家集训队2011]数颜色

    Cogs 1901. [国家集训队2011]数颜色 ★★★   输入文件:nt2011_color.in   输出文件:nt2011_color.out   简单对比时间限制:0.6 s   内存限制 ...

  5. BZOJ 2150 cogs 1861 [国家集训队2011]部落战争

    题目描述 lanzerb的部落在A国的上部,他们不满天寒地冻的环境,于是准备向A国的下部征战来获得更大的领土. A国是一个M*N的矩阵,其中某些地方是城镇,某些地方是高山深涧无人居住.lanzerb把 ...

  6. happiness[国家集训队2011(吴确)]

    [试题来源] 2011中国国家集训队命题答辩 [问题描述] 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科 ...

  7. COGS1882 [国家集训队2011]单选错位

    ★   输入文件:nt2011_exp.in   输出文件:nt2011_exp.out   简单对比时间限制:1 s   内存限制:512 MB [试题来源] 2011中国国家集训队命题答辩 [问题 ...

  8. 1893. [国家集训队2011]等差子序列(bitset)

    ★★   输入文件:nt2011_sequence.in   输出文件:nt2011_sequence.out   简单对比时间限制:0.3 s   内存限制:512 MB [试题来源] 2011中国 ...

  9. bzoj2144 【国家集训队2011】跳跳棋

    Description 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他 ...

随机推荐

  1. eclipse 右键没有Build Path

    如果Project Explorer右键没有build pathWindow ->show view 选择package explorer 参考https://blog.csdn.net/cod ...

  2. Python进阶-函数默认参数

    Python进阶-函数默认参数 写在前面 如非特别说明,下文均基于Python3 一.默认参数 python为了简化函数的调用,提供了默认参数机制: def pow(x, n = 2): r = 1 ...

  3. 嵌入式框架Zorb Framework搭建六:定时器的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  4. linux实验-基本指令1

    1.root帐号登录,查看/tmp目录,如果/tmp目录下没有子目录myshare,则建立该目录. 2.创建帐号testuser. 3.把myshare目录及其目录下的所有文件和子目录的拥有者该为te ...

  5. MySQL分区的限制(最多有多少个分区)

    MySQL分区的限制 •   只能对数据表的整型列进行分区,或者数据列可以通过分区函数转化成整型列 •   最大分区数目不能超过1024 •   如果含有唯一索引或者主键,则分区列必须包含在所有的唯一 ...

  6. Hibernate-ORM:11.Hibernate中的关联查询

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述Hibernate中的关联查询,及其级联(cascade)操作,以及指定哪一方维护关联关系的(i ...

  7. 【jQuery】 效果

    [jQuery] 效果 资料 http://www.w3school.com.cn/jquery/jquery_ref_effects.asp 1. 显示隐藏 hide(); 隐藏 show(): 显 ...

  8. 去掉google play专为手机设计标识

    google play上的应用默认都会有个“专为手机设计”的标识 有时应用明明已经针对平板作了优化,但为什么这个标识还在呢,如何去掉这个标识呢,其实只需要两个步骤就好了: 1. 标记为支持高分辨率 & ...

  9. 程序员编程利器:20款最好的免费的IDEs和编辑器

    程序员编程利器:20款最好的免费的IDEs和编辑器 还没转眼明年可就大年三十了,忙的可真是晕头转了个向,看着亲朋好友们那让人欣羡的小肚腩,不禁感慨,岁月是一把猪饲料,绿了芭蕉,肥了那杨柳小蛮腰,可怜我 ...

  10. Python 3基础教程21-列表和元组

    本文介绍列表也元组,先来看看他们的定义. # 元组和列表 # 元组的定义 x = 5,6,2,6 # 或者这样写 x = (5,6,2,6) # 列表定义 y = [5,6,2,6] # 元组的使用, ...