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

Tim's Programming Space  
Tim's Programming Space
日歷
<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
統(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)來維護(hù),但是很囧的問題就是:詢問區(qū)間要用線段樹,詢問第k大要用平衡樹。。。
解決這個問題的方法很簡單,就是線段樹套平衡樹,線段樹中的每個節(jié)點都有一棵平衡樹,維護(hù)線段樹所記錄的這個區(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))   (其實在查詢線段樹和平衡樹的時候不可能同時都達(dá)到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 閱讀(1491) 評論(0)  編輯 收藏 引用

只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


 
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>
            亚洲丶国产丶欧美一区二区三区| 欧美激情四色| 亚洲第一区在线| 国产一区二三区| 国产一区二区三区无遮挡| 国产精品日韩一区| 欧美三级资源在线| 国产日韩一区二区三区| 国产一区二区日韩| 99精品视频一区二区三区| 午夜久久久久久久久久一区二区| 欧美国产一区二区| 经典三级久久| **网站欧美大片在线观看| 91久久中文字幕| 亚洲性视频h| 久久九九热免费视频| 欧美肥婆在线| 一本久久青青| 久久精品五月婷婷| 欧美韩日一区二区三区| 国产精品视频一区二区高潮| 亚洲成人在线| 亚洲女性喷水在线观看一区| 狼狼综合久久久久综合网 | 一区二区三区高清在线观看| 亚洲欧美中文在线视频| 欧美成人一区二区| 国产麻豆视频精品| aa成人免费视频| 美女任你摸久久| 亚洲视频一区在线观看| 欧美国产免费| 韩国av一区二区三区| 亚洲一区二区成人在线观看| 欧美成人免费在线视频| 亚洲一区免费看| 欧美精品成人| 亚洲国产精品一区制服丝袜 | 亚洲午夜电影在线观看| 免费在线欧美黄色| 亚洲欧美清纯在线制服| 欧美日韩综合| 亚洲日本在线观看| 蜜臀av在线播放一区二区三区| 亚洲少妇自拍| 欧美日韩综合另类| av成人免费在线观看| 蜜桃伊人久久| 久久人人爽人人| 加勒比av一区二区| 久久久久久精| 欧美在线观看视频一区二区三区 | 美女日韩欧美| 极品日韩久久| 免费久久久一本精品久久区| 欧美制服丝袜第一页| 99pao成人国产永久免费视频| 久久久久高清| 国产最新精品精品你懂的| 在线免费一区三区| 久久精品青青大伊人av| 欧美视频一区二区三区四区| 欧美va亚洲va日韩∨a综合色| 久久亚洲国产精品一区二区| 午夜影视日本亚洲欧洲精品| 国产区亚洲区欧美区| 欧美一区二区三区免费大片| 午夜精品久久久久99热蜜桃导演| 国产精品一区二区三区免费观看| 欧美亚洲在线视频| 欧美中文字幕精品| 亚洲国产成人一区| 最新日韩在线视频| 欧美本精品男人aⅴ天堂| 亚洲美女淫视频| 中文一区二区| 国产在线观看91精品一区| 久热精品视频在线观看一区| 久久在线免费观看视频| 亚洲精品乱码| 亚洲先锋成人| 一区在线播放视频| 91久久久在线| 国产精品日韩在线| 亚洲大胆人体视频| 欧美日韩一级视频| 久久国产主播| 欧美国产第一页| 性亚洲最疯狂xxxx高清| 久久久久久久一区二区三区| 亚洲伦理网站| 亚洲欧美日韩在线高清直播| 在线欧美亚洲| 亚洲综合色噜噜狠狠| 亚洲国产成人精品女人久久久 | 亚洲国产精品福利| 国产精品久久一级| 美国成人直播| 欧美日韩国产一级| 久久嫩草精品久久久精品一| 欧美精品久久久久久久| 欧美中日韩免费视频| 欧美激情欧美狂野欧美精品 | 欧美 日韩 国产一区二区在线视频 | 亚洲清纯自拍| 亚洲欧美日韩国产中文| 亚洲免费电影在线| 久久精品道一区二区三区| 在线一区二区三区做爰视频网站| 欧美亚洲一区二区在线| 欧美日韩情趣电影| 免费一级欧美片在线播放| 欧美色偷偷大香| 一二三区精品福利视频| 亚洲国产一成人久久精品| 欧美精品久久一区| 亚洲视屏在线播放| 欧美日韩在线第一页| 久久综合伊人77777蜜臀| 国产精品欧美久久| 亚洲精品久久视频| 亚洲人成高清| 欧美freesex8一10精品| 久久在线免费观看| 国产美女精品免费电影| 99天天综合性| 亚洲婷婷国产精品电影人久久| 牛人盗摄一区二区三区视频| 久久伊人精品天天| 国产欧美日韩在线播放| 亚洲午夜未删减在线观看| 亚洲综合首页| 国产精品白丝jk黑袜喷水| 亚洲毛片在线观看| 一本一本a久久| 欧美日韩免费| 亚洲毛片一区| 亚洲综合国产| 国产精品手机视频| 亚洲欧美视频在线| 久久久久久久久久看片| 国产一区二区三区久久精品| 小黄鸭精品密入口导航| 久久久综合香蕉尹人综合网| 黄色av一区| 女人色偷偷aa久久天堂| 亚洲欧洲日韩女同| 亚洲一区影音先锋| 国产农村妇女毛片精品久久莱园子| 亚洲欧美日韩在线高清直播| 久热精品视频在线观看一区| 亚洲黄一区二区三区| 欧美精品一区二| 这里只有视频精品| 久久久国产午夜精品| 激情欧美日韩一区| 欧美大片免费观看| 亚洲视频福利| 久久蜜桃精品| 亚洲精品综合| 国产精品欧美日韩一区| 久久亚洲综合网| 亚洲精品之草原avav久久| 亚洲欧美视频在线观看视频| 国内一区二区在线视频观看| 欧美国产专区| 亚洲女爱视频在线| 老司机一区二区三区| 一级日韩一区在线观看| 国产视频在线观看一区二区| 欧美成人高清| 午夜精品亚洲一区二区三区嫩草| 欧美成人激情在线| 亚洲欧美国产精品桃花| 亚洲第一偷拍| 国产精品一区一区三区| 农夫在线精品视频免费观看| 亚洲午夜精品久久久久久app| 欧美成在线观看| 国产精品久久久久久久久久久久久 | 欧美午夜无遮挡| 欧美日韩精品在线播放| 欧美精品播放| 99精品欧美一区二区蜜桃免费| 在线视频精品一区| 在线播放不卡| 国产麻豆精品在线观看| 欧美日韩亚洲综合在线| 另类欧美日韩国产在线| 性欧美长视频| 亚洲视频一区二区免费在线观看| 欧美成ee人免费视频| 欧美在线免费播放| 亚洲欧美日本伦理| 一区二区三区日韩| 亚洲高清不卡| 黄色成人精品网站| 国产午夜亚洲精品羞羞网站| 国产精品a久久久久|