CodeForces - 609F+windows bat script

Keywords: Windows

In fact, the idea is quite simple, but the details are very big...

First of all, we think of sorting frog. Then we use the number size of frog to represent the coordinates of mos. After the discretization, we record the number of the smallest frog corresponding to the point with the line segment tree, that is, the smallest frog of xi is that one.

Add mos according to the time, if there is already frog to eat, then eat, after eating, also maintain the frog can eat further, if not, save the mos with map, note that there may be more than one mos in a location.

The segment tree uses tag, and it must be judged that tag < x is the only way to keep up with the new and adjust tm for 10,000 years.

The deletion posture of map has been thinking for a long time. If you delete it directly, it++ can not go to the next place. So it2=it, it++ can delete it2.

Although it took three nights to read the wrong questions for the first time, neglected the details for the second time, and thought too simply. On the third night, I studied map. However, I learnt a wave of matching under windows this morning to make it.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define maxl 200010
#define inf 2000000001

using namespace std;

long long n,m,cnt;
struct frog{long long x,len,ind;} a[maxl];
long long ref[maxl],mosnum[maxl],mosb[maxl],anscnt[maxl],anslen[maxl];
struct mosq{long long ind,p,b;} mos[maxl];
struct node
{
	long long l,r,x,tag;
}tree[maxl<<2];
typedef pair<long long,long long> resmos;
map <long long,resmos> res;
map <long long,resmos> :: iterator itl,itr,it,it2;

inline long long read()
{
	long long x=0;char ch=getchar();
	while(ch<'0' || ch>'9') ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}

bool cmpa(const frog &x,const frog &y)
{
	return	x.x<y.x;
}

bool cmpm(const mosq &x,const mosq &y)
{
	return x.p<y.p;
}

void build(long long k,long long l,long long r)
{
	tree[k].l=l;tree[k].r=r;tree[k].x=inf;tree[k].tag=0;
	if(l==r)
		return;
	long long mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
}

void prework()
{
	n=read();m=read();
	a[0].x=-1;
	for(long long i=1;i<=n;i++)
		a[i].x=read(),a[i].len=read(),a[i].ind=i;
	sort(a+1,a+1+n,cmpa);
	mos[0].p=-1;
	for(long long i=1;i<=m;i++)
		mos[i].p=read(),mos[i].b=read(),mos[i].ind=i;
	sort(mos+1,mos+1+m,cmpm);
	cnt=0; 
	for(long long i=1;i<=m;i++)
	if(mos[i].p!=mos[i-1].p)
	{
		cnt++;
		ref[cnt]=mos[i].p;
		mosnum[mos[i].ind]=cnt;
		mosb[mos[i].ind]=mos[i].b;
	}
	else
		mosnum[mos[i].ind]=cnt,mosb[mos[i].ind]=mos[i].b;
	build(1,1,cnt);
}

/*long long findmore(long long x)
{
	long long l=1,r=cnt,mid;
	if(ref[cnt]<x)
		return cnt+1;
	while(l+1<r)
	{
		mid=(l+r)>>1;
		if(ref[mid]<x)
			l=mid;
		else
			r=mid;
	}
	if(ref[l]>=x)
		return l;
	else
		return r;
}

long long findless(long long x)
{
	long long l=1,r=cnt,mid;
	if(ref[1]>x)
		return 0;
	while(l+1<r)
	{
		mid=(l+r)>>1;
		if(ref[mid]<x)
			l=mid;
		else
			r=mid;
	}
	if(ref[r]<=x)
		return r;
	else
		return l;
}*/

void gank(long long k)
{
	long long x;
	if(tree[k].tag && tree[k].l<tree[k].r)
	{
		x=tree[k].tag;
		if(tree[k<<1].x>x)
			tree[k<<1].x=x,tree[k<<1].tag=x;
		if(tree[k<<1|1].x>x)
		tree[k<<1|1].x=x,tree[k<<1|1].tag=x;
	}
	tree[k].tag=0;
}

void add(long long k,long long l,long long r,long long x)
{
	gank(k);
	if(tree[k].l==l && tree[k].r==r)
	{
		if(tree[k].x>x)
			tree[k].x=x,tree[k].tag=x;
		return;
	}
	long long mid=(tree[k].l+tree[k].r)>>1;
	if(r<=mid)
		add(k<<1,l,r,x);
	else
	if(l>mid)
		add(k<<1|1,l,r,x);
	else
	{
		add(k<<1,l,mid,x);
		add(k<<1|1,mid+1,r,x);
	}
}

long long find(long long k,long long l)
{
	gank(k);
	if(tree[k].l==tree[k].r)
		return tree[k].x;
	long long mid=(tree[k].l+tree[k].r)>>1;
	if(l<=mid)
		return find(k<<1,l);
	else
		return find(k<<1|1,l);
}

void mainwork()
{
	long long l,r,ind,lastlen;
	for(long long i=1;i<=n;i++)
	{
		l=lower_bound(ref+1,ref+1+cnt,a[i].x)-ref;
		r=upper_bound(ref+1,ref+1+cnt,a[i].x+a[i].len)-ref;
		if(l<r && l>0 && r>0)
			add(1,l,r-1,i);
	}
	resmos d;
	for(long long i=1;i<=m;i++)
	{
		ind=find(1,mosnum[i]);
		if(ind<=n)
		{
			a[ind].len+=mosb[i];
			anscnt[a[ind].ind]++;
			do
			{
				lastlen=a[ind].len;
				itl=res.lower_bound(a[ind].x);
				itr=res.upper_bound(a[ind].x+a[ind].len);
 				for(it=itl;it!=itr;it++)
				{
					d=(*it).second; 
					a[ind].len+=d.first;anscnt[a[ind].ind]+=d.second;
				}
				for(it=itl;it!=itr;)
				{
					it2=it;it++;
					res.erase(it2);
				}
				l=lower_bound(ref+1,ref+1+cnt,a[ind].x)-ref;
				r=upper_bound(ref+1,ref+1+cnt,a[ind].x+a[ind].len)-ref;
				if(l<=r && l>0 && r>0)
					add(1,l,r-1,ind);
			}while(lastlen<a[ind].len);
		}
		else
			if(!res.count(ref[mosnum[i]]))
				res[ref[mosnum[i]]]=make_pair(mosb[i],1);
			else
			{
				d=res[ref[mosnum[i]]];
				d.first+=mosb[i];d.second++;
				res[ref[mosnum[i]]]=d;
			}
	}
}

void print()
{
	for(long long i=1;i<=n;i++)
		anslen[a[i].ind]=a[i].len;
	for(long long i=1;i<=n;i++)
		printf("%I64d %I64d\n",anscnt[i],anslen[i]);
}

int main()
{
//	freopen("in.in","r",stdin);
//	freopen("out.out","w",stdout);
	prework();
	mainwork();
	print();
	return 0;
} 
/*
Counter-shooting program: txt changes bat, and does not need freopen counter-shooting 
:again
make > in.in
F Online < in.in > out.ans
F < in.in > out.out
fc out.out out.ans
if not errorlevel 1 goto again  
pause  
*/

Posted by dm3 on Fri, 08 Feb 2019 06:36:17 -0800