Saturday, December 15, 2018

/**
 * Author      : Dipu Kumar Mohanto
 *               CSE, BRUR.
 * Problem     : D. Closest Equals
 * Category    : Data Structures Variations
 * 
 * Verdict     : Accepted
**/
Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : DCP-60: Holloween Party
Source            : Devskill
Category          : Graph Theory
Algorithm         : Sorting Queries
Verdict           : Accepted
  #include "bits/stdc++.h"   using namespace std;   static const int maxn = 6e5 + 5; static const int inf = 1e7 + 7;   struct node { int l, p; node(int l = 0, int p = 0) : l(l), p(p) {} };   int arr[maxn]; map <int, int> mapper; vector <node> allquery[maxn]; int ans[maxn]; int n, q;   int tree[maxn << 2];   inline void update(int node, int a, int b, int pos, int val) { if (a > b || a > pos || b < pos) return; if (a >= pos && b <= pos) { tree[node] = min(tree[node], val); return; } int lft = node << 1; int rgt = lft | 1; int mid = (a + b) >> 1; update(lft, a, mid, pos, val); update(rgt, mid+1, b, pos, val); tree[node] = min(tree[lft], tree[rgt]); }   inline int query(int node, int a, int b, int i, int j) { if (a > b || a > j || b < i) return inf; if (a >= i && b <= j) return tree[node]; int lft = node << 1; int rgt = lft | 1; int mid = (a + b) >> 1; int p = query(lft, a, mid, i, j); int q = query(rgt, mid + 1, b, i, j); return min(p, q); }   int main() { scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); for (int i = 1; i <= q; i++) { int l, r; scanf("%d %d", &l, &r); allquery[r].emplace_back(l, i); } fill(begin(tree), end(tree), inf); for (int i = 1; i <= n; i++) { if (mapper.find(arr[i]) != mapper.end()) { int pos = mapper[ arr[i] ]; int val = i - pos; update(1, 1, n, pos, val); } mapper[ arr[i] ] = i; for (node it : allquery[i]) ans[it.p] = query(1, 1, n, it.l, i); } for (int i = 1; i <= q; i++) printf("%d\n", ans[i] == inf ? -1 : ans[i]); }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.