Submission #706595

#TimeUsernameProblemLanguageResultExecution timeMemory
706595YugiHackerPoklon (COCI17_poklon)C++14
140 / 140
1809 ms23408 KiB
#include<bits/stdc++.h>
#define el cout<<"\n"
#define f0(i,n) for(int i=0;i<n;++i)
#define f1(i,n) for(int i=1;i<=n;++i)
#define maxn 500005
using namespace std;
int n, m, block, res;
int a[maxn], b[maxn], d[maxn], ans[maxn];
struct query
{
    int l, r, id;
}q[maxn];
bool cmp (query q1, query q2)
{
    if (q1.l/block == q2.l/block) return q1.r < q2.r;
    return q1.l < q2.l;
}
void sub(int x)
{
    d[a[x]]--;
    if (d[a[x]] == 1) res--;
    if (d[a[x]] == 2) res++;
}
void add(int x)
{
    d[a[x]]++;
    if (d[a[x]] == 2) res++;
    if (d[a[x]] == 3) res--;
}
main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m;
    f1 (i, n) cin >> a[i], b[i] = a[i];
    sort(b+1, b+n+1);
    f1 (i, n) a[i] = lower_bound(b+1, b+n+1, a[i]) - b;
    f1 (i, m)
    {
        int l, r;
        cin >> l >> r;
        q[i] = {l, r, i};
    }
    block = sqrt(n);
    sort(q+1, q+m+1, cmp);
    int L = 0, R = 0;
    f1 (i, m)
    {
        while (L<q[i].l) sub(L++);
        while (L>q[i].l) add(--L);
        while (R<q[i].r) add(++R);
        while (R>q[i].r) sub(R--);
        ans[q[i].id] = res;
    }
    f1 (i, m) cout << ans[i] << '\n';
}

Compilation message (stderr)

poklon.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   30 | main()
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...