QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#245985#2831. Game Theorynameless_story#WA 35ms3488kbC++202.1kb2023-11-10 15:06:582023-11-10 15:06:59

Judging History

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

  • [2023-11-10 15:06:59]
  • 评测
  • 测评结果:WA
  • 用时:35ms
  • 内存:3488kb
  • [2023-11-10 15:06:58]
  • 提交

answer

#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
template<class T1,class T2> bool cmin(T1 &x,const T2 &y) { if (y<x) { x=y; return 1; }return 0; }
template<class T1,class T2> bool cmax(T1 &x,const T2 &y) { if (x<y) { x=y; return 1; }return 0; }
#define all(x) (x).begin(),(x).end()
struct node
{
	ll s1,s0;
	int c1,c0;
	void rev() { swap(s1,s0); swap(c1,c0); }
	node operator+(const node &o) const { return {s1+o.s1,s0+o.s0,c1+o.c1,c0+o.c0}; }
};
const int M=8e5+5;
node s[M];
bool lz[M];
int *b;
int z,y;
void build(int x,int l,int r)
{
	lz[x]=0;
	if (l==r)
	{
		if (b[l]) s[x]={l,0,1,0};
		else s[x]={0,l,0,1};
		return;
	}
	int c=x*2,m=l+r>>1;
	build(c,l,m); build(c+1,m+1,r);
	s[x]=s[c]+s[c+1];
}
void modify(int x,int l,int r)
{
	if (z<=l&&r<=y)
	{
		s[x].rev();
		lz[x]^=1;
		return;
	}
	int c=x*2,m=l+r>>1;
	if (lz[x])
	{
		lz[c]^=1;
		lz[c+1]^=1;
		s[c].rev();
		s[c+1].rev();
		lz[x]=0;
	}
	if (z<=m) modify(c,l,m);
	if (y>m) modify(c+1,m+1,r);
	s[x]=s[c]+s[c+1];
}
node tmp;
void ask(int x,int l,int r)
{
	if (z<=l&&r<=y)
	{
		// s[x].rev();
		// lz[x]^=1;
		tmp=tmp+s[x];
		return;
	}
	int c=x*2,m=l+r>>1;
	if (lz[x])
	{
		lz[c]^=1;
		lz[c+1]^=1;
		s[c].rev();
		s[c+1].rev();
		lz[x]=0;
	}
	if (z<=m) ask(c,l,m);
	if (y>m) ask(c+1,m+1,r);
}
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	int n,m,i,j;
	while (cin>>n>>m)
	{
		vector<int> a(n+1);
		{
			string s;
			cin>>s;
			for (i=1; i<=n; i++) a[i]=s[i-1]&1;
		}
		b=a.data();
		build(1,1,n);
		while (m--)
		{
			int l,r;
			cin>>l>>r;
			z=l; y=r;
			modify(1,1,n);
			// for (i=l; i<=r; i++) a[i]^=1;
			// for (i=1; i<=n; i++) cerr<<a[i]; cerr<<endl;
			// int c=count(all(a),1);
			int c=s[1].c1;
			ll ans=0;
			{
				z=c; y=n; tmp={0,0,0,0};
				ask(1,1,n);
				ans+=tmp.s1;
				// cerr<<ans<<endl;
			}
			if (c>1)
			{
				z=1; y=c-1; tmp={0,0,0,0};
				ask(1,1,n);
				ans-=tmp.s0;
				// cerr<<ans<<endl;
			}
			// for (i=c; i<=n; i++) ans+=a[i]*i;
			// for (i=1; i<c; i++) ans-=(!a[i])*i;
			cout<<ans<<'\n';
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3468kb

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: 3456kb

input:

1 1
0
1 1

output:

1

result:

ok single line: '1'

Test #3:

score: -100
Wrong Answer
time: 35ms
memory: 3488kb

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
2
1
2
0
1
0
1
2
0
0
2
0
1
2
0
1
2
1
0
1
2
1
0
0
1
2
2
1
0
1
2
2
2
1
0
1
0
0
1
0
1
0
2
2
1
0
1
2
1
1
0
2
0
2
2
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
2
0
1
0
0
1
1
0
1
0
1
2
0
2
1
0
0
2
1
2
0
1
2
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
2
1
0
2
0
1
0
1
...

result:

wrong answer 2nd lines differ - expected: '3', found: '2'