#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 200005
#define B 635
#define V 4005
int n, t, na, nc, S, id = 1;
struct line { int l, r, w; } a[N], c[N], o[N];
int e[B][B], f[B][B], we[B][B], wf[B][B], sz[N];
int compare_length(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()
{
for (; nc;) a[na++] = c[--nc];
qsort(a, na, sizeof *a, compare_length);
for (int i = 0; i <= na / B; ++i) sz[i] = 0;
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(void)
{
scanf("%d%d", &n, &t);
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 == V) rebuild();
++S;
}
else if (op == 2)
{
struct line del = o[x];
del.w = -1;
c[nc++] = del;
if (nc == V) rebuild();
--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]-1; 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]-1; 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.cpp: In function 'int main()':
segments.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d%d", &n, &t);
| ~~~~~^~~~~~~~~~~~~~~~
segments.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &op, &x);
| ~~~~~^~~~~~~~~~~~~~~~~
segments.cpp:61:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d", &y);
| ~~~~~^~~~~~~~~~
segments.cpp:78:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d%d", &y, &k); z=0;
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
14 ms |
4640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1844 ms |
15296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3331 ms |
15044 KB |
Output is correct |
2 |
Correct |
3325 ms |
14852 KB |
Output is correct |
3 |
Correct |
3339 ms |
14856 KB |
Output is correct |
4 |
Correct |
3357 ms |
14860 KB |
Output is correct |
5 |
Execution timed out |
5043 ms |
14228 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1018 ms |
14844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
14 ms |
4640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
14 ms |
4640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |