Submission #242095

# Submission time Handle Problem Language Result Execution time Memory
242095 2020-06-26T19:05:18 Z LucaDantas Ball Machine (BOI13_ballmachine) C++17
27.5397 / 100
290 ms 69744 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#ifdef MY_DEBUG_FLAG
template<typename T> void debug(T a) { cerr << a << ' '; }
template<typename T, typename U> void debug(pair<T, U> a) { cerr << a.first << ' ' << a.second << ' '; }
template<typename T> void debug(vector<T> a) { for(auto it : a) debug(it);}
template<typename T> void debug(set<T> a) { for(auto it : a) debug(it);}
#define db(a) cerr << "DEBUG ( " << #a << " == "; debug(a); cerr << ")\n"
#else
#define db(...)
#endif

#define pb push_back
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false), cout.tie(nullptr), cin.tie(nullptr)
#define sz(a) ((int)(a).size())
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define dec(i,n,a) for(int i=(n); i>=(a); i--)
#define clr(a,v) memset(a, v, sizeof(a))
#define all(a) (a).begin(),(a).end()

constexpr int inf = 0x3f3f3f3f;
constexpr int MAXN = 3e5 + 10;
constexpr int LOGN = 20;
constexpr int mod = 1000000007;

int n, root;

int pai[MAXN][LOGN+2], menor[MAXN], in[MAXN], out[MAXN], aux = 0;

bool act[MAXN];

vi g[MAXN]; // contains only the children of a node

int calc_menor(int u) {
	int ans = u;
	for(auto v : g[u]) {
		ans = min(ans, calc_menor(v));
	}
	return ans;
}

void dfs(int u) {
	in[u] = aux++;
	sort(all(g[u]), [](const int& a, const int& b) {
		return menor[a] < menor[b];
	});
	for(auto v : g[u]) {
		dfs(v);
	}
	out[u] = aux++;
}

struct comp {
	bool operator()(const int& a, const int& b) {
		return in[a] < in[b];
	}
};

priority_queue<int, vector<int>, comp> q;

void binary_parents() {
	for(int j = 1; j < LOGN; j++) {
		for(int i = 1; i <= n; i++) {
			if(pai[i][j-1] == 0) continue;
			pai[i][j] = pai[pai[i][j-1]][j-1];
		}
	}
}

int bit[2*MAXN];

void upd(int x, int v) {
	for(x++; x < 2*MAXN; x += x&-x)
		bit[x] += v;
}

int query(int x) {
	int ans = 0;
	for(x++; x > 0; x -= x&-x)
		ans += bit[x];
	return ans;
}

int add() {
	assert(!q.empty());
	int now = q.top(); // db(now);
	q.pop();
	act[now] = 1;
	upd(in[now], 1);
	upd(out[now], -1);
	return now;
}

void remove(int k) {
	// db(act[k]);
	dec(i,LOGN,0) {
		if(act[pai[k][i]]) k = pai[k][i];
	}
	assert(act[k]);
	act[k] = 0;
	q.push(k);
	upd(in[k], -1); upd(out[k], 1);
}

int32_t main() {
	int Q;
	scanf("%lld %lld", &n, &Q);
	rep(i,1,n+1) {
		int p; scanf("%lld", &p);
		pai[i][0] = p;
		if(p == 0) root = i;
		else g[p].pb(i);
	}

	binary_parents();
	calc_menor(root);
	dfs(root);

	// int oi, cnta = 0;
	// while(1) { cin >> oi; db(oi); db(cnta++); }

	rep(i,1,n+1) q.push(i);

	while(Q--) {
		int type, k;
		scanf("%lld %lld", &type, &k);
		if(type == 1) {
			rep(i,0,k) {
				if(i == k-1) printf("%lld\n", add());
				else add(); // returns the position that was added
			}
		}
		else {
			printf("%lld\n", query(in[k])-1); // how many active parents he has
			remove(k); // removes the last parent of this guy 
		}
	}

	// rep(i,1,n+1) { db(i), db(act[i]); }
}

Compilation message

ballmachine.cpp: In function 'int32_t main()':
ballmachine.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld %lld", &n, &Q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:118:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int p; scanf("%lld", &p);
          ~~~~~^~~~~~~~~~~~
ballmachine.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld", &type, &k);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 19 ms 14976 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Runtime error 77 ms 43984 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Runtime error 70 ms 43120 KB Execution killed with signal 11 (could be triggered by violating memory limits)
4 Runtime error 20 ms 14976 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 20 ms 14976 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 22 ms 15360 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 21 ms 15488 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Runtime error 20 ms 15360 KB Execution killed with signal 11 (could be triggered by violating memory limits)
9 Runtime error 22 ms 16768 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 35 ms 22648 KB Execution killed with signal 11 (could be triggered by violating memory limits)
11 Runtime error 96 ms 44404 KB Execution killed with signal 11 (could be triggered by violating memory limits)
12 Runtime error 76 ms 43252 KB Execution killed with signal 11 (could be triggered by violating memory limits)
13 Runtime error 75 ms 43508 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 65 ms 13320 KB Output is correct
2 Runtime error 138 ms 66928 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Runtime error 107 ms 57836 KB Execution killed with signal 11 (could be triggered by violating memory limits)
4 Runtime error 54 ms 31352 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 46 ms 30464 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 47 ms 30584 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 45 ms 28660 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Correct 55 ms 13172 KB Output is correct
9 Runtime error 139 ms 66540 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 135 ms 66800 KB Execution killed with signal 11 (could be triggered by violating memory limits)
11 Runtime error 134 ms 66288 KB Execution killed with signal 11 (could be triggered by violating memory limits)
12 Runtime error 128 ms 62064 KB Execution killed with signal 11 (could be triggered by violating memory limits)
13 Correct 135 ms 33516 KB Output is correct
14 Runtime error 116 ms 57836 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 104 ms 42356 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Correct 244 ms 32748 KB Output is correct
3 Correct 170 ms 32236 KB Output is correct
4 Runtime error 117 ms 58092 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 122 ms 57712 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 121 ms 57708 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 125 ms 55160 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Correct 181 ms 32108 KB Output is correct
9 Runtime error 226 ms 69744 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Correct 267 ms 34420 KB Output is correct
11 Correct 290 ms 34416 KB Output is correct
12 Correct 240 ms 32752 KB Output is correct
13 Correct 281 ms 38764 KB Output is correct
14 Runtime error 137 ms 61548 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 138 ms 66668 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Runtime error 179 ms 64876 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Correct 154 ms 36976 KB Output is correct
4 Runtime error 135 ms 66668 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 164 ms 67308 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 129 ms 65648 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 153 ms 64868 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Correct 198 ms 36976 KB Output is correct
9 Runtime error 95 ms 58220 KB Execution killed with signal 11 (could be triggered by violating memory limits)