Feed the dogs
Time Limit: 6000MSMemory Limit: 65536K
Total Submissions: 7153Accepted: 1910

Description

Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other. 

Your task is to help Jiajia calculate which dog ate the food after each feeding. 

Input

The first line contains n and m, indicates the number of dogs and the number of feedings. 

The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier. 

Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding. 

You can assume that n<100001 and m<50001. 

Output

Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.

Sample Input

7 2
1 5 2 6 3 7 4
1 5 3
2 7 1

Sample Output

3
2
题目大意:查询区间第K小数
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 using namespace std;
  6 const int N = 100011;
  7 
  8 int a[N];
  9 int b[N];
 10 int ans;
 11 int res[50011];
 12 struct node
 13 {
 14     int l;
 15     int r;
 16     int mid;
 17     int cnt;
 18 };
 19 struct seg
 20 {
 21     int s,t;
 22     int k;
 23     int index;
 24 };
 25 bool cmp(seg a,seg b)
 26 {
 27     if(a.s==b.s)return a.t < b.t;
 28     return a.s < b.s;
 29 }
 30 node seg_tree[3*N];
 31 seg segs[50011];
 32 
 33 void make(int l,int r,int num)
 34 {
 35     seg_tree[num].l = l;
 36     seg_tree[num].r = r;
 37     seg_tree[num].mid = (l+r)/2;
 38     seg_tree[num].cnt = 0;
 39     if(l+1!=r)
 40     {
 41         make(l,seg_tree[num].mid,2*num);
 42         make(seg_tree[num].mid,r,2*num+1);
 43     }
 44 }
 45 void insert(int val,int num)
 46 {
 47     if(seg_tree[num].l+1==seg_tree[num].r)
 48     {
 49         seg_tree[num].cnt++;
 50         return;
 51     }
 52     if(val<seg_tree[num].mid)
 53     {
 54         insert(val,2*num);
 55     }
 56     else
 57     {
 58         insert(val,2*num+1);
 59     }
 60     seg_tree[num].cnt++;
 61 }
 62 void del(int val,int num)
 63 {
 64     if(seg_tree[num].l+1==seg_tree[num].r)
 65     {
 66         seg_tree[num].cnt--;
 67         return;
 68     }
 69     if(val<seg_tree[num].mid)
 70     {
 71         del(val,2*num);
 72     }
 73     else
 74     {
 75         del(val,2*num+1);
 76     }
 77     seg_tree[num].cnt--;
 78 }
 79 void cal(int k,int num)
 80 {
 81     if(seg_tree[num].l+1==seg_tree[num].r)
 82     {
 83         ans = a[seg_tree[num].l];
 84         return;
 85     }
 86     if(k<=seg_tree[2*num].cnt)
 87     {
 88         cal(k,2*num);
 89     }
 90     else
 91     {
 92         cal(k-seg_tree[2*num].cnt,2*num+1);
 93     }
 94 }
 95 
 96 int n,m;
 97 
 98 int b_search(int v)
 99 {
100     int l = 1,r = n+1;
101     while(l<r)
102     {
103         int m = l + (r-l)/2;
104         if(a[m]==v)return m;
105         else if(a[m]>v)
106         {
107             r = m;
108         }
109         else
110         {
111             l = m+1;
112         }
113     }
114     return -1;
115 }
116 
117 int main()
118 {
119     scanf("%d%d",&n,&m);
120     make(1,n+1,1);
121     for(int i=1;i<=n;i++)
122     {
123         scanf("%d",a+i);
124         b[i] = a[i];
125     }
126     sort(a+1,a+n+1);
127 
128     int st,ed,k;
129     for(int i=0;i<m;i++)
130     {
131         scanf("%d%d%d",&st,&ed,&k);
132         if(st>ed)swap(st,ed);
133         segs[i].s = st;
134         segs[i].t = ed;
135         segs[i].k = k;
136         segs[i].index = i;
137     }
138     sort(segs,segs+m,cmp);
139     int prest = 0,preed =0;
140     for(int i=0;i<m;i++)
141     {
142         for(int j=prest;j<=min(segs[i].s-1,preed);j++)
143         {
144             if(j!=0)
145             {
146                  del(b_search(b[j]),1);
147             }
148         }
149         for(int j=max(preed+1,segs[i].s);j<=segs[i].t;j++)
150         {
151             insert(b_search(b[j]),1);
152         }
153          cal(segs[i].k,1);
154          res[segs[i].index] = ans;
155          prest = segs[i].s;
156          preed = segs[i].t;
157 
158     }
159     for(int i=0;i<m;i++)
160     {
161         printf("%d\n",res[i]);
162     }
163     return 0;
164 }
165