【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 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()
时间: 2024-10-05 04:45:01

【POJ】2104 K-th Number的相关文章

【POJ】2449 Remmarguts&#39; Date(k短路)

http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k 首先我们建反向边,跑一次从汇到源的最短路,将跑出来的最短路作为估价函数h 根据f=g+h 我们将源s先走,此时实际价值g为0,估价为最短路(他们的和就是s-t的最短路) 将所有s所连的边都做相同的处理,加入到堆中(假设此时到达的点为x,那么x的g等于s到这个点的边权,因为根据最优,g+h此时是从x

【POJ】1228 Grandpa&#39;s Estate(凸包)

http://poj.org/problem?id=1228 随便看看就能发现,凸包上的每条边必须满足,有相邻的边和它斜率相同(即共线或凸包上每个点必须一定在三点共线上) 然后愉快敲完凸包+斜率判定,交上去wa了QAQ.原因是忘记特判一个地方....因为我们求的凸包是三点共线的凸包,在凸包算法中我们叉积判断只是>0而不是>=0,那么会有一种数据为所有点共线的情况,此时求出来的凸包上的点是>原来的点的(此时恰好符合答案NO,因为可以在这条线外随便点一个点就是一个凸包了...)然后特判一下.

【POJ】 Instant Complexity (模拟)

[POJ] Instant Complexity (模拟) Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1905   Accepted: 657 Description Analyzing the run-time complexity of algorithms is an important tool for designing efficient programs that

【POJ】【2449】Remmarguts&#39; Date

K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstra,得到估价函数h(x)(x到T的最短路距离) f(x)=g(x)+h(x) 按f(x)维护一个堆……T第k次出堆时的g(T)即为ans 另外,需要特判:如果S==T,k++ 1 Source Code 2 Problem: 2449 User: sdfzyhy 3 Memory: 11260K T

【POJ】2278 DNA Sequence

各种wa后,各种TLE.注意若AC非法,则ACT等一定非法.而且尽量少MOD. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 105 8 #define NXTN 4 9 10 char str[15]; 11 12 typedef struct Matrix {

【POJ】1739 Tony&#39;s Tour

http://poj.org/problem?id=1739 题意:n×m的棋盘,'#'是障碍,'.'是空白,求左下角走到右下角且走过所有空白格子的方案数.(n,m<=8) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; #define BIT(a,b) ((a)<<((b)<<1)) #

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

【POJ】2513 Colored Sticks

字典树+并查集. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 500005 6 #define MAXL 11 7 #define TRIEN 26 8 9 typedef struct Trie { 10 int v; 11 Trie *next[TRIEN]; 12 Trie() { 13 v = 0; 14 for (int i=0; i<TRI