fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAX = 1e5 + 5;
  5. int n, q, arr[MAX], sqroot;
  6. vector<int> Block[400];
  7.  
  8. bool Query(int L, int R, int val) {
  9. int LB = L / sqroot;
  10. int RB = R / sqroot;
  11. int found = 0;
  12.  
  13. if(LB == RB) {
  14. for(int i = L; i <= R; i++)
  15. found += arr[i] == val;
  16. return found;
  17. }
  18.  
  19. int tmp = (LB + 1) * sqroot;
  20. for(int i = L; i < tmp; i++)
  21. found += arr[i] == val;
  22.  
  23. tmp = RB * sqroot;
  24. for(int i = tmp; i <= R; i++)
  25. found += arr[i] == val;
  26.  
  27. for(int i = LB + 1; i < RB; i++) {
  28. tmp = binary_search(Block[i].begin(), Block[i].end(), val);
  29. found += tmp;
  30. }
  31. return found;
  32. }
  33.  
  34. int main() {
  35. scanf("%d", &n);
  36.  
  37. sqroot = sqrt(n);
  38. for(int i = 0; i < n; i++) {
  39. scanf("%d", arr + i);
  40. Block[i / sqroot].push_back(arr[i]);
  41. }
  42.  
  43. for(int i = 0;i <= sqroot; i++)
  44. sort(Block[i].begin(), Block[i].end());
  45.  
  46. scanf("%d", &q);
  47. string ans = "";
  48. while(q--) {
  49. int L, R, X;
  50. scanf("%d %d %d", &L, &R, &X);
  51. --L; --R;
  52. ans += Query(L, R, X) ? "1" : "0";
  53. }
  54. cout<<ans;
  55. return 0;
  56. }
Success #stdin #stdout 0s 4384KB
stdin
5
1234567 666666 3141593 666666 4343434
5
1 5 3141593
1 5 578202
2 4 666666
4 4 7135610
1 1 1234567
stdout
10101