QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#196342 | #2831. Game Theory | CSU2023# | TL | 0ms | 0kb | C++14 | 2.8kb | 2023-10-01 16:05:55 | 2023-10-01 16:05:56 |
answer
#include<bits/stdc++.h>
#define LL long long
#define mk make_pair
#define pii pair<int,int>
#define fi first
#define se second
using std::cin;
using std::cout;
using std::ios;
using std::max;
using std::min;
using std::swap;
const int N = 4e5 + 3;
const LL mod = 998244353;
char s[N];
int n,q;
struct Tree{
int t;
struct node{
int lc,rc;
int tag;
LL sum[2];
LL psum[2];
}a[N << 1];
inline void pushup(int u){
for(int i = 0;i < 2;++i){
a[u].sum[i] = a[a[u].lc].sum[i] + a[a[u].rc].sum[i];
a[u].psum[i] = a[a[u].lc].psum[i] + a[a[u].rc].psum[i];
}
}
inline void pushdown(int u){
if(!a[u].tag) return ;
a[a[u].lc].tag ^= 1;
a[a[u].rc].tag ^= 1;
swap(a[a[u].lc].sum[0],a[a[u].lc].sum[1]);
swap(a[a[u].lc].psum[0],a[a[u].lc].psum[1]);
swap(a[a[u].rc].sum[0],a[a[u].rc].sum[1]);
swap(a[a[u].rc].psum[0],a[a[u].rc].psum[1]);
a[u].tag = 0;
}
inline void build(int &u,int l,int r){
if(!u) u = ++t;
a[u].lc = a[u].rc = a[u].tag = a[u].sum[0] = a[u].sum[1] = a[u].psum[1] = a[u].psum[0] = 0;
if(l == r){
a[u].sum[s[l] - '0'] = 1;
a[u].psum[s[l] - '0'] = l;
return ;
}
int mid = (l + r) >> 1;
build(a[u].lc,l,mid);
build(a[u].rc,mid + 1,r);
pushup(u);
}
inline void updata(int u,int l,int r,int ll,int rr){
if(l == ll && r == rr){
swap(a[u].sum[0],a[u].sum[1]);
swap(a[u].psum[0],a[u].psum[1]);
a[u].tag ^= 1;
return ;
}
pushdown(u);
int mid = (l + r) >> 1;
if(rr <= mid) updata(a[u].lc,l,mid,ll,rr);
else if(ll > mid) updata(a[u].rc,mid + 1,r,ll,rr);
else updata(a[u].lc,l,mid,ll,mid),updata(a[u].rc,mid + 1,r,mid + 1,rr);
pushup(u);
}
inline LL query(int u,int l,int r,int ll,int rr,int w){
if(ll > rr) return 0;
if(l == ll && r == rr) return a[u].psum[w];
pushdown(u);
int mid = (l + r) >> 1;
if(rr <= mid) return query(a[u].lc,l,mid,ll,rr,w);
else if(ll > mid) return query(a[u].rc,mid + 1,r,ll,rr,w);
else return query(a[u].lc,l,mid,ll,mid,w) + query(a[u].rc,mid + 1,r,mid + 1,rr,w);
}
inline LL get1(int u){
return a[u].sum[1];
}
inline int getpos(int u,int l,int r,int x){
if(l == r) return a[u].sum[0] ? 0 : 1;
pushdown(u);
int mid = (l + r) >> 1;
if(x <= mid) return getpos(a[u].lc,l,mid,x);
else return getpos(a[u].rc,mid + 1,r,x);
}
}T;
int main(){
while(scanf("%d%d",&n,&q)){
int rt = 0;
scanf("%s",s + 1);
T.build(rt,1,n);
while(q--){
int l ,r;
scanf("%d%d",&l,&r);
T.updata(rt,1,n,l,r);
int s1 = T.get1(rt);
int p = T.getpos(rt,1,n,s1);
if(!s1) printf("%lld\n",(T.query(rt,1,n,s1 + 1,n,1) * 2 - T.query(rt,1,n,1,s1 - 1,0) - s1) % mod);
else printf("%lld\n",(T.query(rt,1,n,s1 + 1,n,1) * 2 + s1 - T.query(rt,1,n,1,s1 - 1,0)) % mod);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 2 010 1 2 2 3 5 1 00000 1 5
output:
1 3 5 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 ...