Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为何我会RE?????

Posted by njh_2001 at 2017-04-07 23:01:42 on Problem 3667
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int n;
int read()
{
	int ans=0; char ch=getchar();
	while(ch>'9'||ch<'0') ch=getchar();
	while(ch<='9'&&ch>='0') 
	{
		ans=ans*10+ch-'0';
		ch=getchar();
	}
	return ans;
}
struct tree
{
	int l,r;
	int s[2];
	int sum;
	int lazy;
	tree* ch[2];
	tree(){ch[0]=ch[1]=NULL; }
	void push_up()
	{
		if(ch[0]!=NULL&&ch[1]!=NULL)
		{
			sum=max(ch[0]->sum,ch[1]->sum);
			sum=max(sum,ch[0]->s[1]+ch[1]->s[0] );
			for(int i=0;i<2;i++)
			{
				s[i]=ch[i]->s[i];
				if(ch[i]->s[i^1]==ch[i]->s[i] ) s[i]+=ch[i^1]->s[i];
			}
		}
	}
	void push_down()
	{
		if(ch[0]!=NULL&&ch[1]!=NULL) ch[0]->lazy=ch[1]->lazy=lazy;
		if(lazy==1) 
		{
			sum=s[1]=s[0]=0;
			if(ch[0]!=NULL&&ch[1]!=NULL)
				for(int i=0;i<2;i++)
				{
					ch[i]->sum=ch[i]->s[0]=ch[i]->s[1]=0;
				}
		}
		if(lazy==0)
		{
			int k=r-l+1;
			sum=s[1]=s[0]=k;
			if(ch[0]!=NULL&&ch[1]!=NULL)	
				for(int i=0;i<2;i++)
				{
					ch[i]->s[0]=ch[i]->sum=ch[i]->s[1]=ch[i]->r-ch[i]->l+1;
				}
		}
		lazy=-1;
	}
}*root;
typedef tree* node;
void build(node &o,int l,int r)
{
	if(o==NULL) o=new tree();
	o->l=l; o->r=r;
	o->lazy=-1;
	if(o->l==o->r)
	{
		o->sum=o->s[0]=o->s[1]=1;
		return ;	
	}
	int mid=(l+r)>>1;
	build(o->ch[0],l,mid);
	build(o->ch[1],mid+1,r);
	o->push_up();
}
void update(node &o,int l,int r,int num)
{
	if(o->lazy!=-1) o->push_down();
	if(o->l==l&&o->r==r)
	{
		o->lazy=num;
		o->push_down();
		return;
	}
	int mid=(o->l+o->r)>>1;
	if(mid>=r) update(o->ch[0],l,r,num);
	else if(mid<l) update(o->ch[1],l,r,num);
	else
	{
		update(o->ch[0],l,mid,num);
		update(o->ch[1],mid+1,r,num);
	}
	o->push_up();
}
int query(node o,int k)
{
	if(o->lazy!=-1) o->push_down();
	int ans=0;
	if(o->sum<k) return ans;
	int mid=(o->l+o->r)>>1;
	if(o->ch[0]->sum>=k) 
	{
		 ans=query(o->ch[0],k);
	}
	else if(o->ch[0]->s[1]!=0&&o->ch[0]->s[1]+o->ch[1]->s[0]>=k) 
	{
		int id=mid-o->ch[0]->s[1]+1;
		update(root,id,id+k-1,1);
		ans=id;
	}
	else 
	{
		ans=query(o->ch[1],k);
	}
	o->push_up();
	return ans;
}
int main()
{
	n=read();
	root=NULL;
	build(root,1,n);
	int m=read();
	for(int i=0;i<m;i++)
	{
		int ord=read();
		if(ord==1)
		{
			int t=read();
			cout<<query(root,t)<<endl;
		}
		else if(ord==2)
		{
			int t1=read(), t2=read();
			update(root,t1,t1+t2-1,0);
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator