Files
SH-Quizzes/ACMOJ-1686.cpp
2023-12-23 22:23:48 +08:00

238 lines
6.8 KiB
C++

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cassert>
#include<cstdio>
#define unlikely(x) __builtin_expect(!!(x), 0)
struct char_reader
{
FILE* f; char *buf,*p1,*p2; int size;
char_reader(FILE* fin,int bufsize=1<<16) { f=fin; size=bufsize; p1=p2=0; buf=new char[size]; }
~char_reader() { delete []buf; }
inline int operator()() { return p1==p2&&(p2=(p1=buf)+fread(buf,1,size,f),p1==p2)?EOF:*p1++; }
};
struct char_writer
{
FILE* f; char *buf,*p,*end; int size;
char_writer(FILE* fout,int bufsize=1<<16) { f=fout; size=bufsize; buf=new char[size]; p=buf; end=buf+bufsize; }
~char_writer() { fwrite(buf,p-buf,1,f); delete []buf; }
inline char operator()(char ch)
{
if(unlikely(end==p)) { fwrite(buf,end-buf,1,f); p=buf; }
return *p++=ch;
}
};
char_reader gch(stdin);
char_writer wch(stdout);
template<typename T> inline int read(T& t)
{
bool f=false; int ch; while(ch=gch(),!((ch>='0'&&ch<='9')||ch=='-')) { if(ch==EOF) return 0; }
t=0;
if(ch=='-') f=true,ch=gch(); t=ch^48; while(ch=gch(),ch>='0'&&ch<='9') t=(t<<3)+(t<<1)+(ch^48);
if(f) t=-t;
return 1;
}
inline int read(char &c)
{
c=0; int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; } c=ch;
return 1;
}
inline int read(char *s)
{
int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; }
*s++=ch; while(ch=gch(),!(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')&&ch!=EOF) *s++=ch;
return 1;
} inline int read(const char *s) { return read((char*)s); }
inline int readline(char *s)
{
int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; }
*s++=ch; while(ch=gch(),!(ch=='\n'||ch=='\r')&&ch!=EOF) *s++=ch;
return 1;
} inline int readline(const char *s) { return readline((char*)s); }
template<typename T> inline void write(T t)
{
int stk[20],cnt=0;
if(t==0) { wch('0'); return; } if(t<0) { wch('-'); t=-t; }
while(t>0) { stk[cnt++]=t%10; t/=10; } while(cnt) wch(stk[--cnt]+'0');
}
inline void write(char t) { wch(t); }
inline void write(char *s) { while(*s) wch(*s++); } inline void write(const char *s) { write((char*)s); }
#if __cplusplus >= 201103L
template<typename T,typename... Args> inline int read(T& t,Args&... args) { return read(t)+read(args...); }
template<typename T,typename... Args> inline void write(T t,Args... args) { write(t); write(args...); }
#else
template<typename A_t,typename B_t> inline int read(A_t &a,B_t &b) { return read(a)+read(b); }
template<typename A_t,typename B_t,typename C_t> inline int read(A_t &a,B_t &b,C_t &c) { return read(a)+read(b)+read(c); }
template<typename A_t,typename B_t,typename C_t,typename D_t> inline int read(A_t &a,B_t &b,C_t &c,D_t &d) { return read(a)+read(b)+read(c)+read(d); }
template<typename A_t,typename B_t> inline void write(A_t a,B_t b) { write(a); write(b); }
template<typename A_t,typename B_t,typename C_t> inline void write(A_t a,B_t b,C_t c) { write(a); write(b); write(c); }
template<typename A_t,typename B_t,typename C_t,typename D_t> inline void write(A_t a,B_t b,C_t c,D_t d) { write(a); write(b); write(c); write(d); }
#endif
using namespace std;
typedef long long LL;
const int maxn=1e6+10;
char s[maxn];
class SegmentTree
{
struct Node
{
int num0;
LL IdxSumOfZero;
unsigned char InverseTag,RawData;
}buf[maxn<<3]; int NodeCnt=0;
int Length;
unsigned char mem[maxn];
inline void PushDown(int p,LL L,LL R)
{
if(buf[p].InverseTag==0) return;
buf[p].num0=(R-L+1)-buf[p].num0;
buf[p].InverseTag=0;
buf[p].IdxSumOfZero=R*(R+1)/2-L*(L-1)/2-buf[p].IdxSumOfZero;
if(L==R)
{
buf[p].RawData^=1;
return;
}
buf[p*2].InverseTag^=1;
buf[p*2+1].InverseTag^=1;
}
inline void PushUp(int p,LL L,LL R)
{
LL M=(L+R)>>1;
buf[p].num0=(buf[p*2].InverseTag==0?buf[p*2].num0:M-L+1-buf[p*2].num0)
+(buf[p*2+1].InverseTag==0?buf[p*2+1].num0:(R-(M+1)+1)-buf[p*2+1].num0);
buf[p].IdxSumOfZero=(buf[p*2].InverseTag==0?buf[p*2].IdxSumOfZero:M*(M+1)/2-L*(L-1)/2-buf[p*2].IdxSumOfZero)
+(buf[p*2+1].InverseTag==0?buf[p*2+1].IdxSumOfZero:R*(R+1)/2-M*(M+1)/2-buf[p*2+1].IdxSumOfZero);
}
void build(int p,int L,int R)
{
assert(L<=R);
if(L==R)
{
buf[p].RawData=mem[L];
buf[p].num0+=(mem[L]==0);
if(mem[L]==0) buf[p].IdxSumOfZero=L;
return;
}
int M=(L+R)>>1;
build(p*2,L,M);
build(p*2+1,M+1,R);
PushUp(p,L,R);
}
int QuerySinglePointStatus(int p,int L,int R,int position)
{
PushDown(p,L,R);
if(L==R) return buf[p].RawData;
int M=(L+R)>>1;
if(position<=M) return QuerySinglePointStatus(p*2,L,M,position);
else return QuerySinglePointStatus(p*2+1,M+1,R,position);
}
void InverseBlock(int p,int BL,int BR,int OL,int OR)
{
PushDown(1,BL,BR);
if(OL<=BL&&BR<=OR)
{
buf[p].InverseTag^=1;
return;
}
int M=(BL+BR)>>1;
if(OL<=M) InverseBlock(p*2,BL,M,OL,OR);
if(OR>M) InverseBlock(p*2+1,M+1,BR,OL,OR);
PushUp(p,BL,BR);
}
int QueryAllZero()
{
PushDown(1,1,Length);
return buf[1].num0;
}
LL QueryIdxSumOfZeroInRange(int p,int BL,int BR,int OL,int OR)
{
LL res=0;
PushDown(p,BL,BR);
if(OL<=BL&&BR<=OR) return buf[p].IdxSumOfZero;
int M=(BL+BR)>>1;
if(OL<=M) res+=QueryIdxSumOfZeroInRange(p*2,BL,M,OL,OR);
if(OR>M) res+=QueryIdxSumOfZeroInRange(p*2+1,M+1,BR,OL,OR);
PushUp(p,BL,BR);
return res;
}
public:
void Init(char *s,int n)
{
Length=n;
for(int i=1;i<=Length;i++) mem[i]=s[i-1]-'0';
build(1,1,Length);
}
void InverseRange(int L,int R)
{
if(L>R) return;
// for(int i=L;i<=R;i++) mem[i]^=1;
InverseBlock(1,1,Length,L,R);
}
inline int NumberOfZero()
{
return QueryAllZero();
// int cnt=0,tmp;
// for(int i=1;i<=Length;i++) cnt+=(mem[i]==0);
// if(tmp!=cnt)
// {
// puts("Error In NumberOfZero()");
// printf("%d %d\n",tmp,cnt);
// }
// return cnt;
}
inline LL IdxSumOfZeroInRange(int L,int R)
{
if(L>R) return 0;
return QueryIdxSumOfZeroInRange(1,1,Length,L,R);
// LL res=0;
// for(int i=L;i<=R;i++)
// if(mem[i]==0)
// res+=i;
// return res;
}
inline LL IdxSumOfOneInRange(LL L,LL R)
{
if(L>R) return 0;
return R*(R+1)/2-L*(L-1)/2-QueryIdxSumOfZeroInRange(1,1,Length,L,R);
// LL res=0;
// for(int i=L;i<=R;i++)
// if(mem[i]==1)
// res+=i;
// return res;
}
inline char GetStatus(int position)
{
return '0'+QuerySinglePointStatus(1,1,Length,position);
// return '0'+mem[position];
}
}SgT;
int n,m;
LL Solve()
{
LL res=0;
int PostionOfFirstMove=SgT.NumberOfZero();
LL Right=SgT.IdxSumOfZeroInRange(PostionOfFirstMove+1,n);
LL Left=SgT.IdxSumOfOneInRange(1,PostionOfFirstMove-1);
res=(Right-Left)*2;
if(SgT.GetStatus(PostionOfFirstMove)=='0') res+=PostionOfFirstMove;
else res-=PostionOfFirstMove;
return res;
}
int main()
{
#ifdef local
freopen("pro.in","r",stdin);
#endif
// write(212474836480ll,'\n'); return 0;
read(n,m,s);
SgT.Init(s,n);
write(Solve(),'\n');
for(int i=0;i<m;i++)
{
int l,r; read(l,r);
SgT.InverseRange(l,r);
write(Solve(),'\n');
}
return 0;
}