답안 #343534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343534 2021-01-04T08:05:56 Z ogibogi2004 원숭이와 사과 나무 (IZhO12_apple) C++14
0 / 100
912 ms 262148 KB
#include<bits/stdc++.h>
using namespace std;
const int INF=1e9;
struct tree
{
	struct node
	{
		node *l,*r;
		int minval,cnt;
		int L,R;
		int lazy;
		node(int _l,int _r)
		{
			l=nullptr;
			r=nullptr;
			minval=0;cnt=_r-_l+1;
			L=_l;R=_r;
		}
	};
	node* root=nullptr;
	node* xd;
	node* merge(node *n1,node *n2)
	{
		if(!n1)return n2;
		if(!n2)return n1;
		node *ret=new node(n1->L,n2->R);
		if(n1->minval==n2->minval)
		{
			tie(ret->minval,ret->cnt)=make_pair(n1->minval,n1->cnt+n2->cnt);
		}
		else tie(ret->minval,ret->cnt)=min(make_pair(n1->minval,n1->cnt),make_pair(n2->minval,n2->cnt));
		ret->l=n1;ret->r=n2;
		return ret;
	}
	node* push(node *t,int L,int R)
	{
		if(!t)t=new node(L,R);
		if(t->lazy>=1)
		{
			if(t->L!=t->R)
			{
				int mid=(t->L+t->R)/2;
				if(t->l)
				{
					t->l->lazy+=t->lazy;
				}
				else 
				{
					t->l=new node(L,mid);
					t->l->lazy+=t->lazy;
				}
				if(t->r)
				{
					t->r->lazy+=t->lazy;
				}
				else 
				{
					t->r=new node(mid+1,R);
					t->r->lazy+=t->lazy;
				}
			}
			t->minval+=t->lazy;
			t->lazy=0;
		}
		return t;
	}
	node* upd(node *t,int L,int R,int ql,int qr)
	{
		t=push(t,L,R);
		if(L>qr||R<ql)return t;
		if(L>=ql&&R<=qr)
		{
			t->lazy=1;
			t=push(t,L,R);
			return t;
		}
		int mid=(L+R)/2;
		t->l=upd(t->l,L,mid,ql,qr);
		t->r=upd(t->r,mid+1,R,ql,qr);
		t=merge(t->l,t->r);
		return t;
	}
	node* query(node *t,int L,int R,int ql,int qr)
	{	
		//cout<<L<<" "<<R<<" "<<ql<<" "<<qr<<endl;
		t=push(t,L,R);
		if(L>qr||R<ql)return NULL;
		if(L>=ql&&R<=qr)
		{
			return t;
		}
		int mid=(L+R)/2;
		return merge(query(t->l,L,mid,ql,qr),query(t->r,mid+1,R,ql,qr));
	}
	void print(node* t)
	{
		if(!t)return;
		cout<<t->L<<" "<<t->R<<" "<<t->minval<<" "<<t->cnt<<endl;
		print(t->l);
		print(t->r);
	}
}st;
int main()
{
	int c=0;
	int m,d,x,y;
	cin>>m;
	while(m--)
	{
		cin>>d>>x>>y;
		x+=c;y+=c;
		if(d==1)
		{
			st.xd=st.query(st.root,0,INF,x,y);
			if(st.xd->minval>0)
			{
				c=y-x+1;
			}
			else c=y-x+1-st.xd->cnt;
			cout<<c<<endl;
		}
		else st.root=st.upd(st.root,0,INF,x,y);
		//st.print(st.root);
	}
return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 57 ms 21484 KB Output is correct
5 Correct 69 ms 26128 KB Output is correct
6 Correct 69 ms 26348 KB Output is correct
7 Correct 70 ms 26092 KB Output is correct
8 Correct 505 ms 156012 KB Output is correct
9 Runtime error 912 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
10 Halted 0 ms 0 KB -