Advertisement
MaximCherchuk

Ex

Mar 4th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int M = 70000 + 10;
  6. int all[M], n, len;
  7. vector<unordered_set<int>> s;
  8.  
  9. void init() {
  10.     scanf("%d", &n);
  11.     len = sqrt(n + .01) + 1;
  12.     s.resize(len);
  13.     for (int i = 0, t; i < n; ++i) {
  14.         scanf("%d", all + i);
  15.     }
  16.     for (int i = 0; i < n; ++i) {
  17.         s[i / len].insert(all[i]);
  18.     }
  19. }
  20.  
  21. inline int __attribute__((always_inline)) query(int a, int b, int number) {
  22.     if(a == b) {
  23.         if(all[a] == number) {
  24.             return 1;
  25.         }
  26.         return 0;
  27.     }
  28.     int cl = a / len, cr = b / len;
  29.     if (cl == cr) {
  30.         for (int i = a; i <= b; ++i) {
  31.             if (all[i] == number) {
  32.                 return 1;
  33.             }
  34.         }
  35.         return 0;
  36.     }
  37.  
  38.     for (int i = cl + 1; i <= cr - 1; ++i) {
  39.         if (s[i].find(number) != s[i].end()) {
  40.             return 1;
  41.         }
  42.     }
  43.    
  44.     for (int i = a, end = (cl + 1) * len - 1; i <= end; ++i) {
  45.         if (all[i] == number) {
  46.             return 1;
  47.         }
  48.     }
  49.     for (int i = cr * len; i <= b; ++i) {
  50.         if (all[i] == number) {
  51.             return 1;
  52.         }
  53.     }
  54.     return 0;
  55. }
  56.  
  57. void solve() {
  58.     int queries, a, b, number;
  59.     scanf("%d", &queries);
  60.     for (int i = 0; i < queries; ++i) {
  61.         scanf("%d%d%d", &a, &b, &number);
  62.         printf("%d", query(a - 1, b - 1, number));
  63.     }
  64. }
  65.  
  66. int main() {
  67.     init();
  68.     solve();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement