标签:
题目链接:点击打开链接
细节参见代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include <cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; typedef long long ll; typedef long double ld; const ld eps = 1e-9, PI = 3.1415926535897932384626433832795; const int mod = 1000000000 + 7; const int INF = 0x3f3f3f3f; const int seed = 131; const ll INF64 = ll(1e18); const int maxn = 1e5 + 10; int n,m,cnt,root[maxn],a[maxn],x,y,k; struct node { int l, r, sum; }T[maxn * 40]; vector<int> v; int getid(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; } void update(int l, int r, int& x, int y, int pos) { T[++cnt] = T[y]; T[cnt].sum++; x = cnt; if(l == r) return ; int mid = (l + r) >> 1; if(mid >= pos) update(l, mid, T[x].l, T[y].l, pos); else update(mid+1, r, T[x].r, T[y].r, pos); } int query(int l, int r, int x, int y, int k) { if(l == r) return l; int mid = (l + r) >> 1; int sum = T[T[y].l].sum - T[T[x].l].sum; if(sum >= k) return query(l, mid, T[x].l, T[y].l, k); else return query(mid+1, r, T[x].r, T[y].r, k - sum); } int main() { while(~scanf("%d%d",&n,&m)) { for(int i = 1; i <= n; i++) scanf("%d",&a[i]), v.push_back(a[i]); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for(int i = 1; i <= n; i++) update(1, n, root[i], root[i-1], getid(a[i])); while(m--) { scanf("%d%d%d",&x,&y,&k); printf("%d\n", v[query(1, n, root[x-1],root[y], k)-1]); } } return 0; }
标签:
原文地址:http://blog.csdn.net/weizhuwyzc000/article/details/51388192