zoj 2112 主席树+树状数组求动态第k大

Dynamic Rankings

Time Limit: 10 Seconds      Memory Limit: 32768 KB

The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They have developed a more powerful system such that for N numbers a[1], a[2], ..., a[N], you can ask it like: what is the k-th smallest number of a[i], a[i+1], ..., a[j]? (For some i<=j, 0<k<=j+1-i that you have given to it). More powerful, you can even change the value of some a[i], and continue to query, all the same.

Your task is to write a program for this computer, which

- Reads N numbers from the input (1 <= N <= 50,000)

- Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], ..., a[j] and change some a[i] to t.


Input

The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.

The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format

Q i j k or
C i t

It represents to query the k-th number of a[i], a[i+1], ..., a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.

There're NO breakline between two continuous test cases.


Output

For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],..., a[j])

There're NO breakline between two continuous test cases.


Sample Input

2
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3


Sample Output

3
6
3
6

37332592014-08-12 20:40:21Accepted 2112C++14032360houzi

差点超内存;

参考;http://www.2cto.com/kf/201406/309987.html


/*
如果我们更新了arr[i]那么它将会影响T[i]~T[n]。难道我们要一个一个改么。
这样改显然时间上不允许。但是你会发现它的改变对T[i]~T[n]的影响是一样的。
如果我们把影响(左子树增加多少值,右子树增加多少值)记录下来。那我们就
只需用原始值减去变化值就好了。变化值我们可以用树状数组来记录。我们把变
化当作一个值,那么就成了单点更新区间求和问题了。只是这里不是一个值而是
一个线段树而已,但是我们可以类似的处理。也就是每个树状数组的结点都是一
棵线段树。那么这个问题就圆满解决了。
分析下时空复杂度。首先建一棵空树m*log2(m)。m为hash后值的个数。
然后建n个树.n*log2(m)。然后q次查询操作.2*q*log2(m).
所以总时间复杂度为:O((m+n+2*q)*log2(m))。空间复杂度。4*m+n*lon2(m)。*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define N 50100
#define M 2000000
using namespace std;
int lson[M],rson[M],c[M],tmp[M],s[N],t[N],use[N],a[N],num,n,m;
struct node
{
  int l,r,id,k;
}qu[10100];
int build(int x,int y)//建空树
{
   int root=num++;
   c[root]=0;
   if(x!=y)
   {
     int mid=(x+y)>>1;
     lson[root]=build(x,mid);
     rson[root]=build(mid+1,y);
   }
   return root;
}
int lowbit(int x)
{
  return x&(-x);
}
int hash(int x)
{
  return lower_bound(tmp,tmp+m,x)-tmp;
}
int insert(int root,int k,int val)
{
   int newroot,tp,l,r;
    newroot=num++;tp=num-1;
    c[newroot]=c[root]+val;
    l=0; r=m-1;
    while(l<r)
    {//非递归插入。节省内存
      int mid=(l+r)>>1;
      if(k<=mid)
      {
         lson[newroot]=num++; rson[newroot]=rson[root];//共享结点
         newroot=lson[newroot];  root=lson[root];
         r=mid;
      }
      else
      {
         rson[newroot]=num++;  lson[newroot]=lson[root];
         newroot=rson[newroot];  root=rson[root];
         l=mid+1;
      }
      c[newroot]=c[root]+val;
    }
    return tp;
}
int sum(int x)
{
   int sm=0;
   while(x)
   {
      sm+=c[lson[use[x]]];
      x-=lowbit(x);
   }
   return sm;
}
int find(int x,int y,int k)
{
   int i;
   int left=t[x-1];
   int right=t[y];
   for(i=x-1;i;i-=lowbit(i))
     use[i]=s[i];
    for(i=y;i;i-=lowbit(i))
      use[i]=s[i];
    int l=0,r=m-1;
    while(l<r)
    {
       int mid=(l+r)>>1;
       int pos=sum(y)-sum(x-1)+c[lson[right]]-c[lson[left]];
       if(pos>=k)//初始值加改变值
       {
            r=mid;//计算对应子树改变
            for(i=x-1;i;i-=lowbit(i))
              use[i]=lson[use[i]];
            for(i=y;i;i-=lowbit(i))
               use[i]=lson[use[i]];
            left=lson[left];
            right=lson[right];
       }
       else
       {
         l=mid+1;
         k-=pos;
         for(i=x-1;i;i-=lowbit(i))
            use[i]=rson[use[i]];
          for(i=y;i;i-=lowbit(i))
            use[i]=rson[use[i]];
          left=rson[left];
          right=rson[right];
       }
    }
    return l;
}
void update(int x,int pos,int val)
{
   while(x<=n)//树状数组更新
   {//s为树状数组结点。当然也是线段树的根啦。
     s[x]=insert(s[x],pos,val);
     x+=lowbit(x);
   }
}
int main()
{
  int tt,q,k,i;
  char str[10];
 // freopen("a.txt","r",stdin);
  //freopen("b.txt","w",stdout);
  scanf("%d",&tt);
  while(tt--)
  {
    scanf("%d%d",&n,&q);
    k=0;
    for(i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
      tmp[k++]=a[i];
    }
    for(i=1;i<=q;i++)
    {
       scanf("%s",str);
       if(str[0]=='Q')
       {
         scanf("%d%d%d",&qu[i].l,&qu[i].r,&qu[i].k);
         qu[i].id=1;
       }
       else
       {
        scanf("%d%d",&qu[i].l,&qu[i].r);
        qu[i].id=0;  tmp[k++]=qu[i].r;
       }
    }
    sort(tmp,tmp+k);
    m=unique(tmp,tmp+k)-tmp;  num=0;
    t[0]=build(0,m-1);
    for(i=1;i<=n;i++)
       t[i]=insert(t[i-1],hash(a[i]),1);
    memset(s,0,sizeof(s));
    for(i=1;i<=q;i++)
    {
        if(qu[i].id==1)
           printf("%d\n",tmp[find(qu[i].l,qu[i].r,qu[i].k)]);
        else
        {
           update(qu[i].l,hash(a[qu[i].l]),-1);
           update(qu[i].l,hash(qu[i].r),1);
           a[qu[i].l]=qu[i].r;//开始忘了改这里无限wa啊。。。
        }
    }
  }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值