Submission #874421

# Submission time Handle Problem Language Result Execution time Memory
874421 2023-11-17T00:27:35 Z sleepntsheep Growing Trees (BOI11_grow) C++17
10 / 100
1000 ms 6188 KB
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define INLINE inline __attribute__((always_inline))
#define assert_(x) if (!(x)) __builtin_trap()
 
#define N 100005
 
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];
        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;
 
    push(v);
    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)
{
    push(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);
    spliti(y2, &y3, &y4, s);
 
 
    ++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)
{
    srand(time(NULL));
    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:149:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
grow.cpp:150:41: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |     for (int x, i = 0; i < n; ++i) scanf("%d", &x), join(&t, t, treap(x));
      |                                    ~~~~~^~~~~~~~~~
grow.cpp:154:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |         scanf(" %c%d%d", &op, &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1101 ms 6188 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 8 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 7 ms 548 KB Output is correct
5 Correct 69 ms 636 KB Output is correct
6 Correct 119 ms 848 KB Output is correct
7 Correct 9 ms 344 KB Output is correct
8 Execution timed out 1057 ms 992 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1046 ms 1340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1008 ms 1332 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1102 ms 4084 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1064 ms 2380 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1030 ms 1876 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 601 ms 2644 KB Output is correct
2 Execution timed out 1073 ms 2632 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1044 ms 4444 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 262 ms 3320 KB Output is correct