func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 100005, SQRT = 550, Q = 100005;   struct Query {     int l, r, id;     Query() {}     Query(int l, int r, int id) : l(l), r(r), id(id) {}     bool operator<(const Query& q) const {       if (l / SQRT != q.l / SQRT) return l / SQRT < q.l / SQRT;       return r > q.r;     }   } queries[Q];   int arr[N], n, k;   int freq[2000005], answer[Q], mo_distinct, mo_has_special;   deque<int> nums[N];   map<int, int> freq_ratio[N];   inline void add_elem(int x) {     if (++freq[x] == 1) ++mo_distinct;   }   inline void rem_elem(int x) {     if (freq[x]-- == 1) --mo_distinct;   }   inline void add_freq_ratio(int x, int diff) {     bool before = freq_ratio[x].size() <= 1;     ++freq_ratio[x][diff];     bool after = freq_ratio[x].size() == 1;     if (!before && after) ++mo_has_special;     if (before && !after) --mo_has_special;   }   inline void rem_freq_ratio(int x, int diff) {     bool before = freq_ratio[x].size() == 1;     --freq_ratio[x][diff];     if (freq_ratio[x][diff] == 0) freq_ratio[x].erase(diff);     bool after = freq_ratio[x].size() <= 1;     if (!before && after) ++mo_has_special;     if (before && !after) --mo_has_special;   }   void add_begin(int idx, int x) {     add_elem(x);     if (!nums[x].empty()) {       int diff = nums[x].front() - idx;       add_freq_ratio(x, diff);     } else {       ++mo_has_special;     }     nums[x].push_front(idx);   }   void add_end(int idx, int x) {     add_elem(x);     if (!nums[x].empty()) {       int diff = idx - nums[x].back();       add_freq_ratio(x, diff);     } else {       ++mo_has_special;     }     nums[x].push_back(idx);   }   void rem_begin(int x) {     rem_elem(x);     int front = nums[x].front();     nums[x].pop_front();     if (!nums[x].empty()) {       int diff = nums[x].front() - front;       rem_freq_ratio(x, diff);     } else {       --mo_has_special;     }   }   void rem_end(int x) {     rem_elem(x);     int back = nums[x].back();     nums[x].pop_back();     if (!nums[x].empty()) {       int diff = back - nums[x].back();       rem_freq_ratio(x, diff);     } else {       --mo_has_special;     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &arr[i]);     int q;     scanf( %d , &q);     for (int i = 0; i < q; ++i) {       int l, r;       scanf( %d %d , &l, &r);       --l;       --r;       queries[i] = Query(l, r, i);     }     sort(queries, queries + q);     mo_has_special = mo_distinct = 0;     int l = 0, r = 0;     add_end(0, arr[0]);     for (int i = 0; i < q; ++i) {       while (r < queries[i].r) {         ++r;         add_end(r, arr[r]);       }       while (l > queries[i].l) {         --l;         add_begin(l, arr[l]);       }       while (l < queries[i].l) rem_begin(arr[l++]);       while (r > queries[i].r) rem_end(arr[r--]);       if (mo_has_special != 0) {         answer[queries[i].id] = mo_distinct;       } else {         answer[queries[i].id] = mo_distinct + 1;       }     }     for (int i = 0; i < q; ++i) printf( %d n , answer[i]);     return 0;   }   