Submission #707132

# Submission time Handle Problem Language Result Execution time Memory
707132 2023-03-08T13:43:24 Z mars4 Growing Trees (BOI11_grow) C++17
100 / 100
280 ms 15384 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds; 

#define ff              first
#define ss              second
#define ll              int64_t
#define ld              long double
#define nl              cout<<"\n"
#define i128            __int128_t
#define all(v)          v.begin(),v.end()
#define mset(a,v)       memset((a),(v),sizeof(a))
#define forn(i,a,b)     for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b)     for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio()        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define mod         1'000'000'007
#define mod2        998'244'353 
#define inf         1'000'000'000'000'007
#define pi          3.14159265358979323846

template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;

template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p)        {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v)                {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v)                 {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s)                   {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class T> ostream& operator<<(ostream& out,ordered_set<T> &s)           {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m)         {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}

void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail> 
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

//---------------------------------mars4---------------------------------

class SegtreeMin
{
	ll N=0;
	vector<ll> segtree;
	vector<ll> lazy;

	ll _build(vector<ll> &a,ll st,ll ed,ll i=1)
	{
		if(st==ed)
			return segtree[i]=a[st];
		ll mid=(st+ed)>>1;
		return segtree[i]=min(_build(a,st,mid,i<<1),_build(a,mid+1,ed,i<<1|1));
	}

	void _push(ll st,ll ed,ll val,ll i)
	{
		if(st!=ed)
		{
			lazy[i<<1]+=val;
			lazy[i<<1|1]+=val;
		}
	}

	void _update(ll st,ll ed,ll l,ll r,ll val,ll i=1)
	{
		if(lazy[i])
		{
			segtree[i]+=lazy[i];
			_push(st,ed,lazy[i],i);
			lazy[i]=0;
		}
		if(st>ed or st>r or ed<l)
			return;
		if(st>=l and ed<=r)
		{
			segtree[i]+=val;
			_push(st,ed,val,i);
			return;
		}
		ll mid=(st+ed)>>1;
		_update(st,mid,l,r,val,i<<1);
		_update(mid+1,ed,l,r,val,i<<1|1);
		segtree[i]=min(segtree[i<<1],segtree[i<<1|1]);
	}

	ll _query(ll st,ll ed,ll l,ll r,ll i=1)
	{
		if(lazy[i])
		{
			segtree[i]+=lazy[i];
			_push(st,ed,lazy[i],i);
			lazy[i]=0;
		}
		if(st>ed or st>r or ed<l)
			return inf;
		if(st>=l and ed<=r)
			return segtree[i];
		ll mid=(st+ed)>>1;
		return min(_query(st,mid,l,r,i<<1),_query(mid+1,ed,l,r,i<<1|1));
	}

	ll _first_element_atmost_x(ll st,ll ed,ll l,ll r,ll val,ll i=1)
	{
		if(lazy[i])
		{
			segtree[i]+=lazy[i];
			_push(st,ed,lazy[i],i);
			lazy[i]=0;
		}
		if(st>ed or st>r or ed<l or segtree[i]>val)
			return -1;
		if(st==ed)
			return st;
		ll mid=(st+ed)>>1;
		ll ind=_first_element_atmost_x(st,mid,l,r,val,i<<1);
		if(ind==-1)
			ind=_first_element_atmost_x(mid+1,ed,l,r,val,i<<1|1);
		return ind;
	}

	ll _last_element_atmost_x(ll st,ll ed,ll l,ll r,ll val,ll i=1)
	{
		if(lazy[i])
		{
			segtree[i]+=lazy[i];
			_push(st,ed,lazy[i],i);
			lazy[i]=0;
		}
		if(st>ed or st>r or ed<l or segtree[i]>val)
			return -1;
		if(st==ed)
			return st;
		ll mid=(st+ed)>>1;
		ll ind=_last_element_atmost_x(mid+1,ed,l,r,val,i<<1|1);
		if(ind==-1)
			ind=_last_element_atmost_x(st,mid,l,r,val,i<<1);
		return ind;
	}

	public:
	void init(ll n,ll val=0)
	{
		N=n;
		segtree=vector<ll>(N<<2,val);
		lazy=vector<ll>(N<<2);
	}

	void build(vector<ll> &a)
	{
		_build(a,0,N-1);
	}

	void update(ll l,ll h,ll val)
	{
		_update(0,N-1,l,h,val);
	}

	ll query(ll l,ll h)
	{
		return _query(0,N-1,l,h);
	}

	ll first_element_atmost_x(ll l,ll r,ll val)
	{
		return _first_element_atmost_x(0,N-1,l,r,val);
	}

	ll last_element_atmost_x(ll l,ll r,ll val)
	{
		return _last_element_atmost_x(0,N-1,l,r,val);
	}

	void clear()
	{
		segtree=vector<ll>(N<<2);
		lazy=vector<ll>(N<<2);
	}
};

class SegtreeMax
{
	ll N=0;
	vector<ll> segtree;
	vector<ll> lazy;

	ll _build(vector<ll> &a,ll st,ll ed,ll i=1)
	{
		if(st==ed)
			return segtree[i]=a[st];
		ll mid=(st+ed)>>1;
		return segtree[i]=max(_build(a,st,mid,i<<1),_build(a,mid+1,ed,i<<1|1));
	}

	void _push(ll st,ll ed,ll val,ll i)
	{
		if(st!=ed)
		{
			lazy[i<<1]+=val;
			lazy[i<<1|1]+=val;
		}
	}

	void _update(ll st,ll ed,ll l,ll r,ll val,ll i=1)
	{
		if(lazy[i])
		{
			segtree[i]+=lazy[i];
			_push(st,ed,lazy[i],i);
			lazy[i]=0;
		}
		if(st>ed or st>r or ed<l)
			return;
		if(st>=l and ed<=r)
		{
			segtree[i]+=val;
			_push(st,ed,val,i);
			return;
		}
		ll mid=(st+ed)>>1;
		_update(st,mid,l,r,val,i<<1);
		_update(mid+1,ed,l,r,val,i<<1|1);
		segtree[i]=max(segtree[i<<1],segtree[i<<1|1]);
	}

	ll _query(ll st,ll ed,ll l,ll r,ll i=1)
	{
		if(lazy[i])
		{
			segtree[i]+=lazy[i];
			_push(st,ed,lazy[i],i);
			lazy[i]=0;
		}
		if(st>ed or st>r or ed<l)
			return -inf;
		if(st>=l and ed<=r)
			return segtree[i];
		ll mid=(st+ed)>>1;
		return max(_query(st,mid,l,r,i<<1),_query(mid+1,ed,l,r,i<<1|1));
	}

	ll _first_element_atleast_x(ll st,ll ed,ll l,ll r,ll val,ll i=1)
	{
		if(lazy[i])
		{
			segtree[i]+=lazy[i];
			_push(st,ed,lazy[i],i);
			lazy[i]=0;
		}
		if(st>ed or st>r or ed<l or segtree[i]<val)
			return -1;
		if(st==ed)
			return st;
		ll mid=(st+ed)>>1;
		ll ind=_first_element_atleast_x(st,mid,l,r,val,i<<1);
		if(ind==-1)
			ind=_first_element_atleast_x(mid+1,ed,l,r,val,i<<1|1);
		return ind;
	}

	public:
	void init(ll n,ll val=0)
	{
		N=n;
		segtree=vector<ll>(N<<2,val);
		lazy=vector<ll>(N<<2);
	}

	void build(vector<ll> &a)
	{
		_build(a,0,N-1);
	}

	void update(ll l,ll r,ll val)
	{
		_update(0,N-1,l,r,val);
	}

	ll query(ll l,ll r)
	{
		return _query(0,N-1,l,r);
	}

	ll first_element_atleast_x(ll l,ll r,ll val)
	{
		return _first_element_atleast_x(0,N-1,l,r,val);
	}

	void clear()
	{
		segtree=vector<ll>(N<<2);
		lazy=vector<ll>(N<<2);
	}
};

int main()
{
	fastio();
	ll z,n,m,t,k,i,j,l,d,h,r;
	cin>>n>>m;
	vector<ll> a(n);
	for(ll &i:a)
	{
		cin>>i;
	}
	sort(all(a));
	SegtreeMin smin;
	SegtreeMax smax;
	smin.init(n);
	smin.build(a);
	smax.init(n);
	smax.build(a);
	forn(i,0,m)
	{
		char ch;
		cin>>ch>>l>>r;
		if(ch=='F')
		{
			ll lind=smax.first_element_atleast_x(0,n-1,r);
			if(lind!=-1)
			{
				if(n-lind<=l)
				{
					smin.update(lind,n-1,1);
					smax.update(lind,n-1,1);
				}
				else
				{
					ll rval=smax.query(lind+l-1,lind+l-1);
					ll rind=smin.last_element_atmost_x(0,n-1,rval);
					ll mind=smin.last_element_atmost_x(0,n-1,rval-1);
					if(mind>=lind)
					{
						smin.update(lind,mind,1);
						smax.update(lind,mind,1);
					}
					ll rem=l-(mind-lind+1);
					if(rem)
					{
						smin.update(rind-rem+1,rind,1);
						smax.update(rind-rem+1,rind,1);
					}
				}
			}
		}
		else
		{
			ll lind=smax.first_element_atleast_x(0,n-1,l);
			ll rind=smin.last_element_atmost_x(0,n-1,r);
			if(lind==-1 or rind==-1)
			{
				cout<<"0\n";
			}
			else
			{
				cout<<rind-lind+1<<"\n";
			}
		}
	}
	cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
	return 0;
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:303:5: warning: unused variable 'z' [-Wunused-variable]
  303 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
grow.cpp:303:11: warning: unused variable 't' [-Wunused-variable]
  303 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
grow.cpp:303:13: warning: unused variable 'k' [-Wunused-variable]
  303 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
grow.cpp:303:15: warning: unused variable 'i' [-Wunused-variable]
  303 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
grow.cpp:303:17: warning: unused variable 'j' [-Wunused-variable]
  303 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
grow.cpp:303:21: warning: unused variable 'd' [-Wunused-variable]
  303 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                     ^
grow.cpp:303:23: warning: unused variable 'h' [-Wunused-variable]
  303 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 107 ms 12236 KB Output is correct
2 Correct 184 ms 13420 KB Output is correct
3 Correct 61 ms 12884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 552 KB Output is correct
5 Correct 50 ms 1120 KB Output is correct
6 Correct 58 ms 1264 KB Output is correct
7 Correct 6 ms 972 KB Output is correct
8 Correct 20 ms 1700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 1768 KB Output is correct
2 Correct 62 ms 2584 KB Output is correct
3 Correct 2 ms 1168 KB Output is correct
4 Correct 27 ms 2184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 1604 KB Output is correct
2 Correct 66 ms 2484 KB Output is correct
3 Correct 15 ms 1364 KB Output is correct
4 Correct 62 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 9156 KB Output is correct
2 Correct 162 ms 11272 KB Output is correct
3 Correct 19 ms 3020 KB Output is correct
4 Correct 49 ms 11104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 10308 KB Output is correct
2 Correct 164 ms 12196 KB Output is correct
3 Correct 59 ms 11216 KB Output is correct
4 Correct 16 ms 3036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 11212 KB Output is correct
2 Correct 109 ms 13348 KB Output is correct
3 Correct 59 ms 12588 KB Output is correct
4 Correct 16 ms 3004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 11908 KB Output is correct
2 Correct 164 ms 10624 KB Output is correct
3 Correct 40 ms 14364 KB Output is correct
4 Correct 70 ms 13448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 11840 KB Output is correct
2 Correct 168 ms 13620 KB Output is correct
3 Correct 280 ms 15384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 13888 KB Output is correct