标签:
Description
Input
Output
Sample Input
7 3 1 5 2 6 3 7 4 2 5 3 4 4 1 1 7 3
Sample Output
5 6 3
Hint
Source
1 /* 2 宋代郑思肖 3 《画菊》 4 花开不并百花丛,独立疏篱趣未穷。 5 宁可枝头抱香死,何曾吹落北风中。 6 */ 7 #include <iostream> 8 #include <cstdio> 9 #include <algorithm> 10 #include <cstring> 11 #include <vector> 12 #include <utility> 13 #include <iomanip> 14 #include <string> 15 #include <cmath> 16 #include <queue> 17 #include <assert.h> 18 #include <map> 19 #include <ctime> 20 #include <cstdlib> 21 #include <stack> 22 #define LOCAL 23 const int MAXN = 100000 + 10; 24 const int MAXM = 5000 + 10; 25 const int INF = 0x7fffffff; 26 const int SIZE = 450; 27 const int maxnode = 250005 + 10; 28 using namespace std; 29 typedef long long ll; 30 using namespace std; 31 struct DATA{ 32 int val, x;//x代表位置 33 bool operator < (const DATA &b)const{ 34 return val < b.val; 35 } 36 }data[MAXN]; 37 struct QUESTION{ 38 int l, r; 39 int k; 40 }q[MAXM]; 41 int c[MAXN], id[MAXN], Ans[MAXN]; 42 int tmp[MAXN]; 43 bool mark[MAXN]; 44 int Max = -INF, Min = INF, pos, n, m; 45 //树状数组 46 inline int lowbit(int x){return x&-x;} 47 int sum(int x){ 48 int tmp = 0; 49 while (x > 0){ 50 tmp += c[x]; 51 x -= lowbit(x); 52 } 53 return tmp; 54 } 55 void add(int x, int val){ 56 while (x <= n){ 57 c[x] += val; 58 x += lowbit(x); 59 } 60 return; 61 } 62 63 void init(){ 64 memset(mark, 0, sizeof(mark)); 65 memset(c, 0, sizeof(c)); 66 scanf("%d%d", &n, &m); 67 for (int i = 1; i <= n; i++){ 68 scanf("%d", &data[i].val); 69 data[i].x = i; 70 Max = max(Max, data[i].val); 71 Min = min(Min, data[i].val); 72 } 73 //排序 74 sort(data + 1, data + 1 + n); 75 } 76 void solve(int l, int r, int L, int R){ 77 if (l > r || L == R) return; 78 int mid = (L + R) >> 1; 79 while (data[pos + 1].val <= mid && pos < n){ 80 add(data[pos + 1].x, 1); 81 pos++; 82 } 83 while (data[pos].val > mid){ 84 add(data[pos].x, -1); 85 pos--; 86 } 87 int cnt = 0;//记录找到答案的回答个数 88 for (int i = l; i <= r; i++){ 89 if (sum(q[id[i]].r) - sum(q[id[i]].l - 1) > q[id[i]].k - 1){ 90 Ans[id[i]] = mid; 91 mark[i] = 1; 92 cnt++; 93 }else mark[i] = 0; 94 } 95 int l1 = l, l2 = l + cnt; 96 for (int i = l; i <= r; i++) 97 if (mark[i]) tmp[l1++] = id[i]; 98 else tmp[l2++] = id[i]; 99 100 for (int i = l; i <= r; i++) id[i] = tmp[i]; 101 solve(l, l1 - 1, L, mid); 102 solve(l1, l2 - 1, mid + 1, R); 103 } 104 void work(){ 105 pos = 0;//初始化即data中的下标 106 for (int i = 1; i <= m; i++){ 107 scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].k); 108 } 109 for (int i = 1; i <= m; i++) id[i] = i; 110 solve(1, m, Min, Max + 1); 111 for (int i = 1; i <= m; i++) printf("%d\n", Ans[i]); 112 } 113 114 int main(){ 115 116 init(); 117 work(); 118 return 0; 119 }
标签:
原文地址:http://www.cnblogs.com/hoskey/p/4338312.html