Submission #978462

# Submission time Handle Problem Language Result Execution time Memory
978462 2024-05-09T08:44:40 Z sleepntsheep Fish 3 (JOI24_fish3) C
0 / 100
419 ms 41276 KB
#include<stdio.h>
#include<stdlib.h>

#define N 300000

#define Q 300000

int n, q, *eh[N+1], eo[N+1];
long long d,ans[Q];

void push(int i,int j)
{
    int o=eo[i]++;
    if(!o)eh[i]=(int*)malloc(2*sizeof**eh);
    else if(!(o&o-1))eh[i]=(int*)realloc(eh[i],2*o*sizeof**eh);
    eh[i][o]=j;
}


/*
 * sweep from left to right,
 *
 * decompose fishes to dominoes
 *
 * amortized O(n) dominoes move
 *
 *
 * need segtree that can 
 * decrease k from a[l..r], add k to b[l..r]
 * point query a[i]
 * range query b[l..r]
 */

#define N_ (((N+1)<<2)+1)
long long ta[N_], la[N_], tb[N_], lb[N_], c;

int dominoes[N+1], top;

void pul(int v,int l,int r)
{
    long long ww=r-l+1;
    ta[v] = ta[2*v] +  ta[2*v+1] + ww * (la[2*v] + la[2*v+1]);
    tb[v] = tb[2*v] +  tb[2*v+1] + ww * (lb[2*v] + lb[2*v+1]);
}

void add(int x,int y,long long k,long long k2,int v,int l,int r)
{
    if (r < x || y < l) return;
    if (x <= l && r <= y)
    {
        la[v] += k;
        lb[v] += k2;
        return;
    }
    add(x, y, k, k2, 2*v, l, l+(r-l)/2);
    add(x, y, k, k2, 2*v+1, l+(r-l)/2+1, r);
    pul(v, l, r);
}
void add_(int x,int y,long long k,long long k2) { add(x, y, k, k2, 1, 0, n); }

long long query1(int p,int v,int l,int r)
{
    if(l==r)return ta[v] + (r-l+1ll) * la[v];
    return la[v] * (r-l+1ll) + (p <= l+(r-l)/2 ? query1(p,2*v,l,l+(r-l)/2): query1(p,2*v+1,l+(r-l)/2+1,r));
}
long long query1_(int p) { return query1(p, 1, 0, n); }

long long query2(int x,int y,int v,int l,int r)
{
    if(r<x||y<l)return 0;
    if (x<=l&&r<=y) return lb[v] * (r-l+1ll) + tb[v];
    return lb[v] * (r-l+1ll) + query2(x, y, 2*v, l, l+(r-l)/2) + query2(x, y, 2*v+1, l+(r-l)/2+1, r);
}
long long query2_(int x,int y) { return query2(x, y, 1, 0, n); }

int broken = 0;

void fix(int r)
{
    long long delta = query1_(r-1) - query1_(r);
    if (delta <= 0)
        return;
    int ii = (delta + d - 1) / d;
    if (query1_(dominoes[top]) < d * ii)
    {
        int lower = dominoes[top], upper = r;
        while (upper - lower > 1)
        {
            int mid = lower + (upper - lower) / 2;
            if (query1_(mid) < d * ii) lower = mid;
            else upper = mid;
        }
        if (lower > broken)
            broken = lower;
    }
    add_(dominoes[top], r-1, ii * d, ii);
}

int main()
{
    scanf("%d%lld",&n,&d);
    add_(0, 0, -1e18, 0);
    for(int i=1;i<=n;++i)scanf("%lld",&c), add_(i, i, c, 0);

    scanf("%d",&q);
    for(int ll,rr,i=0;i<q;++i)scanf("%d%d",&ll,&rr),push(rr,i),push(rr,ll);

    for(int r=1;r<=n;++r)
    {
        fix(r);

        while (top && query1_(dominoes[top]) - query1_(dominoes[top]-1) < d)
            fix(dominoes[top--]);

        if (query1_(r) - query1_(r-1) >= d)
            dominoes[++top] = r;

        for(int j=0;j<eo[r];j+=2)
        {
            int ll=eh[r][j+1], ii=eh[r][j];
            if (broken >= dominoes[top])
                ans[ii] = -1;
            else
                ans[ii] = query2_(ll, r);
        }
    }

    for(int i=0;i<q;++i)printf("%lld\n",ans[i]);
}

Compilation message

Main.c: In function 'push':
Main.c:15:18: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   15 |     else if(!(o&o-1))eh[i]=(int*)realloc(eh[i],2*o*sizeof**eh);
      |                 ~^~
Main.c: In function 'main':
Main.c:101:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |     scanf("%d%lld",&n,&d);
      |     ^~~~~~~~~~~~~~~~~~~~~
Main.c:103:26: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |     for(int i=1;i<=n;++i)scanf("%lld",&c), add_(i, i, c, 0);
      |                          ^~~~~~~~~~~~~~~~
Main.c:105:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |     scanf("%d",&q);
      |     ^~~~~~~~~~~~~~
Main.c:106:31: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |     for(int ll,rr,i=0;i<q;++i)scanf("%d%d",&ll,&rr),push(rr,i),push(rr,ll);
      |                               ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Incorrect 1 ms 10588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 419 ms 41276 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 108 ms 18300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 381 ms 31140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Incorrect 1 ms 10588 KB Output isn't correct
4 Halted 0 ms 0 KB -