Submission #431886

# Submission time Handle Problem Language Result Execution time Memory
431886 2021-06-17T16:42:11 Z amunduzbaev Regions (IOI09_regions) C++14
0 / 100
40 ms 43388 KB
/* made by amunduzbaev */
 
#include "bits/stdc++.h"
 
using namespace std;
 
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template<class T> using oset = tree<T, 
//~ null_type, less_equal<T>, rb_tree_tag, 
//~ tree_order_statistics_node_update>;
 
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define mem(arr, v) memset(arr, v, sizeof arr)
#define int long long
#define degub(x) cout<<#x<<" : "<<x<<"\n"
#define GG cout<<"here"<<endl;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii; 
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, true : false; }
template<int sz> using tut = array<int, sz>;
void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);  
	freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
 
const int N = 2e3+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
const int B = 1;
 
#define MULTI 0
int n, m, k, t, q, ans, res, a[N];
int heavy[N], last[N], id[N];
int bs[N / B + 10][N];
int sb[N][N / B + 10];
vii edges[N], cnt[N];

//~ BIG_SMALL BIG_BIG
int cur;
void bsdfs(int u, int p = -1, int cnt = 0){
	//~ cout<<cur<<" "<<id[a[u]]<<" "<<a[u]<<" "<<cnt<<"\n";
	bs[cur][a[u]] += cnt;
	if(id[a[u]] == cur) cnt++;
	for(auto x : edges[u]) bsdfs(x, u, cnt);
}

//~ SMALL_BIG
map<int, int> sbdfs(int u, int p = -1){
	map<int, int> cnt;
	if(heavy[a[u]]) cnt[a[u]]++;
	for(auto x : edges[u]) {
		map<int, int> tmp = sbdfs(x, u);
		if(sz(tmp) > sz(cnt)) swap(cnt, tmp);
		for(auto x : tmp) cnt[x.ff] += x.ss;
	}
	
	if(!heavy[a[u]]){
		for(auto x : cnt) sb[a[u]][x.ff] += x.ss;
	} return cnt;
}

int tin[N], tout[N];
void dfs(int u, int p = -1){
	tin[u] = t++;
	for(auto x : edges[u]) dfs(x, u);
	tout[u] = t - 1;
}

bool par(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }

void solve(int t_case){
	cin>>n>>m>>q;
	cin>>a[1];
	for(int i=2;i<=n;i++){
		int p; cin>>p>>a[i];
		edges[p].pb(i);
	}
	
	for(int i=1;i<=n;i++) cnt[a[i]].pb(i);
	int last = 1;
	for(int i=1;i<=m;i++){
		if(sz(cnt[i]) > B) heavy[i] = 1, id[i] = last++;
	}
	
	for(int i=1;i<last;i++) cur = i, bsdfs(1);
	sbdfs(1), dfs(1);

	for(int i=1;i<=q;i++){
		int x, b; cin>>x>>b;
		if(heavy[a[x]]) cout<<bs[id[a[x]]][a[b]]<<endl;
		else if(heavy[a[b]]) cout<<sb[a[x]][a[b]]<<endl;
		else { res = 0;
			//~ SMALL_SMALL
			for(auto xx : cnt[a[x]])
				for(auto y : cnt[a[b]]) res += par(xx, y);
			cout<<res<<endl;
		}
	}
}

signed main(){
	NeedForSpeed
	if(MULTI){
		int t; cin>>t;
		for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
	} else solve(1);
	return 0;
}

Compilation message

regions.cpp: In function 'void usaco(std::string)':
regions.cpp:38:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 | void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:39:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 328 KB Output isn't correct
2 Incorrect 1 ms 456 KB Output isn't correct
3 Incorrect 3 ms 456 KB Output isn't correct
4 Incorrect 4 ms 584 KB Output isn't correct
5 Incorrect 5 ms 584 KB Output isn't correct
6 Incorrect 22 ms 2120 KB Output isn't correct
7 Incorrect 32 ms 1352 KB Output isn't correct
8 Incorrect 27 ms 1736 KB Output isn't correct
9 Runtime error 13 ms 4500 KB Execution killed with signal 11
10 Runtime error 5 ms 840 KB Execution killed with signal 11
11 Runtime error 12 ms 4164 KB Execution killed with signal 11
12 Runtime error 15 ms 7112 KB Execution killed with signal 11
13 Runtime error 11 ms 3136 KB Execution killed with signal 11
14 Runtime error 9 ms 2808 KB Execution killed with signal 11
15 Runtime error 9 ms 3960 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 3256 KB Execution killed with signal 11
2 Runtime error 5 ms 880 KB Execution killed with signal 11
3 Runtime error 11 ms 3400 KB Execution killed with signal 11
4 Runtime error 4 ms 712 KB Execution killed with signal 6
5 Runtime error 40 ms 43388 KB Execution killed with signal 11
6 Runtime error 5 ms 712 KB Execution killed with signal 11
7 Runtime error 5 ms 780 KB Execution killed with signal 11
8 Runtime error 6 ms 712 KB Execution killed with signal 11
9 Runtime error 5 ms 796 KB Execution killed with signal 11
10 Runtime error 5 ms 840 KB Execution killed with signal 11
11 Runtime error 4 ms 712 KB Execution killed with signal 11
12 Runtime error 5 ms 712 KB Execution killed with signal 11
13 Runtime error 5 ms 712 KB Execution killed with signal 11
14 Runtime error 7 ms 900 KB Execution killed with signal 6
15 Runtime error 5 ms 712 KB Execution killed with signal 11
16 Runtime error 5 ms 1224 KB Execution killed with signal 6
17 Runtime error 5 ms 840 KB Execution killed with signal 11