青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Tim's Programming Space  
Tim's Programming Space
日歷
<2010年5月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345
統(tǒng)計
  • 隨筆 - 20
  • 文章 - 1
  • 評論 - 40
  • 引用 - 0

導(dǎo)航

常用鏈接

留言簿(3)

隨筆檔案

文章檔案

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

 
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

-----------------------------------------------------------------------------------------------------------------------------
題目意思是要對一個序列詢問某段當(dāng)中的第k大數(shù),并且支持修改單個元素的操作。
50000的數(shù)據(jù)范圍顯然要我們用高級數(shù)據(jù)結(jié)構(gòu)來維護,但是很囧的問題就是:詢問區(qū)間要用線段樹,詢問第k大要用平衡樹。。。
解決這個問題的方法很簡單,就是線段樹套平衡樹,線段樹中的每個節(jié)點都有一棵平衡樹,維護線段樹所記錄的這個區(qū)間的元素。
這樣處理空間上是O(nlogn)的,因為線段樹有l(wèi)ogn層,每層的平衡樹所記的節(jié)點總數(shù)都有n個。
修改很容易想到,把所有包含要修改點的區(qū)間的平衡樹都修改了就行了,但查詢的時候又被囧到了:詢問的區(qū)間不一定就是線段樹里面某個節(jié)點記的某個區(qū)間。
。。最終還是去找了hyf神牛。。。他一語點破天機:二分答案。。
對于每次查詢,二分第k大的數(shù)是多少,在線段樹里查詢這個區(qū)間中小于等于這個值的有多少個,就可以得到答案了。
需要注意的細(xì)節(jié)是:對于一個數(shù),可能會有重復(fù),比如對于1 2 2 2 3,查詢第2大的數(shù),當(dāng)而分到2的時候,可能查到的是第三個2,查詢結(jié)果也就是4。為了解決這個問題,可以在當(dāng)查詢不大于x的數(shù)的個數(shù)t1時,再查詢不大于x-1的數(shù)的個數(shù)t2,如果t2<k<=t1那么此時的x便是所求的第k大的數(shù)。
這樣的話,查詢是O(log(n)log(n)*log(MAXNUM))   (其實在查詢線段樹和平衡樹的時候不可能同時都達到log(n),只是具體是多少我就算不來了。。望高手解答。。),修改是O(log(n)*log(n))的(問題同上。。),就可以在時間范圍內(nèi)解決了。
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #define MAXTREAPNODE 5000000
  6 #define MAXLSTNODE 1000000
  7 #define MAXN 50000
  8 #define MAXNUM 1000000000
  9 #define INFINIT 1000000000
 10 using namespace std;
 11 
 12 
 13 int a[MAXN+1];
 14 class TreapNode{
 15     public:
 16         int v,lt,rt,p,size;
 17         void set(int v){
 18             this->= v;
 19             lt = rt = 0;
 20             p = rand();
 21             size = 1;
 22         }
 23 };
 24 TreapNode treapnode[MAXTREAPNODE+1];
 25 int pos = 0;
 26 class Treap{
 27     private:
 28     int cnt,root;
 29     void RightRotate(int &x){
 30         int lc = treapnode[x].lt;
 31         treapnode[x].lt = treapnode[lc].rt;
 32         treapnode[lc].rt = x;
 33         x = lc;
 34     }
 35     void LeftRotate(int &x){
 36         int rc = treapnode[x].rt;
 37         treapnode[x].rt = treapnode[rc].lt;
 38         treapnode[rc].lt = x;
 39         x = rc;
 40     }
 41     void Renew(int x){
 42         if (!x) return;
 43         treapnode[x].size = treapnode[treapnode[x].lt].size+treapnode[treapnode[x].rt].size+1;
 44     }
 45     void Add(int &x,int v){
 46         if (!x) treapnode[x = (++pos)].set(v);
 47         else{
 48             if (v<=treapnode[x].v){
 49                 Add(treapnode[x].lt,v);
 50                 if (treapnode[treapnode[x].lt].p<treapnode[x].p)
 51                     RightRotate(x);
 52             }
 53             else if (v>treapnode[x].v){
 54                 Add(treapnode[x].rt,v);
 55                 if (treapnode[treapnode[x].rt].p<treapnode[x].p)
 56                     LeftRotate(x);
 57             }
 58             Renew(treapnode[x].lt),Renew(treapnode[x].rt),Renew(x);
 59         }
 60     }
 61     void dfs(int x){
 62         if (!x) return;
 63         dfs(treapnode[x].lt);
 64         printf("%d ",treapnode[x].v);
 65         dfs(treapnode[x].rt);
 66     }
 67     int Ask(int &x,int k){
 68         if (k<=treapnode[treapnode[x].lt].size) return Ask(treapnode[x].lt,k);
 69         if (k == treapnode[treapnode[x].lt].size+1return treapnode[x].v;
 70         if (k>treapnode[treapnode[x].lt].size+1return Ask(treapnode[x].rt,k-treapnode[treapnode[x].lt].size-1);
 71     }
 72     int Find(int &x,int v){
 73         if (v == treapnode[x].v) return treapnode[treapnode[x].lt].size+1;
 74         if (v<treapnode[x].v){
 75             return Find(treapnode[x].lt,v);
 76         }
 77         if (v>treapnode[x].v){
 78             return treapnode[treapnode[x].lt].size+1+Find(treapnode[x].rt,v);
 79         }
 80     }
 81     void Del(int &x,int v){
 82         if (v<treapnode[x].v) Del(treapnode[x].lt,v);
 83         else if (v>treapnode[x].v) Del(treapnode[x].rt,v);
 84         else{
 85             if (!treapnode[x].lt&&!treapnode[x].rt)
 86                 x = 0;
 87             else if (treapnode[x].lt&&!treapnode[x].rt)
 88                 x = treapnode[x].lt;
 89             else if (!treapnode[x].lt&&treapnode[x].rt)
 90                 x = treapnode[x].rt;
 91             else if (treapnode[treapnode[x].lt].p<treapnode[treapnode[x].rt].p)
 92                 RightRotate(x),Del(treapnode[x].rt,v);
 93             else 
 94                 LeftRotate(x),Del(treapnode[x].lt,v);
 95         }
 96         Renew(treapnode[x].lt),Renew(treapnode[x].rt),Renew(x);
 97     }
 98     int GetSmaller(int x,int v){
 99         if (!x) return 0;
100         if (treapnode[x].v<=v)
101             return treapnode[treapnode[x].lt].size+1+GetSmaller(treapnode[x].rt,v);
102         if (treapnode[x].v>v) return GetSmaller(treapnode[x].lt,v);
103     }
104     public:
105     Treap(){cnt = root = 0;}
106     void Add(int v){
107         cnt++;
108         Add(root,v);
109     }
110     void dfs(){
111         dfs(root);
112     }
113     int Ask(int k){
114         return Ask(root,k);
115     }
116     int Find(int v){
117         return Find(root,v);
118     }
119     void Change(int v1,int v2){
120         Del(v1);
121         Add(v2);
122     }
123     void Del(int v){
124         cnt--;
125         Del(root,v);
126     }
127     void DelRank(int k){
128         int v = Ask(k);
129         Del(v);
130     }
131     int Amount(){return cnt;}
132     void Clear(){cnt = root = 0;}
133     int GetSmaller(int x){
134         return GetSmaller(root,x);
135     }
136 };
137 class LSTNode{
138     public:
139         int l,r,lt,rt;
140         Treap T;
141 };
142 int tot = 0;
143 LSTNode lstnode[MAXLSTNODE+1];
144 class LST{
145     private:
146     int AskRank(int x,int l,int r,int v){
147         if (lstnode[x].l>=l&&lstnode[x].r<=r)
148             return lstnode[x].T.GetSmaller(v);
149         else{
150             int mid = (lstnode[x].l+lstnode[x].r)>>1;
151             if (r<=mid) return AskRank(lstnode[x].lt,l,r,v);
152             else if (l>mid) return AskRank(lstnode[x].rt,l,r,v);
153             else
154                 return AskRank(lstnode[x].lt,l,r,v)+AskRank(lstnode[x].rt,l,r,v);
155         }
156     }
157     void Modify(int x,int p,int v){
158         lstnode[x].T.Change(a[p],v);
159         if (lstnode[x].l == lstnode[x].r) return;
160         int mid = (lstnode[x].l+lstnode[x].r)>>1;
161         if (p<=mid) Modify(lstnode[x].lt,p,v);
162         else if (p>mid) Modify(lstnode[x].rt,p,v);
163         else{
164             Modify(lstnode[x].lt,p,v);
165             Modify(lstnode[x].rt,p,v);
166         }
167     }
168     public:
169     LST(){tot=0;pos = 0;}
170     void BuildTree(int l,int r){
171         int x = ++tot;
172         lstnode[x].T.Clear();
173     lstnode[x].l = l,lstnode[x].r = r;
174         for (int i = l; i<=r; i++)
175             lstnode[x].T.Add(a[i]);
176         if (l == r) lstnode[x].lt = lstnode[x].rt = 0;
177         else{
178             int mid = (l+r)>>1;
179             lstnode[x].lt = tot+1,BuildTree(l,mid);
180             lstnode[x].rt = tot+1,BuildTree(mid+1,r);
181         }
182     }
183     int Ask(int L,int R,int k){
184         int l = 0,r = MAXNUM;
185         while (l<=r){
186             int mid = (l+r)>>1;
187             int t1 = AskRank(1,L,R,mid);
188             int t2 = AskRank(1,L,R,mid-1);
189             if (k<=t1&&k>t2) return mid;
190             if (t1<k) l = mid+1;
191             else r = mid;
192         }
193     }
194     void Modify(int p,int v){
195         Modify(1,p,v);
196         a[p] = v;
197     }
198     void Clear(){
199         tot = 0;pos = 0;
200     }
201 };
202 LST T;
203 void Solve(){
204     T.Clear();
205     int n,m;
206     scanf("%d%d",&n,&m);
207     for (int i = 1; i<=n; i++)
208         scanf("%d",&a[i]);
209     T.BuildTree(1,n);
210     for (int i = 1; i<=m; i++){
211         char ch;
212         int t1,t2,t3;
213         scanf("%c",&ch);
214         while (ch == '\n'||ch == ' ') scanf("%c",&ch);
215         if (ch == 'Q'){
216             scanf("%d%d%d",&t1,&t2,&t3);
217             printf("%d\n",T.Ask(t1,t2,t3));
218         }
219         if (ch == 'C'){
220             scanf("%d%d",&t1,&t2);
221             T.Modify(t1,t2);
222         }
223     }
224 }
225 int main(){
226     //freopen("2112.in","r",stdin);
227     //freopen("2112.out","w",stdout);
228     srand(1);
229     int t;
230     scanf("%d",&t);
231     while (t--)
232         Solve();
233     return 0;
234 }
235 


posted on 2009-11-26 19:04 TimTopCoder 閱讀(1486) 評論(0)  編輯 收藏 引用
 
Copyright © TimTopCoder Powered by: 博客園 模板提供:滬江博客
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲成色www久久网站| 国产视频精品免费播放| 91久久黄色| 亚洲精品美女久久7777777| 欧美成人免费小视频| 欧美va天堂| 99视频日韩| 欧美在线播放一区二区| 老巨人导航500精品| 欧美日韩不卡在线| 国产精品视频99| 激情欧美国产欧美| 亚洲人成网站精品片在线观看| 99www免费人成精品| 亚洲综合成人婷婷小说| 欧美影院一区| 亚洲国产天堂久久国产91| 亚洲婷婷综合色高清在线| 欧美一区二区精美| 欧美激情日韩| 狠狠色2019综合网| 在线亚洲欧美视频| 另类成人小视频在线| 日韩午夜三级在线| 久久久在线视频| 国产精品乱人伦一区二区 | 中文国产成人精品久久一| 午夜视频在线观看一区| 欧美成人黑人xx视频免费观看| 国产精品久久久久久久久久尿 | 欧美日韩一级大片网址| 国产欧美一区二区精品秋霞影院| 在线免费日韩片| 欧美一区二区三区成人| 亚洲精品专区| 久久一区二区精品| 国产精品日韩在线观看| 亚洲精品国产欧美| 久久久久久一区| 亚洲一区二区三区在线播放| 欧美激情视频网站| 在线观看日韩一区| 久久国产精品色婷婷| 日韩一区二区福利| 欧美在线观看网站| 亚洲第一精品福利| 欧美一区二区精品| 99国产精品国产精品久久| 久久嫩草精品久久久精品| 国产精品视频第一区| 一本色道88久久加勒比精品 | 欧美尤物巨大精品爽| 欧美日韩直播| 夜色激情一区二区| 最新高清无码专区| 欧美激情精品久久久久久久变态| 1000部精品久久久久久久久| 久久久久久欧美| 欧美一区午夜精品| 国产一区白浆| 久久人人爽国产| 久久精品国产v日韩v亚洲| 国产一区二区三区久久悠悠色av | 亚洲国产成人精品久久| 久久久青草婷婷精品综合日韩| 国产欧美日韩中文字幕在线| 亚洲欧美日韩成人| 亚洲欧美日韩在线播放| 国产伦精品一区二区三区免费| 亚洲影音一区| 亚洲一区二区三区激情| 国产精品一卡| 久久精品二区| 久久亚洲一区| 999亚洲国产精| 中文网丁香综合网| 国产日产精品一区二区三区四区的观看方式| 亚洲午夜一区二区| 亚洲女性裸体视频| 一区二区三区在线免费视频| 免费成人av在线看| 欧美黄污视频| 羞羞答答国产精品www一本| 欧美在线精品免播放器视频| 亚洲国产精品久久久久秋霞影院| 亚洲茄子视频| 国产精品私人影院| 欧美1区3d| 欧美性色视频在线| 久久影院午夜论| 欧美日韩国产不卡| 久久久久久久91| 欧美精品免费视频| 欧美专区在线观看一区| 蜜桃久久av一区| 午夜精品婷婷| 欧美成人dvd在线视频| 午夜一区不卡| 麻豆久久精品| 国产伦精品一区二区三| 亚洲国产成人久久| av成人毛片| 在线观看av不卡| 亚洲一二三区精品| 亚洲精品久久久久久下一站| 亚洲视频中文字幕| 亚洲全黄一级网站| 欧美在线观看网站| 亚洲欧美日韩一区在线观看| 狂野欧美激情性xxxx| 性8sex亚洲区入口| 欧美日韩1080p| 欧美国产日韩一区二区三区| 国产老女人精品毛片久久| 亚洲人成网站在线播| 永久555www成人免费| 亚洲在线视频| 亚洲图片欧美日产| 免费成人黄色片| 久久躁日日躁aaaaxxxx| 国产精品视频免费观看| 亚洲精品网站在线播放gif| 亚洲第一伊人| 久久美女性网| 老司机免费视频久久| 国产亚洲在线| 小辣椒精品导航| 亚洲综合电影| 国产精品久久久久一区二区三区| 亚洲精品视频免费在线观看| 亚洲美女中文字幕| 欧美国产亚洲另类动漫| 欧美激情欧美激情在线五月| 精品999日本| 久久精品视频99| 久久一区二区三区超碰国产精品 | 一区二区91| 欧美华人在线视频| 亚洲日本欧美在线| 一区二区高清在线| 欧美日韩三区| 亚洲午夜一区二区三区| 亚洲欧美影院| 国产精品素人视频| 欧美一区二区三区四区夜夜大片| 欧美伊久线香蕉线新在线| 国产日韩一区二区三区在线| 欧美一区91| 欧美成人一区二区三区| 亚洲精品国产精品国自产观看 | 伊人久久大香线蕉综合热线| 久久国产一二区| 麻豆精品视频在线观看| 亚洲黄色免费网站| 欧美日韩精品欧美日韩精品| 一本久道久久综合婷婷鲸鱼| 亚洲欧美电影院| 国产尤物精品| 欧美高清免费| 亚洲自拍高清| 美女视频一区免费观看| 日韩亚洲不卡在线| 国产酒店精品激情| 免费观看一区| 正在播放亚洲一区| 久久婷婷av| 久久久久一区二区三区| 性做久久久久久久免费看| 国内精品一区二区| 欧美激情日韩| 欧美在线免费观看| 亚洲日本成人| 久久精彩免费视频| 亚洲精品午夜精品| 国产女人水真多18毛片18精品视频| 久久精品欧美| 中国成人亚色综合网站| 嫩模写真一区二区三区三州| 一本色道久久综合狠狠躁篇的优点| 国产精品一二三| 欧美国产精品中文字幕| 性欧美1819sex性高清| 亚洲国产一区二区a毛片| 香蕉久久一区二区不卡无毒影院| 亚洲高清久久久| 国产日产亚洲精品系列| 欧美日韩成人| 欧美xx69| 久久精品一区中文字幕| 亚洲午夜高清视频| 亚洲人成免费| 欧美激情导航| 久久亚洲精选| 久久黄金**| 先锋影院在线亚洲| 99在线|亚洲一区二区| 在线看片一区| 国产亚洲一区二区三区在线播放| 欧美午夜精品久久久久久人妖| 免费在线视频一区|