Submission #520097

# Submission time Handle Problem Language Result Execution time Memory
520097 2022-01-28T11:08:38 Z mars4 Regions (IOI09_regions) C++17
100 / 100
3850 ms 82844 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=110;
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 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 2 ms 296 KB Output is correct
4 Correct 5 ms 200 KB Output is correct
5 Correct 8 ms 328 KB Output is correct
6 Correct 22 ms 328 KB Output is correct
7 Correct 33 ms 416 KB Output is correct
8 Correct 35 ms 456 KB Output is correct
9 Correct 50 ms 1044 KB Output is correct
10 Correct 83 ms 1148 KB Output is correct
11 Correct 133 ms 1628 KB Output is correct
12 Correct 163 ms 2292 KB Output is correct
13 Correct 173 ms 2084 KB Output is correct
14 Correct 178 ms 3020 KB Output is correct
15 Correct 265 ms 6556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 720 ms 7540 KB Output is correct
2 Correct 957 ms 6316 KB Output is correct
3 Correct 1330 ms 10188 KB Output is correct
4 Correct 288 ms 4756 KB Output is correct
5 Correct 373 ms 6000 KB Output is correct
6 Correct 517 ms 6976 KB Output is correct
7 Correct 659 ms 10828 KB Output is correct
8 Correct 971 ms 18404 KB Output is correct
9 Correct 2545 ms 50984 KB Output is correct
10 Correct 3056 ms 81472 KB Output is correct
11 Correct 3850 ms 76104 KB Output is correct
12 Correct 2561 ms 55492 KB Output is correct
13 Correct 2706 ms 55784 KB Output is correct
14 Correct 2921 ms 59164 KB Output is correct
15 Correct 2891 ms 82844 KB Output is correct
16 Correct 3033 ms 80104 KB Output is correct
17 Correct 3502 ms 70292 KB Output is correct