func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 500010, INF = MAXN + 1;   struct Query {     int pos, l, r;   };   struct AddEvent {     int pos, value, start_valid;   };   int n, m;   int as[MAXN];   int close[MAXN];   Query queries[MAXN];   AddEvent add_events[MAXN];   int results[MAXN];   int st[4 * MAXN];   void update(int v, int l, int r, int ind, int val) {     if (l == r) {       assert(st[v] == INF);       st[v] = val;     } else {       int m = (l + r) >> 1;       if (ind <= m)         update(v + v, l, m, ind, val);       else         update(v + v + 1, m + 1, r, ind, val);       st[v] = min(st[v + v], st[v + v + 1]);     }   }   int get(int v, int l, int r, int tl, int tr) {     assert(l <= tl && tr <= r && tl <= tr);     if (l == tl && r == tr) return st[v];     int res = INF, m = (l + r) >> 1;     if (tl <= m) res = get(v + v, l, m, tl, min(tr, m));     if (tr > m) res = min(res, get(v + v + 1, m + 1, r, max(m + 1, tl), tr));     return res;   }   int main() {     for (int i = 0; i < int(4 * MAXN); ++i) st[i] = INF;     scanf( %d%d , &n, &m);     for (int i = 0; i < int(n); ++i) scanf( %d , &as[i]);     for (int i = 0; i < int(m); ++i) {       scanf( %d%d , &queries[i].l, &queries[i].r);       --queries[i].l, --queries[i].r;       queries[i].pos = i;     }     sort(queries, queries + m, [](Query a, Query b) { return a.r < b.r; });     map<int, int> last;     for (int i = n - 1; i >= 0; i--) {       auto it = last.find(as[i]);       if (it == last.end()) {         close[i] = INF;       } else {         close[i] = it->second - i;       }       last[as[i]] = i;     }     for (int i = 0; i < int(n); ++i) {       add_events[i] = {i, close[i], i + close[i]};     }     sort(add_events, add_events + n,          [](AddEvent a, AddEvent b) { return a.start_valid < b.start_valid; });     int qp = 0, ap = 0;     for (int i = 0; i < n; i++) {       while (ap < n && add_events[ap].start_valid == i) {         update(1, 0, n - 1, add_events[ap].pos, add_events[ap].value);         ++ap;       }       while (qp < m && queries[qp].r == i) {         results[queries[qp].pos] = get(1, 0, n - 1, queries[qp].l, queries[qp].r);         ++qp;       }     }     for (int i = 0; i < int(m); ++i) {       if (results[i] == INF) results[i] = -1;       printf( %d n , results[i]);     }     return 0;   }   