【POJ】2104 K-th Number

区间第K大数。
主席树可解。

  1 /* 2104 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <algorithm>
 12 #include <cstdio>
 13 #include <cmath>
 14 #include <ctime>
 15 #include <cstring>
 16 #include <climits>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <functional>
 20 #include <iterator>
 21 #include <iomanip>
 22 using namespace std;
 23 //#pragma comment(linker,"/STACK:102400000,1024000")
 24 
 25 #define sti                set<int>
 26 #define stpii            set<pair<int, int> >
 27 #define mpii            map<int,int>
 28 #define vi                vector<int>
 29 #define pii                pair<int,int>
 30 #define vpii            vector<pair<int,int> >
 31 #define rep(i, a, n)     for (int i=a;i<n;++i)
 32 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 33 #define clr                clear
 34 #define pb                 push_back
 35 #define mp                 make_pair
 36 #define fir                first
 37 #define sec                second
 38 #define all(x)             (x).begin(),(x).end()
 39 #define SZ(x)             ((int)(x).size())
 40 // #define lson            l, mid, rt<<1
 41 // #define rson            mid+1, r, rt<<1|1
 42 
 43 const int maxn = 1e5+5;
 44 const int maxm = maxn * 50;
 45 int T[maxn];
 46 int lson[maxm], rson[maxm], c[maxm];
 47 int a[maxn], b[maxn];
 48 int n, m;
 49 int tot = 0;
 50 
 51 int Build(int l, int r) {
 52     int rt = tot++;
 53     
 54     c[rt] = 0;
 55     if (l == r)
 56         return rt;
 57     
 58     int mid = (l + r) >> 1;
 59     
 60     lson[rt] = Build(l, mid);
 61     rson[rt] = Build(mid+1, r);
 62     
 63     return rt;
 64 }
 65 
 66 int Insert(int rt, int x, int delta) {
 67     int nrt = tot++, ret = nrt;
 68     int l = 0, r = m - 1, mid;
 69     
 70     c[nrt] = c[rt] + delta;
 71     while (l < r) {
 72         mid = (l + r) >> 1;
 73         if (x <= mid) {
 74             lson[nrt] = tot++;
 75             rson[nrt] = rson[rt];
 76             nrt = lson[nrt];
 77             rt = lson[rt];
 78             r = mid;
 79         } else {
 80             lson[nrt] = lson[rt];
 81             rson[nrt] = tot++;
 82             nrt = rson[nrt];
 83             rt = rson[rt];
 84             l = mid + 1;
 85         }
 86         c[nrt] = c[rt] + delta;
 87     }
 88     
 89     return ret;
 90 }
 91 
 92 int Query(int lrt, int rrt, int k) {
 93     int l = 0, r = m - 1, mid;
 94     int tmp;
 95     
 96     while (l < r) {
 97         mid = (l + r) >> 1;
 98         tmp = c[lson[rrt]] - c[lson[lrt]];
 99         if (tmp >= k) {
100             lrt = lson[lrt];
101             rrt = lson[rrt];
102             r = mid;
103         } else {
104             k -= tmp;
105             lrt = rson[lrt];
106             rrt = rson[rrt];
107             l = mid + 1;
108         }
109     }
110     
111     return l;
112 }
113 
114 int main() {
115     ios::sync_with_stdio(false);
116     #ifndef ONLINE_JUDGE
117         freopen("data.in", "r", stdin);
118         freopen("data.out", "w", stdout);
119     #endif
120     
121     int n, q;
122     
123     scanf("%d %d", &n, &q);
124     rep(i, 0, n) {
125         scanf("%d", &a[i]);
126         b[i] = a[i];
127     }
128     sort(b, b+n);
129     m = unique(b, b+n) - b;
130     T[0] = Build(0, m-1);
131     rep(i, 0, n) {
132         int id = lower_bound(b, b+m, a[i]) - b;
133         T[i+1] = Insert(T[i], id, 1);
134     }
135     
136     int l, r, kth;
137     int ans;
138     
139     while (q--) {
140         scanf("%d %d %d", &l, &r, &kth);
141         ans = b[Query(T[l-1], T[r], kth)];
142         printf("%d\n", ans);
143     }
144     
145     #ifndef ONLINE_JUDGE
146         printf("time = %d.\n", (int)clock());
147     #endif
148     
149     return 0;
150 }

划分树可解。

  1 /* 2104 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <algorithm>
 12 #include <cstdio>
 13 #include <cmath>
 14 #include <ctime>
 15 #include <cstring>
 16 #include <climits>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <functional>
 20 #include <iterator>
 21 #include <iomanip>
 22 using namespace std;
 23 //#pragma comment(linker,"/STACK:102400000,1024000")
 24 
 25 #define sti                set<int>
 26 #define stpii            set<pair<int, int> >
 27 #define mpii            map<int,int>
 28 #define vi                vector<int>
 29 #define pii                pair<int,int>
 30 #define vpii            vector<pair<int,int> >
 31 #define rep(i, a, n)     for (int i=a;i<n;++i)
 32 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 33 #define clr                clear
 34 #define pb                 push_back
 35 #define mp                 make_pair
 36 #define fir                first
 37 #define sec                second
 38 #define all(x)             (x).begin(),(x).end()
 39 #define SZ(x)             ((int)(x).size())
 40 // #define lson            l, mid, rt<<1
 41 // #define rson            mid+1, r, rt<<1|1
 42 #define LL                __int64
 43 
 44 const int maxn = 1e5+5;
 45 int order[maxn];
 46 int val[18][maxn];
 47 int toLeft[18][maxn];
 48 
 49 void Build(int l, int r, int dep) {
 50     if (l == r)
 51         return ;
 52     
 53     int mid = (l + r) >> 1;
 54     int same = mid - l + 1;
 55     
 56     rep(i, l, r+1) {
 57         if (val[dep][i] < order[mid])
 58             --same;
 59     }
 60     
 61     int lpos = l, rpos = mid + 1;
 62     rep(i, l, r+1) {
 63         if (val[dep][i] < order[mid]) {
 64             val[dep+1][lpos++] = val[dep][i];
 65         } else if (val[dep][i]==order[mid] && same>0) {
 66             val[dep+1][lpos++] = val[dep][i];
 67             --same;
 68         } else {
 69             val[dep+1][rpos++] = val[dep][i];
 70         }
 71         toLeft[dep][i] = toLeft[dep][l-1] + lpos - l;        
 72     }
 73     
 74     Build(l, mid, dep+1);
 75     Build(mid+1, r, dep+1);
 76 }
 77 
 78 int Query(int l, int r, int k, int L, int R, int dep) {
 79     if (l == r)
 80         return val[dep][l];
 81     
 82     int mid = (L + R) >> 1;
 83     int tmp = toLeft[dep][r] - toLeft[dep][l-1];
 84     
 85     if (tmp >= k) {
 86         int ll = L + toLeft[dep][l-1] - toLeft[dep][L-1];
 87         int rr = ll + tmp - 1;
 88         
 89         return Query(ll, rr, k, L, mid, dep+1);
 90     } else {
 91         k-= tmp;
 92         
 93         int rr = r + toLeft[dep][R] - toLeft[dep][r];
 94         int ll = rr - (r-l+1-tmp) + 1;
 95         
 96         return Query(ll, rr, k, mid+1, R, dep+1);
 97     }
 98 }
 99 
100 int main() {
101     ios::sync_with_stdio(false);
102     #ifndef ONLINE_JUDGE
103         freopen("data.in", "r", stdin);
104         freopen("data.out", "w", stdout);
105     #endif
106     
107     int n, q;
108     int l, r, kth;
109     int ans;
110     
111     scanf("%d %d", &n, &q);
112     rep(i, 1, n+1) {
113         scanf("%d", &val[0][i]);
114         order[i] = val[0][i];
115     }
116     sort(order+1, order+1+n);
117     Build(1, n, 0);
118     while (q--) {
119         scanf("%d %d %d", &l, &r, &kth);
120         ans = Query(l, r, kth, 1, n, 0);
121         printf("%d\n", ans);
122     }
123     
124     #ifndef ONLINE_JUDGE
125         printf("time = %d.\n", (int)clock());
126     #endif
127     
128     return 0;
129 }

 


数据发生器。

 1 from copy import deepcopy
 2 from random import randint, shuffle
 3 import shutil
 4 import string
 5 
 6 
 7 def GenDataIn():
 8     with open("data.in", "w") as fout:
 9         t = 1
10         bound = 10**3
11         # fout.write("%d\n" % (t))
12         for tt in xrange(t):
13             n = randint(100, 200)
14             q = randint(100, 200)
15             fout.write("%d %d\n" % (n, q))
16             L = []
17             for i in xrange(n):
18                 x = randint(1, bound)
19                 L.append(x)
20             fout.write(" ".join(map(str, L)) + "\n")
21             for i in xrange(q):
22                 l = randint(1, n)
23                 r = randint(l, n)
24                 k = randint(1, r-l+1)
25                 fout.write("%d %d %d\n" % (l, r, k))
26                 
27             
28                 
29 def MovDataIn():
30     desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
31     shutil.copyfile("data.in", desFileName)
32 
33     
34 if __name__ == "__main__":
35     GenDataIn()
36     MovDataIn()

 

posted on 2016-02-13 23:57  Bombe  阅读(164)  评论(0编辑  收藏  举报

导航