#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 200005
#define B 450
int n, t, b, na, nc, S, id = 1;
struct line { int l, r, w; } a[N], c[B], o[N];
int e[B][B], f[B][B], we[B][B], wf[N][N], sz[N];
int compare_length_descending(const void *a0, const void *b0)
{
const struct line *a = (const struct line*)a0, *b = (const struct line*)b0;
return ((a->r - a->l) - (b->r - b->l));
}
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;
return a[aa].r - a[bb].r;
}
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;
return a[aa].l - a[bb].l;
}
void rebuild_c()
{
for (; nc;) a[na++] = c[nc--];
qsort(a, na, sizeof *a, compare_length_descending);
for (int i = 0; i < na; ++i)
{
e[i/b][sz[i/b]] = f[i/b][sz[i/b]] = i, ++sz[i/b];
}
for (int i = 0; i <= (na / b); ++i)
{
qsort(e[i], sz[i], sizeof **e, compare_left_increasing_index);
qsort(f[i], sz[i], sizeof **f, compare_right_increasing_index);
we[i][sz[i]] = 0;
for (int j = sz[i] - 1; j >= 0; --j)
we[i][j] = we[i][j+1] + a[e[i][j]].w;
wf[i][0] = a[f[i][0]].w;
for (int j = 1; j < sz[i]; ++j)
wf[i][j] = wf[i][j-1] + a[f[i][j]].w;
}
}
int main()
{
scanf("%d%d", &n, &t);
b = ceil(sqrt(n) * log2(n));
for (int z = 0, lastans = 0, op, x, y, k, l, r; n--;)
{
scanf("%d%d", &op, &x);
if (op == 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 == b) rebuild_c();
++S;
}
else if (op == 2)
{
struct line del = o[x];
del.w = -1;
c[nc++] = del;
if (nc == b) rebuild_c();
--S;
}
else
{
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)
{
int zb = Z/b, sb = zb+1;
for (struct line *j = a + Z; j < a + sb * b; ++j) if (j->r - l + 1 < k || r - j->l + 1 < k) z -= j->w;
for (int j = sb; j * sb <= na; ++j)
{
/* r - j->l + 1 < k === j->l > r - k + 1 */
int cnt = 0;
for (int L = 0, R = sz[j]; L <= R;)
{
int M = (L+R)/2;
if (a[e[j][M]].l > r - k + 1) R = M - 1, cnt = we[j][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]; L <= R;)
{
int M = (L+R)/2;
if (a[f[j][M]].r < k + l - 1) L = M + 1, cnt = wf[j][M];
else R = M - 1;
}
z -= cnt;
}
}
}
/* count in buffer */
for (struct line *j = c; j < c + nc; ++j)
if (j->r - l + 1 < k || r - j->l + 1 < k) z -= j->w;
z += S;
lastans = z;
printf("%d\n", z);
}
}
return 0;
}
Compilation message
segments.c: In function 'main':
segments.c:54:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d%d", &n, &t);
| ^~~~~~~~~~~~~~~~~~~~~
segments.c:59:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d%d", &op, &x);
| ^~~~~~~~~~~~~~~~~~~~~~
segments.c:62:13: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d", &y);
| ^~~~~~~~~~~~~~~
segments.c:79:13: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | scanf("%d%d", &y, &k); z=0;
| ^~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libc.a(assert.o): in function `__assert_fail_base':
(.text+0x114): relocation truncated to fit: R_X86_64_PC32 against symbol `__abort_msg' defined in .bss section in /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libc.a(abort.o)
/usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libc.a(loadmsgcat.o): in function `_nl_load_domain':
(.text+0x3c): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x5a): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x61): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x67): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x73): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x48a): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x4a1): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x4ac): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x4c2): relocation truncated to fit: R_X86_64_PC32 against `.bss'
(.text+0x510): additional relocation overflows omitted from the output
/usr/bin/ld: /tmp/cclZ6QYz.o: in function `main':
segments.c:(.text.startup+0x64): undefined reference to `log2'
/usr/bin/ld: segments.c:(.text.startup+0x53d): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status