Submission #520098

# Submission time Handle Problem Language Result Execution time Memory
520098 2022-01-28T11:15:12 Z mars4 Regions (IOI09_regions) C++17
100 / 100
3797 ms 32532 KB
#include <algorithm>
#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 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---------------------------------

const int B=320;
vector<int> st;
vector<int> ed;
vector<int> big;
vector<int> tot;
vector<int> region;
vector<vector<int>> v;
vector<vector<int>> g;
vector<vector<int>> in;
vector<vector<pair<int,int>>> pos;
int timer=-1;

void dfs(ll cur,ll prev)
{
	for(int i:big)
	{
		g[i][region[cur]]+=tot[i];
	}
	timer++;
	st[cur]=timer;
	in[region[cur]].push_back(timer);
	tot[region[cur]]++;
	for(int i:v[cur])
	{
		if(i!=prev)
		{
			dfs(i,cur);
		}
	}
	ed[cur]=timer;
	pos[region[cur]].push_back({st[cur],ed[cur]});
	tot[region[cur]]--;
}

int main()
{
	/* fastio(); */
	ll z,n,m,t,k,i,j,l,d,h,r;
	ll q;
	cin>>n>>m>>q;
	st=vector<int>(n);
	ed=vector<int>(n);
	region=vector<int>(n);
	tot=vector<int>(m);
	v=vector<vector<int>>(n);
	g=vector<vector<int>>(m);
	in=vector<vector<int>>(m);
	pos=vector<vector<pair<int,int>>>(m);
	vector<int> cnt(m);
	cin>>d;
	region[0]=d-1;
	cnt[d-1]++;
	forn(i,1,n)
	{
		cin>>r>>d;
		r--,d--;
		v[r].push_back(i);
		region[i]=d;
		cnt[d]++;
	}
	forn(i,0,m)
	{
		if(cnt[i]>=B)
		{
			g[i]=vector<int>(m);
			big.push_back(i);
		}
	}
	dfs(0,-1);
	while(q--)
	{
		int r1,r2;
		cin>>r1>>r2;
		r1--,r2--;
		if(cnt[r1]>=B)
		{
			cout<<g[r1][r2]<<endl;
		}
		else
		{
			int ans=0;
			for(auto [l,r]:pos[r1])
			{
				ans+=upper_bound(all(in[r2]),r)-lower_bound(all(in[r2]),l);
			}
			cout<<ans<<endl;
		}
	}
	cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
	return 0;
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:83:5: warning: unused variable 'z' [-Wunused-variable]
   83 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
regions.cpp:83:11: warning: unused variable 't' [-Wunused-variable]
   83 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
regions.cpp:83:13: warning: unused variable 'k' [-Wunused-variable]
   83 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
regions.cpp:83:15: warning: unused variable 'i' [-Wunused-variable]
   83 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
regions.cpp:83:17: warning: unused variable 'j' [-Wunused-variable]
   83 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
regions.cpp:83:19: warning: unused variable 'l' [-Wunused-variable]
   83 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                   ^
regions.cpp:83:23: warning: unused variable 'h' [-Wunused-variable]
   83 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 2 ms 300 KB Output is correct
4 Correct 5 ms 200 KB Output is correct
5 Correct 7 ms 328 KB Output is correct
6 Correct 21 ms 328 KB Output is correct
7 Correct 28 ms 424 KB Output is correct
8 Correct 31 ms 480 KB Output is correct
9 Correct 26 ms 1060 KB Output is correct
10 Correct 85 ms 1152 KB Output is correct
11 Correct 137 ms 1520 KB Output is correct
12 Correct 137 ms 2348 KB Output is correct
13 Correct 153 ms 2132 KB Output is correct
14 Correct 262 ms 2852 KB Output is correct
15 Correct 304 ms 6312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1849 ms 7404 KB Output is correct
2 Correct 1896 ms 6208 KB Output is correct
3 Correct 2781 ms 9920 KB Output is correct
4 Correct 294 ms 3144 KB Output is correct
5 Correct 352 ms 5428 KB Output is correct
6 Correct 489 ms 6980 KB Output is correct
7 Correct 1062 ms 8916 KB Output is correct
8 Correct 1072 ms 17812 KB Output is correct
9 Correct 2159 ms 15752 KB Output is correct
10 Correct 2566 ms 32532 KB Output is correct
11 Correct 3797 ms 18928 KB Output is correct
12 Correct 1330 ms 17708 KB Output is correct
13 Correct 1709 ms 18196 KB Output is correct
14 Correct 1884 ms 19956 KB Output is correct
15 Correct 2521 ms 24068 KB Output is correct
16 Correct 2666 ms 31164 KB Output is correct
17 Correct 2217 ms 31084 KB Output is correct