http://www.lydsy.com/JudgeOnline/problem.php?id=1058

当复习一下splay。。。。

做法很简单。。。。。

观察得知每一次插入一个点只需要维护前后的绝对值

观察得知min_sort_gap直接二分已经排好序的数组找到前驱后继更新即可(这里是个贪心,显然成立)

观察得知这是区间操作,所以我用了splayQAQ

注意些细节即可。

好慢啊,,10000ms。。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <set>
  9. #include <map>
  10. using namespace std;
  11. typedef long long ll;
  12. #define pii pair<int, int>
  13. #define mkpii make_pair<int, int>
  14. #define pdi pair<double, int>
  15. #define mkpdi make_pair<double, int>
  16. #define pli pair<ll, int>
  17. #define mkpli make_pair<ll, int>
  18. #define rep(i, n) for(int i=0; i<(n); ++i)
  19. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  20. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  21. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  22. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  23. #define CC(i,a) memset(i,a,sizeof(i))
  24. #define read(a) a=getint()
  25. #define print(a) printf("%d", a)
  26. #define dbg(x) cout << (#x) << " = " << (x) << endl
  27. #define error(x) (!(x)?puts("error"):0)
  28. #define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
  29. #define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
  30. inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
  31. inline const int max(const int &a, const int &b) { return a>b?a:b; }
  32. inline const int min(const int &a, const int &b) { return a<b?a:b; }
  33.  
  34. const int N=500005, oo=~0u>>2;
  35. set<int> s;
  36. int b[N], n, m, ans2=oo, cnt;
  37. struct node *null;
  38. struct node {
  39. node *ch[2], *fa;
  40. int mn, w, s, v;
  41. node() { ch[0]=ch[1]=fa=null; mn=oo; w=oo; s=1; v=oo; }
  42. void pushup() { mn=w; mn=min(mn, min(ch[0]->mn, ch[1]->mn)); s=1+ch[0]->s+ch[1]->s; }
  43. bool d() { return fa->ch[1]==this; }
  44. void setc(node *c, bool d) { c->fa=this; ch[d]=c; }
  45. }*root, *arr[N];
  46. void rot(node *x) {
  47. node *fa=x->fa; bool d=x->d();
  48. fa->fa->setc(x, fa->d());
  49. fa->setc(x->ch[!d], d);
  50. x->setc(fa, !d);
  51. fa->pushup();
  52. if(fa==root) root=x;
  53. }
  54. void splay(node *x, node *fa=null) {
  55. while(x->fa!=fa)
  56. if(x->fa->fa==fa) rot(x);
  57. else x->d()==x->fa->d()?(rot(x->fa), rot(x)):(rot(x), rot(x));
  58. x->pushup();
  59. }
  60. node *sel(node *x, int k) {
  61. if(x==null) return null;
  62. int s=x->ch[0]->s;
  63. if(k==s) return x;
  64. if(k>s) return sel(x->ch[1], k-s-1); else return sel(x->ch[0], k);
  65. }
  66. node *getpos(int pos) {
  67. splay(sel(root, pos));
  68. splay(sel(root, pos+1), root);
  69. return root->ch[1];
  70. }
  71. void fix(int pos, int v, const bool flag) {
  72. node *fa=getpos(pos);
  73. node *c=new node;
  74. c->v=v;
  75. if(fa->ch[1]!=null) c->w=abs(v-fa->v);
  76. fa->setc(c, 0);
  77. if(root->ch[0]!=null) root->w=abs(root->v-v);
  78. if(flag) { arr[++cnt]=c; }
  79. splay(c);
  80. }
  81. void work1() { printf("%d\n", root->mn); }
  82. void work2() { printf("%d\n", ans2); }
  83. void maintain(const int &b) {
  84. set<int>::iterator it=s.lower_bound(b);
  85. ans2=min(ans2, abs(*it-b)); --it;
  86. ans2=min(ans2, abs(*it-b));
  87. s.insert(b);
  88. }
  89. void insert() {
  90. int x=getint(), y=getint();
  91. maintain(y);
  92. node *it=arr[x];
  93. splay(it);
  94. fix(it->ch[0]->s+b[x], y, 0);
  95. ++b[x];
  96. }
  97. void build() {
  98. int t;
  99. for1(i, 1, n) {
  100. read(t);
  101. maintain(t);
  102. fix(i-1, t, 1);
  103. }
  104. }
  105. void init() {
  106. s.insert(oo);
  107. s.insert(-oo);
  108. null=new node;
  109. null->ch[0]=null->ch[1]=null->fa=null; null->s=0;
  110. root=new node;
  111. node *c=new node;
  112. root->setc(c, 1);
  113. }
  114. void pri(node *x=root) {
  115. if(x==null) return;
  116. pri(x->ch[0]);
  117. printf("%d ", x->v);
  118. pri(x->ch[1]);
  119. }
  120. int main() {
  121. read(n); read(m);
  122. init();
  123. build();
  124. char od[20];
  125. for1(i, 1, m) {
  126. scanf("%s", od+1);
  127. if(od[5]=='R') insert();
  128. else if(od[5]=='G') work1();
  129. else if(od[5]=='S') work2();
  130. }
  131. //pri();
  132. return 0;
  133. }

  


Description

小Q的妈妈是一个出纳,经常需要做一些统计报表的工作。今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一。经过仔细观察,小Q发现统计一张报表实际上是维护一个可能为负数的整数数列,并且进行一些查询操作。在最开始的时候,有一个长度为N的整数序列,并且有以下三种操作: INSERT i k 在原数列的第i个元素后面添加一个新元素k; 如果原数列的第i个元素已经添加了若干元素,则添加在这些元素的最后(见下面的例子) MIN_GAP 查询相邻两个元素的之间差值(绝对值)的最小值 MIN_SORT_GAP 查询所有元素中最接近的两个元素的差值(绝对值) 例如一开始的序列为 5 3 1 执行操作INSERT 2 9将得到: 5 3 9 1 此时MIN_GAP为2,MIN_SORT_GAP为2。 再执行操作INSERT 2 6将得到: 5 3 9 6 1 注意这个时候原序列的第2个元素后面已经添加了一个9,此时添加的6应加在9的后面。这个时候MIN_GAP为2,MIN_SORT_GAP为1。于是小Q写了一个程序,使得程序可以自动完成这些操作,但是他发现对于一些大的报表他的程序运行得很慢,你能帮助他改进程序么?

Input

第一行包含两个整数N,M,分别表示原数列的长度以及操作的次数。第二行为N个整数,为初始序列。接下来的M行每行一个操作,即“INSERT i k”,“MIN_GAP”,“MIN_SORT_GAP”中的一种(无多余空格或者空行)。

Output

对于每一个“MIN_GAP”和“MIN_SORT_GAP”命令,输出一行答案即可。

Sample Input

3 5
5 3 1
INSERT 2 9
MIN_SORT_GAP
INSERT 2 6
MIN_GAP
MIN_SORT_GAP

Sample Output

2
2
1

HINT

对于100%的数据,N , M ≤500000 对于所有的数据,序列内的整数不超过5*10^8。

Source

【BZOJ】1058: [ZJOI2007]报表统计(splay+set)的更多相关文章

  1. BZOJ 1058: [ZJOI2007]报表统计( 链表 + set )

    这种题用数据结构怎么写都能AC吧...按1~N弄个链表然后每次插入时就更新答案, 用set维护就可以了... --------------------------------------------- ...

  2. bzoj 1058: [ZJOI2007]报表统计 (Treap)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1058 题面; 1058: [ZJOI2007]报表统计 Time Limit: 15 Sec ...

  3. [BZOJ 1058] [ZJOI2007] 报表统计 【平衡树】

    题目链接:BZOJ - 1058 题目分析 这道题看似是需要在序列中插入一些数字,但其实询问的内容只与相邻的元素有关. 那么我们只要对每个位置维护两个数 Ai, Bi, Ai 就是初始序列中 i 这个 ...

  4. bzoj 1058: [ZJOI2007]报表统计

    Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工 作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个 ...

  5. BZOJ 1058: [ZJOI2007]报表统计 multiset + 卡常

    Code: #include<bits/stdc++.h> #define maxn 600000 #define inf 1000000000 using namespace std; ...

  6. bzoj 1058 [ZJOI2007]报表统计(set)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1058 [题意] 一个序列,提供插入,查询相邻最小差值,查询任意最小差值的操作. [思路 ...

  7. bzoj 1058: [ZJOI2007]报表统计【set】

    我想写FHQtreap的!是set自己跑进代码的!因为太好写了 是有点慢--洛谷上不吸氧会T一个点 就是,用一个set p维护所有点值,ans维护MIN_SORT_GAP的答案,每次insert一个点 ...

  8. bzoj P1058 [ZJOI2007]报表统计——solution

    1058: [ZJOI2007]报表统计 Time Limit: 15 Sec  Memory Limit: 162 MB Submit: 4099  Solved: 1390 [Submit][St ...

  9. 1058: [ZJOI2007]报表统计 - BZOJ

    Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个非 ...

随机推荐

  1. com.tongyan.tutelage:bdservice_v1

    3-21 10:14:20.833 2892-2892/? E/art: No implementation found for long com.baidu.platform.comjni.map. ...

  2. Servlet 过滤器 Filter

    过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类.javax.servlet.Filter 接口定义了三个方法: 下面是对所有编码过滤器 package filter ...

  3. ubuntu开启ftp服务

    首先再防火墙中开启21和20端口 iptables -A INPUT -p tcp --dport -j ACCEPT iptables -A INPUT -p tcp --dport -j ACCE ...

  4. APACHE + LDAP 的权限认证配置方法

    原文地址:http://www.chinaunix.net/jh/49/627646.html 一.前言 很多朋友希望利用 Apache 通过 LDAP 进行用户认证及权限管理.     通过多次试验 ...

  5. 【phpstorm】破解安装

    1.使用前修改C:\windows\system32\Driver\hosts文件,将“0.0.0.0 account.jetbrains.com”添加到hosts文件中. 2. 浏览器打开 http ...

  6. javascript 异常基本语法

    http://www.w3school.com.cn/js/js_onerror.asp try...catch 的作用是测试代码中的错误.   JavaScript - 捕获错误 当我们在网上冲浪时 ...

  7. PostgreSQL在线安装

    背景:CentOS 7 一.在线yum安装 yum -y install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7 ...

  8. python 火车票爬取代码

    1.根据搜索词下载百度图片: # -*- coding: utf-8 -*- """根据搜索词下载百度图片""" import re imp ...

  9. 为什么会找不到D层文件?

    近期两天在重装系统,今天好不easy把各种东西都装齐全了,再打开我的机房收费系统,就提演示样例如以下错误: 看到这个问题.我感觉非常熟,由于曾经也遇到过两次这个问题,都是改了下D层的编译路径.改到了U ...

  10. atitit.抽奖活动插件组件设计--结构设计and 抽奖流程建模

    atitit.抽奖活动插件组件设计--结构设计and 抽奖流程建模 1. 组件结构 1 2. startDraw 开始抽奖流程建模 1 3. 抽奖算法 2 作者:: 老哇的爪子 Attilax 艾龙, ...