1. 程式人生 > >bzoj 1901 ZOJ 2112 Dynamic Rankings [樹狀陣列套主席樹] [線段樹套平衡樹]

bzoj 1901 ZOJ 2112 Dynamic Rankings [樹狀陣列套主席樹] [線段樹套平衡樹]

Dynamic Rankings

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 6900 Solved: 2870

Description

給定一個含有n個數的序列a[1],a[2],a[3]……a[n],程式必須回答這樣的詢問:對於給定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的數是多少(1≤k≤j-i+1),並且,你可以改變一些a[i]的值,改變後,程式還能針對改變後的a繼續回答上面的問題。你需要編一個這樣的程式,從輸入檔案中讀入序列a,然後讀入一系列的指令,包括詢問指令和修改指令。對於每一個詢問指令,你必須輸出正確的回答。 第一行有兩個正整數n(1≤n≤10000),m(1≤m≤10000)。分別表示序列的長度和指令的個數。第二行有n個數,表示a[1],a[2]……a[n],這些數都小於10^9。接下來的m行描述每條指令,每行的格式是下面兩種格式中的一種。 Q i j k 或者 C i t Q i j k (i,j,k是數字,1≤i≤j≤n, 1≤k≤j-i+1)表示詢問指令,詢問a[i],a[i+1]……a[j]中第k小的數。C i t (1≤i≤n,0≤t≤10^9)表示把a[i]改變成為t。

Input

對於每一次詢問,你都需要輸出他的答案,每一個輸出佔單獨的一行。

Output

Sample Input

5 3

3 2 1 4 7

Q 1 4 3

C 2 6

Q 2 5 3

Sample Output

3

6

HINT

20%的資料中,m,n≤100; 40%的資料中,m,n≤1000; 100%的資料中,m,n≤10000。

一句話題意:動態查詢區間第k小值
兩種寫法。。
先考慮樹狀陣列套主席樹。。
如果是靜態查詢那麼直接主席樹就好了,但是設計修改,如果還是用字首和的話修改的時間複雜度將會極高。
那麼可以考慮用樹狀陣列優化,要修改的就把對應整條lowbit路徑上的樹修改即可。
但是如果直接把原來的主席樹修改,那麼還是很不高效,那麼就可以分成兩部分,一部分就是原來的靜態主席樹查詢,另一部分是樹狀陣列套上主席樹,專門負責修改操作,查詢的時候對應地把兩部分加起來就好了。。
然後use陣列一開始不是很明白,實際上就是當前訪問到的節點標號。。。
另外,離線操作是為了得到所有使用的值,方便離散化。。。
還有的話,如果用 rev() 來作為 orig() 函式的反函式的話,會RE的。。。。因為數值大小是在10^9啊。。。。一開始在這裡無限RE。。
還要注意修改之後也要同時把a() 和 orig() 同時修改!!!!!不然會WA的。。。
參考部落格:

http://www.cnblogs.com/Empress/p/4659824.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime> #include<climits> #include<cctype> #include<algorithm> #ifdef WIN32 #define AUTO "%I64d" #else #define AUTO "%lld" #endif using namespace std; #define smax(x,tmp) x=max((x),(tmp)) #define smin(x,tmp) x=min((x),(tmp)) #define maxx(x1,x2,x3) max(max(x1,x2),x3) #define minn(x1,x2,x3) min(min(x1,x2),x3) const int INF=0x3f3f3f3f; const int maxn = 10005; int n,lim; struct Node { int sum; // number cnt int ch[2]; }node[maxn<<8]; int head[maxn]; int maxnode; #define sum(x) node[x].sum #define ch(x,d) node[x].ch[d] void build(int &root,int l,int r) { root = ++maxnode; if(l==r) return; int m=(l+r)>>1; build(ch(root,0),l,m); build(ch(root,1),m+1,r); } void insert(int &root,int last,int l,int r,int pos,int val) { root = ++maxnode; node[root] = node[last]; sum(root) += val; if(l==r) return; int m=(l+r)>>1; if(pos<=m) insert(ch(root,0),ch(last,0),l,m,pos,val); else insert(ch(root,1),ch(last,1),m+1,r,pos,val); } #define lowbit(x) ((x)&(-x)) int use[maxn]; // current node of trees int root[maxn]; // BIT with Chairman_tree of adjustment void add(int x,int pos,int val) { for(int i=x;i<=n;i+=lowbit(i)) insert(root[i],root[i],1,lim,pos,val); } int Sum(int x) { int ret = 0; for(int i=x;i;i-=lowbit(i)) ret += sum(ch(use[i],0)); // in query, visiting the left node to adjust and judge directions return ret; } int query(int s,int t,int root,int last,int l,int r,int k) { if(l==r) return l; int m=(l+r)>>1; int lsize = Sum(t) - Sum(s) + sum(ch(root,0)) - sum(ch(last,0)); // Sum of sigma left if(k<=lsize) { for(int i=s;i;i-=lowbit(i)) use[i] = ch(use[i],0); for(int i=t;i;i-=lowbit(i)) use[i] = ch(use[i],0); return query(s,t,ch(root,0),ch(last,0),l,m,k); } else { for(int i=s;i;i-=lowbit(i)) use[i] = ch(use[i],1); for(int i=t;i;i-=lowbit(i)) use[i] = ch(use[i],1); return query(s,t,ch(root,1),ch(last,1),m+1,r,k-lsize); } } struct Query { bool flag; // false is Q int x,y,z; // (pos,t) (i,j,k) }que[maxn]; struct Data { int val,order; bool operator < (const Data t) const { return val < t.val; } }data[maxn<<1]; // count the queries int maxdata; int a[maxn]; // sequence after discretes int orig[maxn<<1]; // quick link from val_discretion to val_original int main() { freopen("ranking.in","r",stdin); freopen("ranking.out","w",stdout); int q; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) scanf("%d",&data[i].val),data[i].order=i; maxdata = n; for(int i=1;i<=q;i++) // off line to discrete!! { char ch = getchar(); while(ch^'Q' && ch^'C') ch = getchar(); if(ch=='C') que[i].flag=true; scanf("%d%d",&que[i].x,&que[i].y); if(ch=='Q') scanf("%d",&que[i].z); if(ch=='C') data[++maxdata].val=que[i].y; } sort(data+1,data+maxdata+1); int last=-INF; for(int i=1;i<=maxdata;i++) if(last^data[i].val) last = data[i].val , a[data[i].order]=++lim , orig[lim]=data[i].val; // ignore a[0] else a[data[i].order]=lim; //end discretization build(head[0],1,lim); for(int i=1;i<=n;i++) insert(head[i],head[i-1],1,lim,a[i],1); for(int i=1;i<=n;i++) root[i]=head[0]; // tree of adjustion is empty for(int i=1;i<=q;i++) if(que[i].flag) { add(que[i].x,a[que[i].x],-1); a[que[i].x] = lower_bound(orig+1,orig+lim+1,que[i].y)-orig; // update not only the a but also the orig!!!! orig[a[que[i].x]] = que[i].y; add(que[i].x,a[que[i].x],1); } else { for(int j=que[i].x-1;j;j-=lowbit(j)) use[j] = root[j]; for(int j=que[i].y;j;j-=lowbit(j)) use[j] = root[j]; int ans = query(que[i].x-1,que[i].y,head[que[i].y],head[que[i].x-1],1,lim,que[i].z); printf("%d\n",orig[ans]); } return 0; }

線段樹套平衡樹寫法
當初這道題就是神一樣的資料結構,在寫過樹狀陣列套主席樹之後其實發現也挺好寫的,完全沒有看網上的程式碼(題解還是有看。。。。)
其實線段樹的每一個節點就是一棵平衡樹,儲存了區間的值的資訊
然後修改就是在所有包含這個點的區間的平衡樹內先刪除再插入。。。
查詢稍微麻煩一點,是一個二分答案,如果二分數字的話是 log(10^9) 稍微有點大,那麼我們可以線上段樹根節點裡面二分排名,那麼每次確定整個數組裡的一個排名所對應的數時候,到所有查詢區間裡的小區間裡面去求一個rank,但是要注意有重複的情況!!那麼把∑rank求出來就可以和當前查詢的k比較了,然後答案要保留在左區間。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 10005;

struct Node
{
    int val;
    int size,cnt;
    int ch[2],fa;
}node[maxn<<9];
int maxnode;
#define val(x) node[x].val
#define size(x) node[x].size
#define cnt(x) node[x].cnt
#define fa(x) node[x].fa
#define ch(x,d) node[x].ch[d]
int root[maxn<<2]; //segment tree
inline void update(int x)
{
    size(x) = cnt(x);
    int l=ch(x,0) , r=ch(x,1);
    if(l) size(x)+=size(l);
    if(r) size(x)+=size(r);
}
inline void rotate(int x,int &to)
{
    int y=fa(x),z=fa(y);
    int l= ch(y,1)==x , r=l^1;
    if(y==to) to=x;
    else ch(z,ch(z,1)==y)=x;
    fa(ch(x,r))=y; fa(y)=x; fa(x)=z;
    ch(y,l)=ch(x,r); ch(x,r)=y;
    update(y); update(x);
}
void splay(int x,int &to)
{
    while(x^to)
    {
        int y=fa(x),z=fa(y);
        if(y^to)
            if(ch(y,0)==x ^ ch(z,0)==y) rotate(x,to);
            else rotate(y,to);
        rotate(x,to);
    }
}
void insert(int &rt,int key)
{
    if(!rt) { rt=++maxnode; val(rt)=key; size(rt)=cnt(rt)=1; return; }
    int x=rt,y=0;
    while(x && val(x)^key) y=x,x=ch(x,key>val(x));
    if(x) cnt(x)++;
    else
    {
        x=++maxnode;
        val(x)=key;
        cnt(x)=1;
        fa(x)=y;
        if(y) ch(y,key>val(y))=x;
    }
    update(x); // incase s is root!!
    splay(x,rt);
}
inline int find(int &rt,int key) // provided the key exists
{
    int x = rt;
    while(val(x) ^ key) x = ch(x,key>val(x));
    return x;
}
inline int pre(int x) // provided the pre exists
{
    x = ch(x,0);
    while(ch(x,1)) x = ch(x,1);
    return x;
}
inline int next(int x)
{
    x = ch(x,1);
    while(ch(x,0)) x = ch(x,0);
    return x;
}
void erase(int &rt,int key)
{
    if(size(rt)==1)
    {
        rt = 0;
        return;
    }
    int x=find(rt,key);
    splay(x,rt);
    if(cnt(rt)>1)
    {
        size(rt)--;
        cnt(rt)--;
        return;
    }
    if(!ch(x,0) && !ch(x,1)) rt=0;
    if(!ch(x,0) && ch(x,1)) rt=ch(x,1); // list all the limits!!!
    if(!ch(x,1) && ch(x,0)) rt=ch(x,0);
    if(ch(x,0) && ch(x,1))
    {
        int l=pre(x),r=next(x);
        splay(l,rt); splay(r,ch(l,1));
        size(ch(r,0))--;
        if(--cnt(ch(r,0)) == 0) ch(r,0)=0;
        update(r); update(l);
    }
}
int rank(int &rt,int key) // super lower rank , where key is NOT provided to exist
{
    insert(rt,key); // with splay
    int ret = cnt(rt)-1; // necessary to count the equality
    if(ch(rt,0)) ret += size(ch(rt,0));
    erase(rt,key);
    return ret;
}
int kth(int rt,int k)
{
    int lsize = 0;
    if(ch(rt,0)) lsize = size(ch(rt,0));
    if(lsize<k && k<=lsize+cnt(rt)) return val(rt);
    if(k<=lsize) return kth(ch(rt,0),k);
    else return kth(ch(rt,1),k-lsize-cnt(rt));
}

int a[maxn],n;
void build(int rt,int l,int r)
{
    for(int i=l;i<=r;i++) insert(root[rt],a[i]);
    if(l==r) return;
    int m=(l+r)>>1;
    build(rt<<1,l,m);
    build(rt<<1|1,m+1,r);
}
void modify(int rt,int l,int r,int pos,int key) // remember to update a[pos] after
{
    erase(root[rt],a[pos]);
    insert(root[rt],key);
    if(l==r) return;
    int m=(l+r)>>1;
    if(pos<=m) modify(rt<<1,l,m,pos,key);
    else modify(rt<<1|1,m+1,r,pos,key);
}
int query(int rt,int l,int r,int x,int y,int key)
{
    if(x<=l && r<=y) return rank(root[rt],key);
    int m=(l+r)>>1;
    int ret = 0;
    if(x<=m && l<=y) ret += query(rt<<1,l,m,x,y,key);
    if(y>=m+1 && r>=x) ret += query(rt<<1|1,m+1,r,x,y,key);
    return ret;
}
int search(int x,int y,int k)
{
    int l=1,r=n;
    while(l<r)
    {
        int m=(l+r)>>1;
        int rnk = query(1,1,n,x,y,kth(root[1],m)); // as counted above , not needed to +1
        if(rnk < k) l=m+1;
        else r=m;
    }
    return kth(root[1],l);
}
void change(int pos,int key)
{
    modify(1,1,n,pos,key);
    a[pos]=key;
}
int main()
{
    freopen("ranking.in","r",stdin);
    freopen("ranking.out","w",stdout);
    int q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++) scanf("%d",a+i);
    build(1,1,n);
    for(int i=1;i<=q;i++)
    {
        char ch=getchar();
        while(ch^'C' && ch^'Q') ch=getchar();
        int x,y,z;
        if(ch=='C')
        {
            scanf("%d%d",&x,&y);
            change(x,y);
        }
        else
        {
            scanf("%d%d%d",&x,&y,&z);
            int ans = search(x,y,z);
            printf("%d\n",ans);
        }
    }
    return 0;
}

時空複雜度對比。。。。
這裡寫圖片描述