zoukankan      html  css  js  c++  java
  • BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树

    题意概述:带修改求区间第k大。

    分析:

      我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN)。

      然而这里可以修改,主席树显然是凉了,但是注意到主席树的不带修改做法实际上是利用的差分的性质,即主席树本身实际上就是维护的一个前缀和一样的东西。想想普通的前缀和问题,我们求带修改前缀和是怎么做的?树状数组!于是我们用树状数组套线段树,树状数组里面每个点是一棵权值线段树,维护的是位置i前面lowbit(i)范围中的元素的权值信息,每一次更新的时候在logN棵线段树里修改,询问的时候logN棵线段树里一起走,在线段树上二分(树状数组套在外面写起来简单)。

      由于这个题对时间效率要求不是很高我就直接动态开点了没有离散化也没有卡常。实际上就是我比较懒

      时间复杂度O((N+M)logN^2)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<set>
     9 #include<map>
    10 #include<vector>
    11 #include<cctype>
    12 using namespace std;
    13 const int maxn=10005;
    14 const int up=1000000000;
    15 
    16 int N,M,a[maxn];
    17 struct mstruct{
    18     static const int maxnode=4300000;
    19     static const int maxl=10005;
    20     int rt[maxl],np,lc[maxnode],rc[maxnode],sz[maxnode];
    21     int q1[maxl],q2[maxl],l1,l2;
    22     mstruct(){ np=0,sz[0]=0; memset(rt,0,sizeof(rt)); }
    23     int lowbit(int i){ return i&(-i); }
    24     void pushup(int now){ sz[now]=sz[lc[now]]+sz[rc[now]]; }
    25     void inupdate(int &now,int L,int R,int p,int v){
    26         if(!now) now=++np,lc[now]=rc[now]=sz[now]=0;
    27         if(L==R){ sz[now]+=v; return; }
    28         int m=L+R>>1;
    29         if(p<=m) inupdate(lc[now],L,m,p,v);
    30         else inupdate(rc[now],m+1,R,p,v);
    31         pushup(now);
    32     }
    33     void update(int p,int v,bool c){
    34         int i=p;
    35         while(i<=N){
    36             if(c) inupdate(rt[i],0,up,a[p],-1);
    37             inupdate(rt[i],0,up,v,1);
    38             i+=lowbit(i);
    39         }
    40         a[p]=v;
    41     }
    42     void mov(bool rig){
    43         for(int i=0;i<l1;i++) q1[i]=rig?rc[q1[i]]:lc[q1[i]];
    44         for(int i=0;i<l2;i++) q2[i]=rig?rc[q2[i]]:lc[q2[i]];
    45     }
    46     int inquery(int L,int R,int k){
    47         if(L==R) return L;
    48         int m=L+R>>1,s1=0,s2=0;
    49         for(int i=0;i<l1;i++) s1+=sz[lc[q1[i]]];
    50         for(int i=0;i<l2;i++) s2+=sz[lc[q2[i]]];
    51         if(k<=s2-s1){ mov(0); return inquery(L,m,k); }
    52         mov(1); return inquery(m+1,R,k-(s2-s1));
    53     }
    54     int query(int L,int R,int k){
    55         l1=l2=0;
    56         int i=L; while(i) q1[l1++]=rt[i],i-=lowbit(i);
    57         i=R; while(i) q2[l2++]=rt[i],i-=lowbit(i);
    58         return inquery(0,up,k);
    59     }
    60 }tt;
    61 
    62 void data_in()
    63 {
    64     scanf("%d%d",&N,&M);
    65     for(int i=1;i<=N;i++) scanf("%d",&a[i]);
    66     for(int i=1;i<=N;i++) tt.update(i,a[i],0);
    67 }
    68 void work()
    69 {
    70     char op[2];
    71     int l,r,k,t;
    72     for(int i=1;i<=M;i++){
    73         scanf("%s",op);
    74         if(op[0]=='C'){
    75             scanf("%d%d",&l,&t);
    76             tt.update(l,t,1);
    77         }
    78         else if(op[0]=='Q'){
    79             scanf("%d%d%d",&l,&r,&k);
    80             printf("%d
    ",tt.query(l-1,r,k));
    81         }
    82     }
    83 }
    84 int main()
    85 {
    86     data_in();
    87     work();
    88     return 0;
    89 }
    View Code
  • 相关阅读:
    systemmap 使用记录
    reading code record
    吞吐问题
    debug cps 原因
    fopen的a+和rewind
    debug cps && perf debug
    tfo以及quic的阅读笔记
    ss 和netstat
    debug open files
    多核编程 local global
  • 原文地址:https://www.cnblogs.com/KKKorange/p/8798130.html
Copyright © 2011-2022 走看看