Http://acm.timus.ru/problem.aspx? Space = 1 & num = 1613
Hash + binary, where hash can be implemented using map in STL. At first, I didn't notice that the sequence number was ordered !!! So it's OK to use vector.
#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <string.h>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>using namespace std;#define LL long long#define pi acos(-1)#define N 5000#define INF INT_MAX#define eps 1e-8//*********************************************//ural 1613//*********************************************map<int,vector<int> > mp;int main(){ //freopen("a.txt","r",stdin); int n; while(scanf("%d",&n)!=EOF) { int i,j,k; for(i=1;i<=n;i++) { int a; scanf("%d",&a); mp[a].push_back(i); } int m; scanf("%d",&m); while(m--) { int l,r,z; scanf("%d%d%d",&l,&r,&z); int sz=mp[z].size(); if(sz==0) { printf("0"); continue; } int flag=0; int low=0,up=sz-1,mid; while(low<=up) { mid=(low+up)>>1; if(mp[z][mid]>=l && mp[z][mid]<=r) { flag=1; break; } if(mp[z][mid]<l) low=mid+1; if(mp[z][mid]>r) up=mid-1; } if(flag)printf("1"); else printf("0"); } printf("\n"); mp.clear(); } return 0;}