Submission #4557801


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template <typename T,typename E>
struct SegmentTree{
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  using H = function<E(E,E)>;
  int n,height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(F f,G g,H h,T ti,E ei):
    f(f),g(g),h(h),ti(ti),ei(ei){}
  
  void init(int n_){
    n=1;height=0;
    while(n<n_) n<<=1,height++;
    dat.assign(2*n,ti);
    laz.assign(2*n,ei);
  }
  void build(const vector<T> &v){
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  inline T reflect(int k){
    return laz[k]==ei?dat[k]:g(dat[k],laz[k]);
  }
  inline void eval(int k){
    if(laz[k]==ei) return;
    laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
    laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
    dat[k]=reflect(k);
    laz[k]=ei;
  }
  inline void thrust(int k){
    for(int i=height;i;i--) eval(k>>i);
  }
  inline void recalc(int k){    
    while(k>>=1)
      dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1));
  }
  void update(int a,int b,E x){
    thrust(a+=n);
    thrust(b+=n-1);
    for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
      if(l&1) laz[l]=h(laz[l],x),l++;
      if(r&1) --r,laz[r]=h(laz[r],x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int a,T x){
    thrust(a+=n);
    dat[a]=x;laz[a]=ei;
    recalc(a);
  }
  T query(int a,int b){
    thrust(a+=n);
    thrust(b+=n-1);
    T vl=ti,vr=ti;
    for(int l=a,r=b+1;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,reflect(l++));
      if(r&1) vr=f(reflect(--r),vr);
    }
    return f(vl,vr);
  }
};

//INSERT ABOVE HERE
signed main(){
  using ll = long long;
  struct T{
    ll cur,fst;
    T(ll cur,ll fst):cur(cur),fst(fst){}
  };
  struct E{
    ll r,a,b,c;
    E(ll r,ll a,ll b,ll c):r(r),a(a),b(b),c(c){}
    bool operator==(const E &o) const{
      return r==o.r&&a==o.a&&b==o.b&&c==o.c;
    }
  };

  auto f=[](T a,T b){
           return T(max(a.cur,b.cur),max(a.fst,b.fst));
         };

  auto g=[](T a,E b){
           return T(((b.r?a.fst:a.cur)+b.a)/b.b+b.c,a.fst);
         };

  const ll LIM = 1e9;
  auto h=[](E a,E b){
           if(b.r) return b;
           E c(a.r,a.a+a.b*(a.c+b.a),a.b*b.b,0);
           c.c=c.a/c.b+b.c;
           c.a%=c.b;
           if(c.b>LIM){
             c.a=max(0LL,LIM-(c.b-c.a));
             c.b=LIM;
           }
           return c;
         };
  
  T ti(0,0);
  E ei(0,0,1,0);

  ll n,q;
  scanf("%lld %lld",&n,&q);
  vector<ll> a(n);
  for(Int i=0;i<n;i++) scanf("%lld",&a[i]);
  
  SegmentTree<T, E> seg(f,g,h,ti,ei);
  vector<T> v;
  for(Int i=0;i<n;i++) v.emplace_back(a[i],a[i]);
  seg.build(v);
  
  for(Int i=0;i<q;i++){
    Int t,l,r,x;
    scanf("%lld %lld %lld %lld",&t,&l,&r,&x);
    r++;
    if(t==0) seg.update(l,r,E(0,0,1,x));
    if(t==1) seg.update(l,r,E(0,0,x,0));
    if(t==2) printf("%lld\n",seg.query(l,r).cur);
    if(t==3) seg.update(l,r,E(1,0,1,0));
  }
  
  return 0;
}

Submission Info

Submission Time
Task I - ADD DIV MAX RESTORE
User beet
Language C++14 (GCC 5.4.1)
Score 100
Code Size 3311 Byte
Status AC
Exec Time 787 ms
Memory 31980 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:121:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld",&n,&q);
                           ^
./Main.cpp:123:43: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   for(Int i=0;i<n;i++) scanf("%lld",&a[i]);
                                           ^
./Main.cpp:132:45: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld %lld %lld %lld",&t,&l,&r,&x);
                                             ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 33
Set Name Test Cases
Sample example_0, example_1, example_2
All adm_0, adm_1, biasrand_0, biasrand_1, biasrand_2, biasrand_3, example_0, example_1, example_2, maxrand2_0, maxrand2_1, maxrand2_2, maxrand2_3, maxrand2_4, maxrand2_5, maxrand2_6, maxrand2_7, maxrand_0, maxrand_1, smallrand_0, smallrand_1, smallrand_2, smallrand_3, special_0, special_1, swapswap_0, swapswap_1, swapswap_2, swapswap_3, zeroone_0, zeroone_1, zeroone_2, zeroone_3
Case Name Status Exec Time Memory
adm_0 AC 661 ms 29804 KiB
adm_1 AC 607 ms 29804 KiB
biasrand_0 AC 720 ms 31980 KiB
biasrand_1 AC 705 ms 29676 KiB
biasrand_2 AC 574 ms 31724 KiB
biasrand_3 AC 643 ms 29804 KiB
example_0 AC 1 ms 256 KiB
example_1 AC 1 ms 256 KiB
example_2 AC 1 ms 256 KiB
maxrand2_0 AC 664 ms 29932 KiB
maxrand2_1 AC 572 ms 30700 KiB
maxrand2_2 AC 673 ms 29932 KiB
maxrand2_3 AC 551 ms 30700 KiB
maxrand2_4 AC 690 ms 31724 KiB
maxrand2_5 AC 568 ms 30956 KiB
maxrand2_6 AC 721 ms 29932 KiB
maxrand2_7 AC 548 ms 30060 KiB
maxrand_0 AC 787 ms 29676 KiB
maxrand_1 AC 695 ms 29932 KiB
smallrand_0 AC 2 ms 384 KiB
smallrand_1 AC 2 ms 384 KiB
smallrand_2 AC 3 ms 384 KiB
smallrand_3 AC 1 ms 256 KiB
special_0 AC 522 ms 30572 KiB
special_1 AC 519 ms 30316 KiB
swapswap_0 AC 498 ms 30316 KiB
swapswap_1 AC 666 ms 29932 KiB
swapswap_2 AC 508 ms 30188 KiB
swapswap_3 AC 501 ms 29932 KiB
zeroone_0 AC 498 ms 29804 KiB
zeroone_1 AC 463 ms 29932 KiB
zeroone_2 AC 508 ms 30316 KiB
zeroone_3 AC 461 ms 29804 KiB