답안 #750681

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
750681 2023-05-30T06:25:47 Z mars4 Regions (IOI09_regions) C++17
70 / 100
2856 ms 131072 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              int
#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---------------------------------

vector<vector<ll>> v;
vector<vector<ll>> g;
vector<vector<ll>> vst;
vector<ll> regions;
vector<ll> st;
vector<ll> ed;
vector<ll> dfs_cnt;
vector<ll> eregions;
vector<ll> ind;
const ll B=130;
ll timer=-1;

void dfs(ll cur)
{
	timer++;
	st[cur]=timer;
	vst[regions[cur]].push_back(st[cur]);
	for(ll r:eregions)
	{
		g[ind[r]][regions[cur]]+=dfs_cnt[r];
	}
	dfs_cnt[regions[cur]]++;
	for(ll nxt:v[cur])
	{
		dfs(nxt);
	}
	dfs_cnt[regions[cur]]--;
	ed[cur]=timer;
}

int main()
{
	/* fastio(); */
	ll z,n,m,t,k,i,j,l,d,h,r;
	ll q;
	ll root_region;
	cin>>n>>m>>q;
	ll sz=(n+B-1)/B;
	v=vector<vector<ll>>(n);
	vst=vector<vector<ll>>(m);
	g=vector<vector<ll>>(sz,vector<ll>(m));
	regions=vector<ll>(n);
	st=vector<ll>(n);
	ed=vector<ll>(n);
	dfs_cnt=vector<ll>(m);
	ind=vector<ll>(m);
	vector<ll> cnt(m);
	vector<vector<ll>> employee_region(m);
	cin>>root_region;
	root_region--;
	regions[0]=root_region;
	employee_region[root_region].push_back(0);
	forn(i,1,n)
	{
		cin>>l>>r;
		l--,r--;
		v[l].push_back(i);
		regions[i]=r;
		employee_region[r].push_back(i);
		cnt[r]++;
	}
	forn(i,0,m)
	{
		if(cnt[i]>=B)
		{
			ind[i]=(ll)eregions.size();
			eregions.push_back(i);
		}
	}
	dfs(0);
	while(q--)
	{
		cin>>l>>r;
		l--,r--;
		ll res=0;
		if(cnt[l]>=B)
		{
			res=g[ind[l]][r];
		}
		else
		{
			for(ll i:employee_region[l])
			{
				res+=upper_bound(all(vst[r]),ed[i])-lower_bound(all(vst[r]),st[i]);
			}
		}
		cout<<res<<endl;
	}
	cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
	return 0;
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:79:5: warning: unused variable 'z' [-Wunused-variable]
   79 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
regions.cpp:79:11: warning: unused variable 't' [-Wunused-variable]
   79 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
regions.cpp:79:13: warning: unused variable 'k' [-Wunused-variable]
   79 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
regions.cpp:79:15: warning: unused variable 'i' [-Wunused-variable]
   79 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
regions.cpp:79:17: warning: unused variable 'j' [-Wunused-variable]
   79 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
regions.cpp:79:21: warning: unused variable 'd' [-Wunused-variable]
   79 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                     ^
regions.cpp:79:23: warning: unused variable 'h' [-Wunused-variable]
   79 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 2 ms 208 KB Output is correct
4 Correct 4 ms 312 KB Output is correct
5 Correct 7 ms 336 KB Output is correct
6 Correct 24 ms 408 KB Output is correct
7 Correct 31 ms 420 KB Output is correct
8 Correct 40 ms 472 KB Output is correct
9 Correct 44 ms 1004 KB Output is correct
10 Correct 96 ms 1232 KB Output is correct
11 Correct 117 ms 1644 KB Output is correct
12 Correct 147 ms 2404 KB Output is correct
13 Correct 192 ms 2156 KB Output is correct
14 Correct 132 ms 2836 KB Output is correct
15 Correct 229 ms 5628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 729 ms 7184 KB Output is correct
2 Correct 941 ms 6252 KB Output is correct
3 Correct 1322 ms 9604 KB Output is correct
4 Correct 177 ms 6560 KB Output is correct
5 Correct 339 ms 10348 KB Output is correct
6 Correct 635 ms 15828 KB Output is correct
7 Correct 1122 ms 27632 KB Output is correct
8 Correct 1149 ms 42476 KB Output is correct
9 Correct 2497 ms 82024 KB Output is correct
10 Runtime error 56 ms 131072 KB Execution killed with signal 9
11 Runtime error 55 ms 131072 KB Execution killed with signal 9
12 Correct 2679 ms 107988 KB Output is correct
13 Correct 2856 ms 108188 KB Output is correct
14 Runtime error 101 ms 131072 KB Execution killed with signal 9
15 Runtime error 57 ms 131072 KB Execution killed with signal 9
16 Runtime error 50 ms 131072 KB Execution killed with signal 9
17 Runtime error 89 ms 131072 KB Execution killed with signal 9