제출 #978457

#제출 시각아이디문제언어결과실행 시간메모리
978457sleepntsheepFish 3 (JOI24_fish3)C11
0 / 100
408 ms41300 KiB
#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) if (r-1 > broken) broken = r-1; 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]); }

컴파일 시 표준 에러 (stderr) 메시지

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:92:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     scanf("%d%lld",&n,&d);
      |     ^~~~~~~~~~~~~~~~~~~~~
Main.c:94:26: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |     for(int i=1;i<=n;++i)scanf("%lld",&c), add_(i, i, c, 0);
      |                          ^~~~~~~~~~~~~~~~
Main.c:96:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |     scanf("%d",&q);
      |     ^~~~~~~~~~~~~~
Main.c:97:31: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     for(int ll,rr,i=0;i<q;++i)scanf("%d%d",&ll,&rr),push(rr,i),push(rr,ll);
      |                               ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...