POJ2104 K-th Number
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2104 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 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()
以上是关于POJ2104 K-th Number的主要内容,如果未能解决你的问题,请参考以下文章