"Problem solution": Set Theory

Problems B: Set Theory S U B S E T

Time limit: 1 Sec   Memory Limit: 512 MB

Face questions


He declined to publicly face the question.

answer


It seems to be directly simulated by the array.

But I felt the bitset & operation can be the perfect solution to the problem intersection, completely ignored the impact of bitset median time complexity.

base: For each element of the insert, adding a first base (with a negative value) and then inserted into the bitset.

And set: For each element inserted directly determined force exists or not in the current bitset, there is no accumulation into the answer, and set to exist.

Intersection: First answer zero, and create a new bitset. For each element inserted, violence is still strong sentence exists or not in the bitset, throw away does not exist, then there is cumulative into the answer.

For this new bitset, and the original merger bitset There are three ways: 1. Scroll. (The best choice) 2 assignment. (T85) 3. Vigorously phase. (Congratulations T fly).

At the same time plus 1: two options: 1.bitset overall right. (Congratulations T fly) 2.base--. (Excellent algorithm)

While reducing a contrast to the above.

So bitset bit arithmetic, and a bit about. 2e6 this case is still very slow.

Code:

 

#include<bits/stdc++.h>
#define read(A) A=init()
#define rint register int
using namespace std;
char xch,xB[1<<15],*xS(xB),*xTT(xB);
#define getc() (xS==xTT&&(xTT=(xS=xB)+fread(xB,1,1<<15,stdin),xS==xTT)?0:*xS++)
inline int init()
{
	int x=0,f=1;char ch=getc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getc();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getc();}
	return x*f;
}
int m,siz,wei,opt,sum,ai,extra;
long long ans;
bitset <2000004> bit[2];
main()
{
	read(m);wei=1000000;extra=1000000;
	int now=1;
	for(rint i=1;i<=m;++i)
	{
		read(opt);
		if(opt==1)
		{
			read(sum);
			while(sum--)
			{
				read(ai);
				if(!bit[now][ai+wei])
				{
					ans+=ai,++siz;
					bit[now][ai+wei]=1;
				}
			}
			printf("%lld\n",ans);
		}
		else if(opt==2)
		{
			ans=siz=0;
			read(sum);
			while(sum--)
			{
				read(ai);
				if(bit[now][ai+wei])
				{
					bit[now^1].set(ai+extra);
					ans+=ai,++siz;
				}
			}
			wei=extra;
			bit[now].reset();
			now^=1;
			printf("%lld\n",ans);
		}
		else if(opt==3){--wei;ans+=siz;printf("%lld\n",ans);}
		else{++wei;ans-=siz;printf("%lld\n",ans);}
	}
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/xingmi-weiyouni/p/11711326.html