#include <stdio.h>
#define INLINE inline __attribute__((always_inline))
#define assert_(x) if (!(x)) __builtin_trap()
#define N 200001
unsigned seed = 0x8321798;
INLINE unsigned rand_(void)
{
return seed = ((seed << 3) ^ 0x23a8f91c);
}
int A[N], B[N], S[N], L[N], R[N], D[N], AT;
INLINE int treap(int x)
{
A[++AT] = x; B[AT] = rand_(); S[AT] = 1;
return AT;
}
INLINE void pull(int v)
{
S[v] = 1 + S[L[v]] + S[R[v]];
}
INLINE void push(int v)
{
if (v && D[v])
{
A[v] += D[v];
if (L[v])
{
D[L[v]] += D[v];
D[R[v]] += D[v];
}
D[v] = 0;
}
}
void split(int v, int *l, int *r, int val)
{
if (!v) { *l=*r=0; return; }
push(v);
if (A[v] <= val)
{
split(R[v], R+v, r, val);
*l = v;
}
else
{
split(L[v], l, L+v, val);
*r = v;
}
pull(v);
}
void spliti(int v, int *l, int *r, int ind)
{
if (!v) { *l=*r=0; return; }
push(v);
if (S[L[v]] < ind)
{
spliti(R[v], R+v, r, ind - S[L[v]] - 1);
*l = v;
}
else
{
spliti(L[v], l, L+v, ind);
*r = v;
}
pull(v);
}
void merge(int *v, int l, int r)
{
if (!l || !r) { *v = l^r; return; }
push(l), push(r);
if (B[l] < B[r])
{
merge(L+r, l, L[r]);
*v = r;
}
else
{
merge(R+l, R[l], r);
*v = l;
}
pull(*v);
}
void join(int *v, int l, int r)
{
if (!l || !r) { *v = l^r; return; }
if (B[l] < B[r]) { int T = l; l = r; r = T; }
push(l), push(r);
int y1, y2;
split(l, &y1, &y2, A[r]);
join(R+r, R[r], y2);
join(L+r, L[r], y1);
*v = r;
pull(*v);
}
int order(int v, int k)
{
if (!v) return 0;
if (A[v] < k)
return 1 + S[L[v]] + order(R[v], k);
return order(L[v], k);
}
int n, q, t;
char op;
void print(int v)
{
if (!v) return;
print(L[v]);
printf("%d ", A[v]);
print(R[v]);
}
INLINE void fertilize(int s, int h)
{
int j = order(t, h);
int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
spliti(t, &y1, &y2, j);
if (0)
{
printf(" Y1(%d) : ", S[y1]); print(y1); puts("");
printf(" Y2(%d) : ", S[y2]); print(y2); puts("");
}
spliti(y2, &y3, &y4, s);
if (0)
{
printf(" Y3(%d) : ", S[y3]); print(y3); puts("");
printf(" Y4(%d) : ", S[y4]); print(y4); puts("");
puts("\n");
}
++D[y3];
push(y3);
join(&y2, y3, y4);
merge(&t, y1, y2);
}
INLINE void query(int x, int y)
{
printf("%d\n", order(t, y+1) - order(t, x));
}
int main(void)
{
scanf("%d%d", &n, &q);
for (int x, i = 0; i < n; ++i) scanf("%d", &x), join(&t, t, treap(x));
for (int x, y, i = 0; i < q; ++i)
{
scanf(" %c%d%d", &op, &x, &y);
if (op == 'F') fertilize(x, y);
else query(x, y);
}
return 0;
}
Compilation message
grow.cpp: In function 'int main()':
grow.cpp:167:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
grow.cpp:168:41: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | for (int x, i = 0; i < n; ++i) scanf("%d", &x), join(&t, t, treap(x));
| ~~~~~^~~~~~~~~~
grow.cpp:172:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
172 | scanf(" %c%d%d", &op, &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1063 ms |
6284 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
96 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
260 ms |
3228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
716 ms |
4564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
980 ms |
3752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1041 ms |
4856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1059 ms |
4120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
5176 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
565 ms |
4436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |