社区讨论

为什么 TLE45pts?

P14973『GTOI - 2D』木棍参与者 1已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mlh9k166
此快照首次捕获于
2026/02/11 08:00
上周
此快照最后确认于
2026/02/12 17:50
上周
查看原帖
错在 Subtask 4,6,7,仿题解第一篇。
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int mod=998244353;
int n,q;
long long jc[N],inv[N];
string s;
long long qpow(long long a,long long b){
	long long ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod,b>>=1;
	}
	return ans;
}
void init(){
    jc[0]=1;
	for(int i=1;i<=N-5;i++) jc[i]=jc[i-1]*i%mod;
	inv[N-5]=qpow(jc[N-5],mod-2);
	for(int i=N-6;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
long long C(int n,int m){
	if(m<0||m>n) return 0;
	return jc[n]*inv[m]%mod*inv[n-m]%mod;
}
long long work_V(int len1,int len2){
    if((len1-len2)%2) return 0;
    int now=(len1-len2)/2;
    return (C(len1,now)-C(len1,now-1)+mod)%mod;
}
struct node{
    int cnt1,cnt0,r1,r0;
    bool flag;
}tree[N<<1];
void pushup(int p){
    node A=tree[p<<1],B=tree[p<<1|1];
    tree[p].cnt1=A.cnt1+B.cnt1-min(A.cnt0,B.cnt1);
    tree[p].cnt0=A.cnt0+B.cnt0-min(A.cnt0,B.cnt1);
    tree[p].r1=A.r1+B.r1-min(A.r0,B.r1);
    tree[p].r0=A.r0+B.r0-min(A.r0,B.r1);
    tree[p].flag=0;
}
void pushdown(int p){
    if(tree[p].flag==0) return ;
    swap(tree[p<<1].cnt1,tree[p<<1].r1);
    swap(tree[p<<1].cnt0,tree[p<<1].r0);
    tree[p<<1].flag=!tree[p<<1].flag;
    swap(tree[p<<1|1].cnt1,tree[p<<1|1].r1);
    swap(tree[p<<1|1].cnt0,tree[p<<1|1].r0);
    tree[p<<1|1].flag=!tree[p<<1|1].flag;
    tree[p].flag=0;
}
void build(int p,int l,int r,string s){
    if(l==r){
        if(s[l-1]=='1') tree[p]={1,0,0,1,0};
        else tree[p]={0,1,1,0,0};
        return ;
    }
    int mid=l+r>>1;
    build(p<<1,l,mid,s);build(p<<1|1,mid+1,r,s);
    pushup(p);
}
void update(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y){
        swap(tree[p].cnt1,tree[p].r1);
        swap(tree[p].cnt0,tree[p].r0);
        tree[p].flag=!tree[p].flag;
        return ;
    }
    pushdown(p);
    int mid=l+r>>1;
    if(x<=mid) update(p<<1,l,mid,x,y);
    if(y>mid) update(p<<1|1,mid+1,r,x,y);
    pushup(p);
}
node query(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y) return tree[p];
    pushdown(p);
    int mid=l+r>>1;
    node L={0,0,0,0,0},R={0,0,0,0,0};
    if(x<=mid) L=query(p<<1,l,mid,x,y);
    if(y>mid) R=query(p<<1|1,mid+1,r,x,y);
    if(L.cnt0==0&&L.cnt1==0) return R;
    if(R.cnt0==0&&R.cnt1==0) return L;
    node ans;
    ans.cnt1=L.cnt1+R.cnt1-min(L.cnt0,R.cnt1);
    ans.cnt0=L.cnt0+R.cnt0-min(L.cnt0,R.cnt1);
    ans.r1=L.r1+R.r1-min(L.r0,R.r1);
    ans.r0=L.r0+R.r0-min(L.r0,R.r1);
    ans.flag=0;
    return ans;
}
signed main(){
	scanf("%d",&n);cin>>s;
    init();build(1,1,n,s);
	scanf("%d",&q);
	while(q--){
		int op,l,r;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1) update(1,1,n,l,r);
		else{
            node ss=query(1,1,n,l,r);
            printf("%lld\n",work_V(r-l+1,ss.cnt1+ss.cnt0));
        }
	}
    return 0;
}
豆包对比无果,求大佬解答。

回复

1 条回复,欢迎继续交流。

正在加载回复...