原题链接:
方法很多,我用的是快排+二分,练习一下stl lower_bound这个函数。。。1 #include2 #include 3 #include 4 #include 5 #include 6 using std::sort; 7 using std::lower_bound; 8 const int Max_N = 100010; 9 struct node {10 int v, id;11 }rec[Max_N];12 int arr[Max_N];13 bool cmp(node &a, node &b) {14 if (a.v == b.v) return a.id < b.id;15 return a.v < b.v;16 }17 int main() {18 #ifdef LOCAL19 freopen("in.txt", "r", stdin);20 freopen("out.txt", "w+", stdout);21 #endif22 int n, m, a, b, pos;23 while (~scanf("%d %d", &n, &m)) {24 for (int i = 0; i < n; i++) {25 scanf("%d", &rec[i].v);26 rec[i].id = i + 1;27 }28 sort(rec, rec + n, cmp);29 for (int i = 0; i < n; i++) arr[i] = rec[i].v;30 while (m--) {31 scanf("%d %d", &a, &b);32 pos = lower_bound(arr, arr + n, b) - arr;33 node &k = rec[pos + a - 1];34 if (k.v == b) printf("%d\n", k.id);35 else puts("0");36 }37 }38 return 0;39 }