Pagini recente » Cod sursa (job #214447) | Cod sursa (job #1153237) | Cod sursa (job #2609974) | Cod sursa (job #65628) | Cod sursa (job #2244731)
#include <bits/stdc++.h>
using namespace std;
ifstream in("pq.in");
ofstream out("pq.out");
const int N_MAX = 1e5, Q_MAX = 1e5, A_MAX = 99999;
struct Query
{
int st, dr, ind;
bool operator <(const Query& other) const
{
if(st == other.st)
return dr < other.dr;
return st < other.st;
}
};
int n, m;
int a[N_MAX + 2];
Query q[Q_MAX + 2];
int ans[Q_MAX + 2], last[A_MAX + 2];
int aib[N_MAX + 2];
void update(int pos, int val)
{
while(pos <= n)
{
aib[pos] = max(aib[pos], val);
pos += pos & (-pos);
}
}
int query(int pos)
{
int ans = 0;
while(pos)
{
ans = max(ans, aib[pos]);
pos -= pos & (-pos);
}
return ans;
}
int main()
{
in >> n >> m;
for(int i = 1; i <= n; i++)
in >> a[i];
for(int i = 1; i <= m; i++)
{
in >> q[i].st >> q[i].dr;
q[i].ind = i;
}
sort(q + 1, q + m + 1);
int j = m;
for(int i = n; i >= 1; i--)
{
if(last[a[i]])
update(last[a[i]], last[a[i]] - i);
last[a[i]] = i;
while(q[j].st == i)
{
int val = query(q[j].dr);
if(val == 0)
val = -1;
ans[q[j].ind] = val;
j--;
}
}
for(int i = 1; i <= m; i++)
out << ans[i] << '\n';
return 0;
}