Submission #868109

# Submission time Handle Problem Language Result Execution time Memory
868109 2023-10-30T12:43:42 Z sleepntsheep Segments (IZhO18_segments) C++17
16 / 100
878 ms 10588 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native")

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define N 200005
int V;

int n, t, na, nc, S, id = 1;
struct line { int l, r, w; } a[N], c[N], o[N];
int e[N], f[N], we[N], wf[N], pf[N], sz[7000];

int compare_length(const void *a0, const void *b0)
{
    const struct line *a = (const struct line*)a0, *b = (const struct line*)b0;
    if (a->r - a->l != b->r - b->l) return ((a->r - a->l) - (b->r - b->l));
    if (a->l != b->l) return a->l - b->l;
    return a->w - b->w;
}

int compare_left_increasing_index(const void *a0, const void *b0)
{
    const int aa = *(const int*)a0, bb = *(const int*)b0;
    if (a[aa].l != a[bb].l) return a[aa].l - a[bb].l;
    if (a[aa].r != a[bb].r) return a[aa].r - a[bb].r;
    return a[aa].w - a[bb].w;
}

int compare_right_increasing_index(const void *a0, const void *b0)
{
    const int aa = *(const int*)a0, bb = *(const int*)b0;
    if (a[aa].r != a[bb].r) return a[aa].r - a[bb].r;
    if (a[aa].l != a[bb].l) return a[aa].l - a[bb].l;
    return a[aa].w - a[bb].w;
}

inline void rebuild()
{
    for (; nc;) a[na++] = c[--nc];
    qsort(a, na, sizeof *a, compare_length);
    memset(sz, 0, sizeof *sz * (na / V + 1));
    for (int i = 0; i < na; ++i) e[i] = f[i] = i, ++sz[i/V];
    pf[0] = a[0].w;
    for (int i = 1; i < na; ++i) pf[i] = pf[i-1] + a[i].w;
    for (int i = 0; i * V <= na; ++i)
    {
        qsort(e+i*V, sz[i], sizeof *e, compare_left_increasing_index);
        qsort(f+i*V, sz[i], sizeof *f, compare_right_increasing_index);

        we[i*V+sz[i]-1] = a[e[i*V+sz[i]-1]].w;
        for (int j = i*V+sz[i]-1; --j >= i*V;) we[j] = we[j+1] + a[e[j]].w;
        wf[i*V] = a[f[i*V]].w;
        for (int j = i*V+1; j < i*V+sz[i]; ++j) wf[j] = wf[j-1] + a[f[j]].w;
    }
}

int main(void)
{
    scanf("%d%d", &n, &t);
    V = sqrt(n * log2(n));

    for (int z = 0, lastans = 0, op, x, y, k, l, r; n--;)
    {
        scanf("%d%d", &op, &x);
        switch (op)
        {
            case 1:
                scanf("%d", &y);
                l = x ^ (t * lastans), r = y ^ (t * lastans);
                if (l>r) { int t=l;l=r;r=t;}
                o[id++] = c[nc++] = (struct line){l, r, 1};
                if (nc == V) rebuild();
                ++S;
                break;
            case 2:
                c[nc++] = (struct line){o[x].l, o[x].r, -1};
                if (nc == V) rebuild();
                --S;
                break;
            case 3:
                scanf("%d%d", &y, &k); z=0;
                l = x ^ (t * lastans), r = y ^ (t * lastans);
                if (l>r) { int t=l;l=r;r=t;}
                /* count in static */
                {
                    int Z = -1;
                    for (int L = 0, R = na-1; L <= R; )
                    {
                        int M = (L+R)/2;
                        if (a[M].r - a[M].l + 1 >= k) R = M - 1, Z = M;
                        else L = M + 1;
                    }
                    if (Z != -1)
                    {
                        z -= (Z>0) * pf[Z-1];
                        int zb = Z/V, sb = zb+1;
                        for (struct line *j = a + Z; j < a + sb * V; ++j) z -= (j->r < l + k - 1 || j->l > r - k + 1) * j->w;
                        for (int j = sb; j * V <= na; ++j)
                        {
                            /* r - j->l + 1 < k === j->l > r - k + 1 */
                            int cnt = 0;
                            for (int L = 0, R = sz[j]-1; L <= R;)
                            {
                                int M = (L+R)/2;
                                if (a[e[j*V+M]].l > r - k + 1) R = M - 1, cnt = we[j*V+M];
                                else L = M + 1;
                            }
                            z -= cnt;
                            cnt = 0;
                            /* j->r - l + 1 < k === j->l < k + l - 1 */
                            for (int L = 0, R = sz[j]-1; L <= R;)
                            {
                                int M = (L+R)/2;
                                if (a[f[j*V+M]].r < k + l - 1) L = M + 1, cnt = wf[j*V+M];
                                else R = M - 1;
                            }
                            z -= cnt;
                        }
                    }
                }

                /* count in buffer */
                for (struct line *j = c; j < c + nc; ++j) z -= (j->r < l + k - 1 || j->l > r - k + 1) * j->w;
                lastans = (z += S);
                printf("%d\n", z);
                break;
        }
    }
    return 0;
}

Compilation message

segments.cpp: In function 'int main()':
segments.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |     scanf("%d%d", &n, &t);
      |     ~~~~~^~~~~~~~~~~~~~~~
segments.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         scanf("%d%d", &op, &x);
      |         ~~~~~^~~~~~~~~~~~~~~~~
segments.cpp:71:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |                 scanf("%d", &y);
      |                 ~~~~~^~~~~~~~~~
segments.cpp:84:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |                 scanf("%d%d", &y, &k); z=0;
      |                 ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8548 KB Output is correct
3 Incorrect 6 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 531 ms 9584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 480 ms 9784 KB Output is correct
2 Correct 480 ms 9668 KB Output is correct
3 Correct 498 ms 10148 KB Output is correct
4 Correct 481 ms 9700 KB Output is correct
5 Correct 752 ms 9652 KB Output is correct
6 Correct 673 ms 9708 KB Output is correct
7 Correct 722 ms 9560 KB Output is correct
8 Correct 857 ms 10584 KB Output is correct
9 Correct 874 ms 10588 KB Output is correct
10 Correct 814 ms 10064 KB Output is correct
11 Correct 515 ms 10052 KB Output is correct
12 Correct 821 ms 10512 KB Output is correct
13 Correct 732 ms 10036 KB Output is correct
14 Correct 628 ms 10188 KB Output is correct
15 Correct 596 ms 10036 KB Output is correct
16 Correct 554 ms 9936 KB Output is correct
17 Correct 590 ms 9336 KB Output is correct
18 Correct 603 ms 9392 KB Output is correct
19 Correct 631 ms 9376 KB Output is correct
20 Correct 600 ms 9408 KB Output is correct
21 Correct 542 ms 9780 KB Output is correct
22 Correct 700 ms 10032 KB Output is correct
23 Correct 760 ms 10312 KB Output is correct
24 Correct 728 ms 9956 KB Output is correct
25 Correct 474 ms 9680 KB Output is correct
26 Correct 489 ms 9672 KB Output is correct
27 Correct 484 ms 9780 KB Output is correct
28 Correct 485 ms 9724 KB Output is correct
29 Correct 776 ms 10060 KB Output is correct
30 Correct 780 ms 10172 KB Output is correct
31 Correct 878 ms 10536 KB Output is correct
32 Correct 818 ms 10260 KB Output is correct
33 Correct 768 ms 10252 KB Output is correct
34 Correct 600 ms 9984 KB Output is correct
35 Correct 705 ms 10176 KB Output is correct
36 Correct 763 ms 10268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 477 ms 9764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8548 KB Output is correct
3 Incorrect 6 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8548 KB Output is correct
3 Incorrect 6 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -