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

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #define MAXN 100010
  5. using namespace std;
  6. typedef long long LL;
  7. typedef double D;
  8. const D a=0.756;
  9. LL ans;
  10. struct ScapeGoat_Tree
  11. {
  12. ScapeGoat_Tree *ch[];
  13. int key,size,cover,ex;
  14. bool bad()
  15. {
  16. return cover*a+<ch[]->cover||cover*a+<ch[]->cover;
  17. }
  18. void pushup()
  19. {
  20. size=ch[]->size+ch[]->size+ex;
  21. cover=ch[]->cover+ch[]->cover+;
  22. }
  23. }*null,*stack[(MAXN<<)+],pool[(MAXN<<)+],*lst[(MAXN<<)+];
  24. int top,len;
  25. inline void Init()
  26. {
  27. null=pool;
  28. null->ch[]=null->ch[]=null;
  29. null->key=null->size=null->cover=null->ex=;
  30. for(int i=;i<(MAXN<<);i++)stack[++top]=pool+i;
  31. }
  32. inline ScapeGoat_Tree *New(int key)
  33. {
  34. ScapeGoat_Tree *p=stack[top--];
  35. p->ch[]=p->ch[]=null;
  36. p->key=key;
  37. p->size=p->ex=p->cover=;
  38. return p;
  39. }
  40. struct Tree
  41. {
  42. Tree *ch[];
  43. int mid,l,r;
  44. ScapeGoat_Tree *root;
  45. Tree(){ch[]=ch[]=NULL;mid=l=r=;root=null;}
  46. void* operator new(size_t size);
  47. }*root,*C,*mempool;
  48. void* Tree :: operator new(size_t size)
  49. {
  50. if(C==mempool)
  51. {
  52. C=new Tree[(<<)+];
  53. mempool=C+(<<)+;
  54. }
  55. C->root=null;
  56. return C++;
  57. }
  58. void travel(ScapeGoat_Tree *p)
  59. {
  60. if(p==null)return;
  61. travel(p->ch[]);
  62. if(p->ex)lst[++len]=p;
  63. else stack[++top]=p;
  64. travel(p->ch[]);
  65. }
  66. ScapeGoat_Tree *divide(int l,int r)
  67. {
  68. if(l>r)return null;
  69. int mid=(l+r)>>;
  70. lst[mid]->ch[]=divide(l,mid-);
  71. lst[mid]->ch[]=divide(mid+,r);
  72. lst[mid]->pushup();
  73. return lst[mid];
  74. }
  75. inline void rebuild(ScapeGoat_Tree *&p)
  76. {
  77. len=;
  78. travel(p);
  79. p=divide(,len);
  80. }
  81. ScapeGoat_Tree **insert(ScapeGoat_Tree *&p,int key)
  82. {
  83. if(p==null )
  84. {
  85. p=New(key);
  86. return &null;
  87. }
  88. p->size++;
  89. p->cover++;
  90. ScapeGoat_Tree **ret=insert(p->ch[p->key<=key],key);
  91. if(p->bad())ret=&p;
  92. return ret;
  93. }
  94. inline void Insert(ScapeGoat_Tree *&Root,int key)
  95. {
  96. ScapeGoat_Tree **p=insert(Root,key);
  97. if(*p!=null )rebuild(*p);
  98. }
  99. inline int Rank_Max(ScapeGoat_Tree *Root,int key)
  100. {
  101. ScapeGoat_Tree *now=Root;
  102. int ret=;
  103. while(now!=null )
  104. if(now->key<=key)
  105. now=now->ch[];
  106. else
  107. ret+=now->ch[]->size+now->ex,now=now->ch[];
  108. return ret;
  109. }
  110. inline int Rank_Min(ScapeGoat_Tree *Root,int key)
  111. {
  112. ScapeGoat_Tree *now=Root;
  113. int ret=;
  114. while(now!=null )
  115. if(now->key>=key)
  116. now=now->ch[];
  117. else
  118. ret+=now->ch[]->size+now->ex,now=now->ch[];
  119. return ret;
  120. }
  121. void del(ScapeGoat_Tree *p,int k)
  122. {
  123. p->size--;
  124. if(p->ex&&p->ch[]->size+==k)
  125. {
  126. p->ex=;
  127. return;
  128. }
  129. if(p->ch[]->size>=k) del(p->ch[],k);
  130. else del(p->ch[],k-p->ch[]->size-p->ex);
  131. }
  132. inline void Del(ScapeGoat_Tree *&Root,int key)
  133. {
  134. del(Root,Rank_Min(Root,key)+);
  135. if(Root->size<Root->cover*a)rebuild(Root);
  136. }
  137. int n,m,pos[MAXN];
  138. void build(Tree *p)
  139. {
  140. p->mid=(p->l+p->r)>>;
  141. if(p->l==p->r)return;
  142. p->ch[]=new Tree;
  143. p->ch[]->l=p->l;
  144. p->ch[]->r=p->mid;
  145. p->ch[]=new Tree;
  146. p->ch[]->l=p->mid+;
  147. p->ch[]->r=p->r;
  148. build(p->ch[]);
  149. build(p->ch[]);
  150. }
  151. void Ins(Tree *p,int key,int aim)
  152. {
  153. Insert(p->root,key);
  154. if(p->l==p->r)return;
  155. Ins(p->ch[p->mid<aim],key,aim);
  156. }
  157. int query_Max(int l,int r,int key,Tree *p)
  158. {
  159. if(l<=p->l&&p->r<=r)
  160. return Rank_Max(p->root,key);
  161. int tmp=;
  162. if(l<=p->mid)tmp+=query_Max(l,r,key,p->ch[]);
  163. if(p->mid<r)tmp+=query_Max(l,r,key,p->ch[]);
  164. return tmp;
  165. }
  166. int query_Min(int l,int r,int key,Tree *p)
  167. {
  168. if(l<=p->l&&p->r<=r)
  169. return Rank_Min(p->root,key);
  170. int tmp=;
  171. if(l<=p->mid)tmp+=query_Min(l,r,key,p->ch[]);
  172. if(p->mid<r)tmp+=query_Min(l,r,key,p->ch[]);
  173. return tmp;
  174. }
  175. void Delete(Tree *p,int key,int aim)
  176. {
  177. Del(p->root,key);
  178. if(p->l==p->r)return;
  179. Delete(p->ch[p->mid<aim],key,aim);
  180. }
  181. int main()
  182. {
  183. //freopen("nt2011_queue.in","r",stdin);
  184. //freopen("nt2011_queue.out","w",stdout);
  185. Init();
  186. scanf("%d",&n);
  187. root=new Tree;
  188. root->l=;
  189. root->r=n;
  190. build(root);
  191. for(int i=;i<=n;i++)
  192. {
  193. int x;
  194. scanf("%d",&x);
  195. pos[i]=x;
  196. Ins(root,x,i);
  197. if(i!=)ans+=query_Max(,i-,x,root);
  198. }
  199. scanf("%d",&m);
  200. for(int i=;i<=m;i++)
  201. {
  202. printf("%lld\n",ans);
  203. int x,y;
  204. scanf("%d%d",&x,&y);
  205. if(x!=) ans-=query_Max(,x-,pos[x],root);
  206. if(x!=n) ans-=query_Min(x+,n,pos[x],root);
  207. Delete(root,pos[x],x);
  208. if(y!=) ans-=query_Max(,y-,pos[y],root);
  209. if(y!=n) ans-=query_Min(y+,n,pos[y],root);
  210. Delete(root,pos[y],y);
  211. swap(pos[x],pos[y]);
  212. Ins(root,pos[x],x);
  213. if(x!=) ans+=query_Max(,x-,pos[x],root);
  214. if(x!=n) ans+=query_Min(x+,n,pos[x],root);
  215. Ins(root,pos[y],y);
  216. if(y!=) ans+=query_Max(,y-,pos[y],root);
  217. if(y!=n) ans+=query_Min(y+,n,pos[y],root);
  218. }
  219. printf("%lld\n",ans);
  220. return ;
  221. }

【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. scala成长之路(6)函数入门

    众所周知,scala作为一门极客型的函数式编程语言,支持的特性包括: 函数拥有“一等公民”身份: 支持匿名函数(函数字面量) 支持高阶函数 支持闭包 部分应用函数 柯里化 首先需要指出,在scala中 ...

  2. kafka概述

    kafka概述 Apache Kafka是一个开源 消息 系统,由Scala写成.是由Apache软件基金会开发的一个开源消息系统项目. Kafka最初是由LinkedIn开发,并于2011年初开源. ...

  3. ruby中将数字转化为字符串格式时差

        工作中有时候会碰到需要把数值展示成比较直观的时间差格式,divmod方法很适合做这个操作.   divmod #输出商和余数的数组    60.divmod(50) #=> [1, 10 ...

  4. Leecode刷题之旅-C语言/python-26.删除数组中的重复项

    /* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remo ...

  5. 使用source命令解决mysql导入乱码问题

    设定编码格式:mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql

  6. 标记编码报错ValueError: bad input shape ()

    <Python机器学习经典实例>2.9小节中,想自己动手实践汽车特征评估质量,所以需要对数据进行预处理,其中代码有把字符串标记编码为对应的数字,如下代码 input_data = ['vh ...

  7. 【jQuery】 Ajax

    [jQuery] Ajax $.ajax({ type: "Post", // 发包方式 cache: false, // 是否缓存 contentType: "appl ...

  8. Python初步

    准备在工作之余看看Python的东西 收录一些资料 Python初学者(零基础学习Python.Python入门)常见问题:书籍推荐.资料.社区 http://blog.csdn.net/xiaowa ...

  9. Leetcode代码补全——二叉树

    在刷leetcode的过程中发现,在原网页输入答案是不需要自己构筑树和链表的,虽然便于直接思考算法,但是久而久之类似过于依赖编辑器,反而不知道如何创建树和链表,因此总结了该网页省略的部分,以其中题为例 ...

  10. 第二十一篇 json,picklz,xml模块

    Json模块 Json模块比较简单,仅有四个方法dumps()和loads()方法,dump()和load()方法,但是却非常的常用,实用性极强. 如果要在不同的编程语言之间传递对象,就必须把对象序列 ...