Submission #16771730


Source Code Expand

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class SVAL, class SFUN> struct segtree_rg{
  int N;
  int logN;
  SVAL*val;
  SFUN*fun;
  void malloc(int maxN, int once = 0){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    val = new SVAL[2*i];
    fun = new SFUN[i];
    if(once){
      setN(maxN);
    }
  }
  void walloc(int maxN, int once = 0, void **mem = &wmem){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    walloc1d(&val, 2*i, mem);
    walloc1d(&fun, i, mem);
    if(once){
      setN(maxN);
    }
  }
  void free(void){
    delete [] val;
    delete [] fun;
  }
  SVAL& operator[](int i){
    return val[N+i];
  }
  void setN(int n, int zerofill = 1, int dobuild = 1){
    int i;
    for(i=1,logN=0;i<n;i*=2,logN++){
      ;
    }
    N = i;
    if(dobuild){
      build();
    }
  }
  void build(void){
    int i;
    for(i=N-1;i;i--){
      segtree_rg_func(val[i], val[2*i], val[2*i+1]);
    }
    int jPV_0s1p = N;
    for(i=(1);i<(jPV_0s1p);i++){
      segtree_rg_id(fun[i]);
    }
  }
  inline void push_one(int a){
    if(2*a < N){
      segtree_rg_func(fun[2*a], fun[a], fun[2*a]);
    }
    segtree_rg_func(val[2*a], fun[a], val[2*a]);
    if(2*a+1 < N){
      segtree_rg_func(fun[2*a+1], fun[a], fun[2*a+1]);
    }
    segtree_rg_func(val[2*a+1], fun[a], val[2*a+1]);
    segtree_rg_id(fun[a]);
  }
  inline void push(int a){
    int i;
    for(i=logN;i;i--){
      push_one(a>>i);
    }
  }
  inline void build(int a){
    while(a > 1){
      a /= 2;
      segtree_rg_func(val[a], val[2*a], val[2*a+1]);
      segtree_rg_func(val[a], fun[a], val[a]);
    }
  }
  inline void change(int a, int b, SFUN f){
    int aa;
    int bb;
    if(a >= b){
      return;
    }
    aa = (a += N);
    bb = (b += N);
    push(a);
    push(b-1);
    if(a%2){
      segtree_rg_func(val[a], f, val[a]);
      a++;
    }
    if(b%2){
      b--;
      segtree_rg_func(val[b], f, val[b]);
    }
    a /= 2;
    b /= 2;
    while(a < b){
      if(a%2){
        segtree_rg_func(val[a], f, val[a]);
        segtree_rg_func(fun[a], f, fun[a]);
        a++;
      }
      if(b%2){
        b--;
        segtree_rg_func(val[b], f, val[b]);
        segtree_rg_func(fun[b], f, fun[b]);
      }
      a /= 2;
      b /= 2;
    }
    build(aa);
    build(bb-1);
  }
  inline SVAL get(int a, int b){
    SVAL res;
    SVAL tmp;
    int fga = 0;
    int fgb = 0;
    a += N;
    b += N;
    push(a);
    push(b-1);
    while(a < b){
      if(a%2){
        if(fga){
          segtree_rg_func(res, res, val[a]);
        }
        else{
          res = val[a];
          fga = 1;
        }
        a++;
      }
      if(b%2){
        b--;
        if(fgb){
          segtree_rg_func(tmp, val[b], tmp);
        }
        else{
          tmp = val[b];
          fgb = 1;
        }
      }
      a /= 2;
      b /= 2;
    }
    if(fga==1 && fgb==0){
      return res;
    }
    if(fga==0 && fgb==1){
      return tmp;
    }
    if(fga==1 && fgb==1){
      segtree_rg_func(res, res, tmp);
      return res;
    }
    return res;
  }
}
;
struct segval{
  int zero;
  int one;
  long long inv;
}
;
struct segfun{
  int r;
}
;
void segtree_rg_id(segfun &res){
  res.r = 0;
}
void segtree_rg_func(segval &res, segfun f, segval a){
  if(f.r){
    res.zero = a.one;
    res.one = a.zero;
    res.inv = (long long) a.one * a.zero - a.inv;
  }
  else{
    res = a;
  }
}
void segtree_rg_func(segval &res, segval a, segval b){
  res.zero = a.zero + b.zero;
  res.one = a.one + b.one;
  res.inv = a.inv + b.inv + (long long) a.one * b.zero;
}
void segtree_rg_func(segfun &res, segfun f, segfun g){
  res.r = f.r ^ g.r;
}
int N;
int Q;
int A[200000];
int T;
int L;
int R;
segtree_rg<segval, segfun> t;
int main(){
  int RZTsC2BF, i;
  wmem = memarr;
  segval v;
  segfun f;
  f.r = 1;
  rd(N);
  rd(Q);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  t.malloc(N);
  t.setN(N, 0, 0);
  for(i=(0);i<(N);i++){
    t[i].zero = 1 - A[i];
    t[i].one = A[i];
    t[i].inv = 0;
  }
  t.build();
  for(RZTsC2BF=(0);RZTsC2BF<(Q);RZTsC2BF++){
    rd(T);
    rd(L);L += (-1);
    rd(R);
    if(T==1){
      t.change(L, R, f);
    }
    else{
      v = t.get(L, R);
      wt_L(v.inv);
      wt_L('\n');
    }
  }
  return 0;
}
// cLay varsion 20200916-1

// --- original code ---
// struct segval{
//   int zero, one;
//   ll inv;
// };
// 
// struct segfun{
//   int r;
// };
// 
// void segtree_rg_id(segfun &res){
//   res.r = 0;
// }
// 
// void segtree_rg_func(segval &res, segfun f, segval a){
//   if(f.r){
//     res.zero = a.one;
//     res.one = a.zero;
//     res.inv = (ll) a.one * a.zero - a.inv;
//   } else {
//     res = a;
//   }
// }
// 
// void segtree_rg_func(segval &res, segval a, segval b){
//   res.zero = a.zero + b.zero;
//   res.one = a.one + b.one;
//   res.inv = a.inv + b.inv + (ll) a.one * b.zero;
// }
// 
// void segtree_rg_func(segfun &res, segfun f, segfun g){
//   res.r = f.r ^ g.r;
// }
// 
// int N, Q, A[2d5], T, L, R;
// segtree_rg<segval, segfun> t;
// {
//   segval v;
//   segfun f; f.r = 1;
// 
//   rd(N,Q,A(N));
//   t.malloc(N);
//   t.setN(N, 0, 0);
//   rep(i,N){
//     t[i].zero = 1 - A[i];
//     t[i].one = A[i];
//     t[i].inv = 0;
//   }
//   t.build();
//   rep(Q){
//     rd(T, L--, R);
//     if(T==1){
//       t.change(L, R, f);
//     } else {
//       v = t.get(L, R);
//       wt(v.inv);
//     }
//   }
// }

Submission Info

Submission Time
Task L - Lazy Segment Tree
User LayCurse
Language C++ (GCC 9.2.1)
Score 100
Code Size 7498 Byte
Status AC
Exec Time 132 ms
Memory 14580 KiB

Compile Error

./Main.cpp: In instantiation of ‘void segtree_rg<SVAL, SFUN>::setN(int, int, int) [with SVAL = segval; SFUN = segfun]’:
./Main.cpp:314:17:   required from here
./Main.cpp:133:24: warning: unused parameter ‘zerofill’ [-Wunused-parameter]
  133 |   void setN(int n, int zerofill = 1, int dobuild = 1){
      |                    ~~~~^~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 22
Set Name Test Cases
Sample 00-sample-01.txt
All 00-sample-01.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt
Case Name Status Exec Time Memory
00-sample-01.txt AC 8 ms 3424 KiB
01-01.txt AC 2 ms 3496 KiB
01-02.txt AC 106 ms 13592 KiB
01-03.txt AC 95 ms 10076 KiB
01-04.txt AC 19 ms 5104 KiB
01-05.txt AC 30 ms 5704 KiB
01-06.txt AC 74 ms 13696 KiB
01-07.txt AC 26 ms 4884 KiB
01-08.txt AC 74 ms 12804 KiB
01-09.txt AC 57 ms 13156 KiB
01-10.txt AC 55 ms 9016 KiB
01-11.txt AC 21 ms 8352 KiB
01-12.txt AC 132 ms 14580 KiB
01-13.txt AC 107 ms 14060 KiB
01-14.txt AC 128 ms 14580 KiB
01-15.txt AC 108 ms 14052 KiB
01-16.txt AC 128 ms 14576 KiB
01-17.txt AC 109 ms 14208 KiB
01-18.txt AC 124 ms 14532 KiB
01-19.txt AC 107 ms 14076 KiB
01-20.txt AC 128 ms 14456 KiB
01-21.txt AC 111 ms 14148 KiB