# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
750675 | mars4 | Regions (IOI09_regions) | C++17 | 4596 ms | 130092 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
const ll B=450;
ll timer=-1;
void dfs(ll cur)
{
timer++;
st[cur]=timer;
vst[regions[cur]].push_back(st[cur]);
for(ll r:eregions)
{
g[regions[cur]][r]+=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;
v=vector<vector<ll>>(n);
vst=vector<vector<ll>>(m);
g=vector<vector<ll>>(m,vector<ll>(B));
regions=vector<ll>(n);
st=vector<ll>(n);
ed=vector<ll>(n);
dfs_cnt=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)
{
eregions.push_back(i);
}
}
dfs(0);
while(q--)
{
cin>>l>>r;
l--,r--;
ll res=0;
if(cnt[l]>=B)
{
res=g[r][l];
}
else
{
for(ll i:employee_region[l])
{
ll led=upper_bound(all(vst[r]),ed[i])-vst[r].begin();
ll lst=lower_bound(all(vst[r]),st[i])-vst[r].begin();
res+=led-lst;
}
}
cout<<res<<endl;
}
cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |