给定一串数字,一个区间和一个数x,问区间内是否有一个数正好等于x。
#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
map<int, vector<int> > m;
vector<int> ans;
int main(int argc, char const *argv[]) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int val;
scanf("%d", &val);
m[val].push_back(i);
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
if (m.find(x) != m.end()) {
vector<int>::iterator it = lower_bound(m[x].begin(), m[x].end(), l);
if (it != m[x].end() && *it <= r) {
ans.push_back(1);
} else {
ans.push_back(0);
}
} else {
ans.push_back(0);
}
}
for (int i = 0; i < ans.size(); i++) {
printf("%d", ans[i]);
}
putchar('\n');
return 0;
}