제출 #1330281

#제출 시각아이디문제언어결과실행 시간메모리
1330281secondaccountmaybeOsumnjičeni (COCI21_osumnjiceni)C++20
110 / 110
573 ms67700 KiB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,q;
vector<pair<ll,ll>>i;
vector<ll>x;
ll u[200005][18];
ll e[200005][18];
void f()
{
	cin>>n;
	i.resize(n+1);
	for(ll j=1;j<=n;j++)
	{
		cin>>i[j].first>>i[j].second;
	}
	x.resize(n+2);
	vector<ll>r(n);
	for(ll j=0;j<n;j++)
	{
		r[j]=i[j+1].second;
	}
	sort(r.begin(),r.end());
	r.erase(unique(r.begin(),r.end()),r.end());
	ll m=r.size();
	vector<ll>b(m+2,0);
	auto u1=[&](ll p,ll v)
	{
		for(p++;p<=m;p+=p&(-p))
		{
			b[p]+=v;
		}
	};
	auto q1=[&](ll p)->ll
	{
		ll s=0;
		for(p++;p>0;p-=p&(-p))
		{
			s+=b[p];
		}
		return s;
	};
	auto g1=[&](ll v)->ll
	{
		return lower_bound(r.begin(),r.end(),v)-r.begin();
	};
	vector<ll>l(n);
	for(ll j=0;j<n;j++)
	{
		l[j]=i[j+1].first;
	}
	sort(l.begin(),l.end());
	l.erase(unique(l.begin(),l.end()),l.end());
	ll k=l.size();
	vector<ll>c(k+2,0);
	auto u2=[&](ll p,ll v)
	{
		for(p++;p<=k;p+=p&(-p))
		{
			c[p]+=v;
		}
	};
	auto q2=[&](ll p)->ll
	{
		ll s=0;
		for(p++;p>0;p-=p&(-p))
		{
			s+=c[p];
		}
		return s;
	};
	auto g2=[&](ll v)->ll
	{
		return lower_bound(l.begin(),l.end(),v)-l.begin();
	};
	ll w=0;
	ll h=0;
	ll o=1;
	auto c1=[&](ll n1,ll n2)->ll
	{
		ll r1=0;
		if(n1>0)
		{
			ll d=g1(n1)-1;
			if(d>=0)
			{
				r1=q1(d);
			}
		}
		ll r2=0;
		ll d2=g2(n2+1);
		if(d2<=k-1)
		{
			r2=(ll)(q2(k-1)-(d2>0?q2(d2-1):0));
		}
		return w-r1-r2;
	};
	auto a1=[&](ll t)
	{
		u1(g1(i[t].second),1);
		u2(g2(i[t].first),1);
		w++;
	};
	auto r3=[&](ll t)
	{
		u1(g1(i[t].second),-1);
		u2(g2(i[t].first),-1);
		w--;
	};
	for(ll j=1;j<=n;j++)
	{
		while(h<n)
		{
			if(c1(i[h+1].first,i[h+1].second)==0)
			{
				h++;
				a1(h);
			}
			else
			{
				break;
			}
		}
		x[j]=h;
		r3(j);
		if(h<j)
		{
			h=j;
		}
	}
	for(ll j=1;j<=n;j++)
	{
		u[j][0]=(x[j]<n)?x[j]+1:n+1;
		e[j][0]=x[j];
	}
	u[n+1][0]=n+1;
	e[n+1][0]=n;
	for(ll g=1;g<18;g++)
	{
		for(ll j=1;j<=n+1;j++)
		{
			ll m1=u[j][g-1];
			if(m1<=n)
			{
				u[j][g]=u[m1][g-1];
				e[j][g]=e[m1][g-1];
			}
			else
			{
				u[j][g]=n+1;
				e[j][g]=n;
			}
		}
	}
	cin>>q;
	while(q--)
	{
		ll p1,r4;
		cin>>p1>>r4;
		ll c2=p1;
		ll a2=0;
		for(ll g=17;g>=0;g--)
		{
			if(c2<=n&&e[c2][g]<r4)
			{
				a2+=(1LL<<g);
				c2=u[c2][g];
			}
		}
		if(c2<=r4)
		{
			a2++;
		}
		cout<<a2<<endl;
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	f();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...