Ural_1613. For Fans of Statistics(快排+二分)

  思路就是快排+二分查找。有重复的数字,多加了一点小处理。

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 70005;

class node {
public:
int num;
int ord;
} a[N];

bool cmp(node a, node b){
if(a.num == b.num) return a.ord < b.ord;
return a.num < b.num;
}

int bin_search(int t, int n) {
int l, r, mid;
l = 1; r = n;
while(l <= r) {
mid = (l + r) >> 1;
if(t == a[mid].num) return mid;
else if(t < a[mid].num) r = mid-1;
else l = mid+1;
}
return 0;
}

int main() {
//freopen("data.in", "r", stdin);

int n, i, q, l, r, t, d, flag;
while(~scanf("%d", &n)) {
for(i = 1; i <= n; i++) {
scanf("%d", &a[i].num);
a[i].ord = i;
}
stable_sort(a+1, a+n+1, cmp);
scanf("%d", &q);
while(q--) {
scanf("%d%d%d", &l, &r, &t);
i = bin_search(t, n);
d = i; flag = 0;
//避免重复的数字找不到的情况
            while(t == a[d].num && d > 0) {
if(a[d].ord >= l && a[d].ord <= r) {
printf("1"); flag = 1; break;
} else d--;
}
if(flag) continue;
d = i;
while(t == a[d].num && d <= n) {
if(a[d].ord >= l && a[d].ord <= r) {
printf("1"); flag = 1; break;
}
else d++;
}
if(flag) continue;
printf("0");
}
printf("\n");
}
return 0;
}



你可能感兴趣的:(for)