답안 #368390

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
368390 2021-02-20T02:16:02 Z TosakaUCW Poklon (COCI17_poklon) C++17
140 / 140
2237 ms 23604 KB
#include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <math.h>

int read(int x = 0, int f = 0, char ch = getchar())
{
    while ('0' > ch or ch > '9')
        f = ch == '-', ch = getchar();
    while ('0' <= ch and ch <= '9')
        x = x * 10 + (ch ^ 48), ch = getchar();
    return f ? -x : x;
}

const int N = 5e5 + 5;
const int M = 5e5 + 5;

int n, m, size, res;
int a[N], b[N], bel[N], ans[N];
int buk[N];

struct Node
{
    int l, r, id;
    bool friend operator<(Node a, Node b) { return bel[a.l] == bel[b.l] ? a.r < b.r : a.l < b.l; }
} q[M];

void update(int i, int val)
{
    if (val == 1)
    {
        if (buk[a[i]] == 2)
            res--;
        ++buk[a[i]];
        if (buk[a[i]] == 2)
            res++;
    }
    else
    {
        if (buk[a[i]] == 2)
            res--;
        --buk[a[i]];
        if (buk[a[i]] == 2)
            res++;
    }
}

void go()
{
    memcpy(b, a, sizeof a), std::sort(b + 1, b + 1 + n);
    int nn = std::unique(b + 1, b + 1 + n) - b - 1;
    for (int i = 1; i <= n; i++)
        a[i] = std::lower_bound(b + 1, b + 1 + nn, a[i]) - b;
}

int main()
{
    n = read(), m = read(), size = sqrt(n);
    for (int i = 1; i <= n; i++)
        a[i] = read(), bel[i] = (i - 1) / size + 1;
    for (int i = 1; i <= m; i++)
        q[i].l = read(), q[i].r = read(), q[i].id = i;
    std::sort(q + 1, q + 1 + m), go();
    for (int i = 1, L = 1, R = 0; i <= m; ans[q[i++].id] = res)
    {
        while (q[i].l < L)
            update(--L, 1);
        while (R < q[i].r)
            update(++R, 1);
        while (L < q[i].l)
            update(L++, -1);
        while (q[i].r < R)
            update(R--, -1);
    }
    for (int i = 1; i <= m; i++)
        printf("%d\n", ans[i]);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2432 KB Output is correct
2 Correct 2 ms 2284 KB Output is correct
3 Correct 7 ms 2432 KB Output is correct
4 Correct 17 ms 2540 KB Output is correct
5 Correct 222 ms 6508 KB Output is correct
6 Correct 219 ms 6380 KB Output is correct
7 Correct 640 ms 10700 KB Output is correct
8 Correct 1099 ms 15252 KB Output is correct
9 Correct 1624 ms 19276 KB Output is correct
10 Correct 2237 ms 23604 KB Output is correct