标签:win names cto space cas not sorted each 思路
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 55340 | Accepted: 19030 | |
Case Time Limit: 2000MS |
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
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<vector> using namespace std; const int N_MAX = 100000 + 4,M_MAX=5000+1; const int ST_SIZE = (1<<18)-1; int N, M; int A[N_MAX]; int l[M_MAX], r[M_MAX], k[M_MAX]; int nums[N_MAX];//nums是排好序的数列,对于其中每一个数进行二分搜索,判断是否符合要求 vector<int>dat[ST_SIZE]; void init(int k,int l,int r) {//节点k,对应区间[l,r) if (r - l == 1) { dat[k].push_back(A[l]);//节点处的数列只有一个值 } else { int k1 = 2 * k + 1, k2 = 2 * k + 2; init(k1,l,(l+r)/2); init(k2,(l+r)/2,r); dat[k].resize(r-l);//先预置空间,才可以进行归并 merge(dat[k1].begin(), dat[k1].end(),dat[k2].begin(),dat[k2].end(),dat[k].begin()); } } int query(int a,int b,int x,int k,int l,int r) {//查找区间[a,b)中小于等于x的数的个数 if (l >= b || r <= a) { return 0; } else if (a <= l&&r <= b) { return upper_bound(dat[k].begin(),dat[k].end(),x)-dat[k].begin(); } else { int lc = query(a, b, x, 2 * k + 1, l, (l + r) / 2); int rc = query(a, b, x, 2 * k + 2, (l + r) / 2, r); return lc + rc; } } int main() { scanf("%d%d",&N,&M); for (int i = 0; i < N; i++) { scanf("%d",&A[i]); nums[i] = A[i]; } sort(nums,nums+N); for (int i = 0; i < M; i++) { scanf("%d%d%d",&l[i],&r[i],&k[i]); l[i]--, r[i]--; } init(0,0,N); for (int i = 0; i < M; i++) { int L = l[i], R = r[i]+1, K = k[i];//!!!!!+1别忘 int lb = -1, ub = N - 1; while (ub - lb > 1) { int mid = (lb + ub) >> 1; int kk = query(L, R, nums[mid], 0, 0, N); if (kk >= K)ub = mid; else lb = mid; } printf("%d\n",nums[ub]); } return 0; }
标签:win names cto space cas not sorted each 思路
原文地址:http://www.cnblogs.com/ZefengYao/p/6655358.html