求区间内个数大于rank的一个数

主席树求一下就好啦!

 

 1 /**************************************************************
 2     Problem: 2223
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:704 ms
 7     Memory:54712 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <algorithm>
12  
13 using namespace std;
14 const int N = 3e5 + 5;
15 const int Cnt_chair = 4500000;
16  
17 struct chair_node {
18     chair_node *ls, *rs;
19     int cnt;
20 } *root[N], mempool[Cnt_chair], *cnt_chair = mempool, *null;
21  
22 int n, mx, ans;
23  
24 inline int read() {
25     int x = 0;
26     char ch = getchar();
27     while (ch < '0' || '9' < ch)
28         ch = getchar();
29     while ('0' <= ch && ch <= '9') {
30         x = x * 10 + ch - '0';
31         ch = getchar();
32     }
33     return x;
34 }
35  
36 #define mid (l + r >> 1)
37 void chair_insert(chair_node *p, chair_node *&q, int l, int r, int x) {
38     (q = ++cnt_chair) -> cnt = p -> cnt + 1;
39     if (l == r) return;
40     if (x <= mid) 
41         q -> rs = p -> rs, chair_insert(p -> ls, q -> ls, l, mid, x);
42     else
43         q -> ls = p -> ls, chair_insert(p -> rs, q -> rs, mid + 1, r, x);
44 }
45  
46 int chair_query(chair_node *p, chair_node *q, int l, int r, int rank) {
47     if (l == r) return l;
48     if (q -> ls -> cnt - p -> ls -> cnt > rank)
49         return chair_query(p -> ls, q -> ls, l, mid, rank);
50     if (q -> rs -> cnt - p -> rs -> cnt > rank)
51         return chair_query(p -> rs, q -> rs, mid + 1, r, rank);
52     return 0;
53 }
54 #undef mid
55  
56 int main() {
57     int Q, i, l, r;
58     n = read(), mx = read();
59     null = cnt_chair;
60     null -> cnt = 0, null -> ls = null -> rs = null;
61     root[0] = null;
62     for (i = 1; i <= n; ++i)
63         chair_insert(root[i - 1], root[i], 1, mx, read());
64     Q = read();
65     while (Q--) {
66         l = read(), r = read();
67         if (l > r) swap(l, r);
68         ans = chair_query(root[l - 1], root[r], 1, mx, (r - l + 1) >> 1);
69         if (ans == 0) printf("no\n");
70         else printf("yes %d\n", ans);
71     }
72     return 0;
73 }
View Code

(p.s. 数组开小了连WA两发。。。555)

posted on 2015-02-20 18:36  Xs酱~  阅读(213)  评论(0编辑  收藏  举报