QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#235412#2831. Game Theoryyiyiyi#WA 53ms3960kbC++144.3kb2023-11-02 18:55:212023-11-02 18:55:22

Judging History

你现在查看的是最新测评结果

  • [2023-11-02 18:55:22]
  • 评测
  • 测评结果:WA
  • 用时:53ms
  • 内存:3960kb
  • [2023-11-02 18:55:21]
  • 提交

answer

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<set> 
#define ll long long
#define lowbit(x) x&(-x)
#define mp make_pair
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define forE(i,x) for(int i=head[x];i;i=nxt[i])
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int maxn=2e5+5;
const int mod=998244353;
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+(c-'0');
		c=getchar();
	}
	return x*f;
}

char c[maxn];

struct SegTree{
	struct node{
		int l,r;
        ll num[2],sum[2],add;
	}t[maxn<<2];
	inline void spread(int p)
	{
		if(t[p].add)
		{
			t[p*2].add^=t[p].add;
			swap(t[p*2].num[0],t[p*2].num[1]);
            swap(t[p*2].sum[0],t[p*2].sum[1]);
            t[p*2+1].add^=t[p].add;
			swap(t[p*2+1].num[0],t[p*2+1].num[1]);
            swap(t[p*2+1].sum[0],t[p*2+1].sum[1]);
			t[p].add=0;
		}
	}
	inline void pushup(int p)
	{
        t[p].sum[0]=t[p*2].sum[0]+t[p*2+1].sum[0];
        t[p].sum[1]=t[p*2].sum[1]+t[p*2+1].sum[1];
        t[p].num[0]=t[p*2].num[0]+t[p*2+1].num[0];
        t[p].num[1]=t[p*2].num[1]+t[p*2+1].num[1];
    }
	inline void build(int p,int l,int r)
	{
		t[p].l=l;t[p].r=r;t[p].add=0,t[p].sum[0]=t[p].sum[1]=t[p].num[0]=t[p].num[1]=0;
		if(l==r)
		{
            int x=c[l]-'0';
            t[p].num[x]++;
            t[p].sum[x]+=l;
			return;
		}
		int mid=(t[p].l+t[p].r)/2;
		build(p*2,l,mid);
		build(p*2+1,mid+1,r);
		pushup(p);
	}
	inline void change(int p,int l,int r)
	{
		if(t[p].l>=l&&t[p].r<=r)
		{
			swap(t[p].num[0],t[p].num[1]);
            swap(t[p].sum[0],t[p].sum[1]);
			t[p].add^=1;
			return;
		}
		spread(p);
		int mid=(t[p].l+t[p].r)/2;
		if(l<=mid) change(p*2,l,r);
		if(r>mid) change(p*2+1,l,r);
		pushup(p);
	}
	inline ll asknum(int p,int l,int r,int v)
	{
        if(l>r) return 0;
		if(t[p].l>=l&&t[p].r<=r)
		{
			return t[p].num[v];
		}
		spread(p);
		int mid=(t[p].l+t[p].r)/2;
		ll res=0;
		if(l<=mid) res+=asknum(p*2,l,r,v);
		if(r>mid) res+=asknum(p*2+1,l,r,v);
		return res;
	}
	inline ll asksum(int p,int l,int r,int v)
	{
        if(l>r) return 0;
		if(t[p].l>=l&&t[p].r<=r)
		{
			return t[p].sum[v];
		}
		spread(p);
		int mid=(t[p].l+t[p].r)/2;
		ll res=0;
		if(l<=mid) res+=asksum(p*2,l,r,v);
		if(r>mid) res+=asksum(p*2+1,l,r,v);
		return res;
	}    
    inline ll get1(int p,int v)
    {
        // printf("%lld %lld\n",p,v);
        if(t[p].l==t[p].r) return t[p].l;
        spread(p);
        int mid=(t[p].l+t[p].r)/2;
        if(t[p*2+1].num[1]>=v) return get1(p*2+1,v);
        else return get1(p*2,v-t[p*2+1].num[1]);
    }
    inline ll get0(int p,int v)
    {
        // printf("%lld %lld\n",p,v);
        if(t[p].l==t[p].r) return t[p].l;
        spread(p);
        int mid=(t[p].l+t[p].r)/2;
        if(t[p*2].num[0]>=v) return get0(p*2,v);
        else return get0(p*2+1,v-t[p*2].num[0]);
    }
}S; 
int n,q;

signed main()
{
    while(scanf("%d %d",&n,&q) != EOF)
    {
        scanf("%s",c+1);
        S.build(1,1,n);
        rep(i,1,q)
        {
            int p1=read(),p2=read();
            S.change(1,p1,p2);
            int tot = S.asknum(1,1,n,0);
            if(tot==0) {printf("%d\n",n);continue;}
            if(tot==n) {printf("0\n");continue;}  
            int l=1,r=n,res=-1;
            // printf("1: %lld\n",tot);
            while(l<=r)
            {
                int mid=(l+r)/2;
                int x=S.asknum(1,1,mid,0),y=S.asknum(1,mid+1,n,1);
                if(x!=0&&x<=y) res=mid,l=mid+1;
                else r=mid-1;
            }
            if(res==-1) {printf("%lld\n",S.asknum(1,1,n,1));continue;}
            ll num=S.asknum(1,1,res,0);
            ll L=S.get0(1,num);
            ll R=S.get1(1,num);
            // printf("3: %lld\n",res);
            ll ans=S.asksum(1,1,L,1);
            // printf("4: %lld %lld %lld\n",num,L,R);
            ans+=(S.asksum(1,R,n,1)-S.asksum(1,1,L,0)+num)*2ll-num;
            printf("%lld\n",ans);
        }
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3880kb

input:

3 2
010
1 2
2 3
5 1
00000
1 5

output:

1
3
5

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3876kb

input:

1 1
0
1 1

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 26ms
memory: 3960kb

input:

2 2
01
2 2
2 2
2 2
01
1 2
1 2
1 2
1
1 1
1 1
1 2
1
1 1
1 1
2 2
00
1 2
1 2
2 2
11
1 2
1 2
2 2
01
2 2
1 1
2 2
10
2 2
1 2
2 2
01
1 2
1 2
1 2
0
1 1
1 1
2 2
01
1 2
2 2
1 2
0
1 1
1 1
1 2
1
1 1
1 1
2 2
10
1 2
1 1
1 2
0
1 1
1 1
2 2
01
1 2
1 2
2 2
10
1 2
1 1
1 2
0
1 1
1 1
1 2
0
1 1
1 1
1 2
1
1 1
1 1
2 2
10
1 ...

output:

0
3
1
3
0
1
0
1
2
0
0
2
0
1
2
0
1
3
1
0
1
2
1
0
0
1
3
2
1
0
1
3
3
2
1
0
1
0
0
1
0
1
0
2
2
1
0
1
2
1
1
0
2
0
2
3
1
0
0
1
2
0
0
1
0
1
0
1
1
0
1
2
2
0
0
2
0
1
0
1
1
0
1
0
1
0
0
1
0
1
0
1
2
0
3
0
1
0
0
1
1
0
1
0
1
2
0
2
1
0
0
3
1
2
0
1
3
2
1
0
0
1
2
0
2
0
0
1
0
1
1
0
1
0
1
0
1
0
1
2
1
0
3
1
0
3
0
1
0
1
...

result:

ok 200000 lines

Test #4:

score: -100
Wrong Answer
time: 53ms
memory: 3804kb

input:

11 20
00010111000
1 6
1 11
5 6
8 11
1 4
3 8
4 11
1 7
5 9
1 4
6 10
5 6
1 7
5 10
1 10
9 11
6 8
1 4
8 11
1 4
10 20
0101000010
3 4
2 2
4 8
4 6
6 7
3 7
3 3
1 5
1 5
6 8
2 2
2 4
2 6
5 7
1 3
2 5
6 8
7 9
5 8
3 10
4 20
1011
2 4
1 4
1 3
2 3
1 1
2 2
1 2
2 4
1 2
3 4
3 4
3 4
1 4
2 2
1 4
1 3
1 1
1 3
1 3
2 2
4 20
1...

output:

23
58
51
25
5
19
48
54
38
33
31
41
57
50
45
27
25
30
53
41
17
20
30
40
38
39
45
30
45
26
17
31
17
17
25
22
24
4
15
34
2
10
3
6
10
7
6
2
0
10
0
10
2
1
7
6
7
4
7
7
4
7
1
5
7
3
5
7
3
7
6
7
6
7
6
6
2
0
5
3
111
116
125
100
128
148
96
78
91
96
131
141
141
127
127
63
75
91
53
53
41
44
21
21
32
71
67
32
35
...

result:

wrong answer 1st lines differ - expected: '16', found: '23'