zoukankan      html  css  js  c++  java
  • [ZJOI2007]报表统计

    题目描述

    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写了一个程序,使得程序可以自动完成这些操作,但是他发现对于一些大的报表他的程序运行得很慢,你能帮助他改进程序么?

    输入输出格式

    输入格式:

    第一行包含两个整数N,M,分别表示原数列的长度以及操作的次数。

    第二行为N个整数,为初始序列。

    接下来的M行每行一个操作,即“INSERT i k”,“MIN_GAP”,“MIN_SORT_GAP”中的一种(无多余空格或者空行)。

    输出格式:

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

    输入输出样例

    输入样例#1:
    3 5
    5 3 1
    INSERT 2 9
    MIN_SORT_GAP
    INSERT 2 6
    MIN_GAP
    MIN_SORT_GAP
    
    输出样例#1:
    2
    2
    1
    

    说明

    对于30%的数据,N ≤ 1000 , M ≤ 5000

    对于100%的数据,N , M ≤500000

    对于所有的数据,序列内的整数不超过5*108。

    时限2s

    操作一:没必要搞一棵splay。我们只用开一个lq[500001][2]的数组记录这个点的前面和后面就行。

    操作二:带修改的小根堆

    操作三:把所有数扔到splay里,按大小为关键字,再插入的时候取个max的差值就好了。

    MIN_SORT_GAP用set维护,插入前lower_bound找最接近的值,更新最小值,然后insert

    MIN_GAP用堆维护,在a,b之间插入c时,把abs(a-b)从堆中删除,然后插入abs(a-c)和abs(b-c)

    (堆不支持删除,所以用了两个堆来达到删除指定数值,具体见代码)

    另一种写法:二叉堆换左偏树,Splay换Treap。

    这题能很好地利用左偏树,首先利用队列两两合并O(n)建堆,在删除时可不必像二叉堆那样建两个堆懒惰删除,只需开一个数组inx[x],记录原始数列中第x个元素在插入后管辖的最后一个元素与第x+1个元素的差值绝对值在左偏树中的序号,在插入新元素时利用左偏树的删除任意节点来删除inx[x]并更新即可。

    不过效率并没有显著加快。

    这是第一种写法代码,由于用stl,所以效率不高,但原题4s时限能过

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<set>
      6 using namespace std;
      7 int ans=2e9;
      8 multiset<int> s;
      9 int heap1[1000001],heap2[1000001],sum1,sum2,n,m,a[500001],l[500001][3];
     10 //multiset<int>::iterator it1;
     11 //multiset<int>::iterator it2;
     12 int it1,it2;
     13 void push1(int x)
     14 {int pos,p;
     15     sum1++;
     16     heap1[sum1]=x;
     17     pos=sum1;
     18     while (pos>1&&heap1[pos]<heap1[pos/2])
     19     {p=heap1[pos];heap1[pos]=heap1[pos/2];heap1[pos/2]=p;
     20      pos/=2;
     21     }
     22 }
     23 void push2(int x)
     24 {int pos,p;
     25     sum2++;
     26     heap2[sum2]=x;
     27     pos=sum2;
     28     while (pos>1&&heap2[pos]<heap2[pos/2])
     29     {
     30      p=heap2[pos];heap2[pos]=heap2[pos/2];heap2[pos/2]=p;
     31      pos/=2;
     32     }
     33 }
     34 void pop1()
     35 {int p;
     36    int pos,next;
     37     heap1[1]=heap1[sum1];
     38     heap1[1]=heap1[sum1];
     39     sum1--;
     40     pos=1;
     41      while (pos*2<=sum1)
     42      {
     43         next=pos*2;
     44         if (next<sum1&&heap1[next+1]<heap1[next]) next++;
     45         if (heap1[pos]<=heap1[next]) break;
     46           p=heap1[pos];heap1[pos]=heap1[next];heap1[next]=p;
     47           pos=next;
     48      }   
     49 }
     50 void pop2()
     51 {int p;
     52    int pos,next;
     53     heap2[1]=heap2[sum2];
     54     sum2--;
     55     pos=1;
     56      while (pos*2<=sum2)
     57      {
     58         next=pos*2;
     59         if (next<sum2&&heap2[next+1]<heap2[next]) next++;
     60         if (heap2[pos]<=heap2[next]) break;
     61           p=heap2[pos];heap2[pos]=heap2[next];heap2[next]=p;
     62           pos=next;
     63      }   
     64 }
     65 int read(){
     66     int x=0,f=1;char ch=getchar();
     67     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     68     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     69     return x*f;
     70 }
     71 int abs(int x)
     72 {
     73     if (x>0) return x;
     74     else return -x;
     75 }
     76 int main()
     77 {int i,j,p,k;
     78     cin>>n>>m;
     79     s.insert(2e9);
     80     s.insert(-2e9);
     81      for (i=1;i<=n;i++)
     82      {
     83          a[i]=read();
     84         l[i][1]=l[i][2]=a[i];
     85         if (i>1)
     86         push1(abs(l[i][1]-l[i-1][2]));
     87         //if (s.begin()!=s.end())
     88          {
     89          it1=*s.lower_bound(a[i]);
     90          it2=*--s.lower_bound(a[i]);
     91          //cout<<it1<<':'<<it2<<endl;
     92          if (ans>abs(it1-a[i]))
     93          ans=abs(it1-a[i]);
     94          if (ans>abs(a[i]-it2))
     95          ans=abs(a[i]-it2);
     96          }
     97          //cout<<'a'<<a[i]<<endl;
     98          s.insert(a[i]);
     99      }
    100      char st[101];
    101      for (i=1;i<=m;i++)
    102      {
    103          scanf("%s",st);
    104          //cout<<st<<endl;
    105          if (st[0]=='I')
    106          {
    107              p=read();k=read();
    108              if (p<n)
    109              {
    110                  push2(abs(l[p][2]-l[p+1][1]));
    111                  push1(abs(k-l[p+1][1]));
    112              }
    113              push1(abs(k-l[p][2]));
    114              l[p][2]=k;
    115          it1=*s.lower_bound(k);
    116          it2=*--s.lower_bound(k);
    117          //cout<<it1<<';'<<it2<<endl;
    118          if (ans>abs(it1-k))
    119          ans=abs(it1-k);
    120          if (ans>abs(k-it2))
    121          ans=abs(k-it2);
    122              s.insert(k);
    123          }
    124          else if (st[4]=='G')
    125          {
    126              while (heap1[1]==heap2[1])
    127              {
    128                  //cout<<'h'<<heap1[1]<<endl;
    129                  pop1();pop2();
    130              }
    131              printf("%d
    ",heap1[1]);
    132          }
    133          else if (st[4]=='S')
    134          {
    135              printf("%d
    ",ans);
    136          }
    137      }
    138 }
  • 相关阅读:
    .net AJAX运行错误 未能加载文件或程序集 "System.Web....”的解决办法
    mysql免安装版使用方法
    XP下IIS+php 5.2.4配置
    distinct order by
    手机操作系统介绍
    .net自带 SQL Server 2005 Express使用
    统计字符串数组中每个字符串所出现的次数
    juqery 学习之三 选择器<层级><基本>
    juqery 学习之三 选择器<简单><内容>
    juqery 学习之三 选择器<可见性><元素属性>
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7125329.html
Copyright © 2011-2022 走看看