Submission #242090

# Submission time Handle Problem Language Result Execution time Memory
242090 2020-06-26T18:59:56 Z LucaDantas Ball Machine (BOI13_ballmachine) C++17
16.1111 / 100
241 ms 46316 KB
#include <bits/stdc++.h>
using namespace std;
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], 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 < 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);
}

int main() {
	int Q;
	scanf("%d %d", &n, &Q);
	rep(i,1,n+1) {
		int p; scanf("%d", &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("%d %d", &type, &k);
		if(type == 1) {
			rep(i,0,k) {
				if(i == k-1) printf("%d\n", add());
				else add(); // returns the position that was added
			}
		}
		else {
			printf("%d\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 'int main()':
ballmachine.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &Q);
  ~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp:115:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int p; scanf("%d", &p);
          ~~~~~^~~~~~~~~~
ballmachine.cpp:132:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &type, &k);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 14976 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Runtime error 57 ms 29560 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Runtime error 53 ms 29176 KB Execution killed with signal 11 (could be triggered by violating memory limits)
4 Runtime error 20 ms 14848 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 18 ms 14976 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 19 ms 15104 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 22 ms 15104 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Runtime error 19 ms 15104 KB Execution killed with signal 11 (could be triggered by violating memory limits)
9 Runtime error 21 ms 15872 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 28 ms 18688 KB Execution killed with signal 11 (could be triggered by violating memory limits)
11 Runtime error 65 ms 29816 KB Execution killed with signal 11 (could be triggered by violating memory limits)
12 Runtime error 51 ms 29048 KB Execution killed with signal 11 (could be triggered by violating memory limits)
13 Runtime error 59 ms 29304 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 60 ms 11256 KB Output is correct
2 Runtime error 112 ms 44404 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Runtime error 76 ms 36440 KB Execution killed with signal 11 (could be triggered by violating memory limits)
4 Runtime error 44 ms 24312 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 36 ms 23808 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 36 ms 23832 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 40 ms 22392 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Correct 53 ms 11256 KB Output is correct
9 Runtime error 97 ms 44784 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 118 ms 44560 KB Execution killed with signal 11 (could be triggered by violating memory limits)
11 Runtime error 101 ms 44276 KB Execution killed with signal 11 (could be triggered by violating memory limits)
12 Runtime error 98 ms 40428 KB Execution killed with signal 11 (could be triggered by violating memory limits)
13 Correct 122 ms 24300 KB Output is correct
14 Runtime error 69 ms 36336 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 69 ms 30604 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Runtime error 134 ms 42408 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Correct 144 ms 23216 KB Output is correct
4 Runtime error 90 ms 39792 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 96 ms 39156 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 94 ms 39256 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 91 ms 36704 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Correct 143 ms 23152 KB Output is correct
9 Runtime error 132 ms 46316 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 135 ms 45424 KB Execution killed with signal 11 (could be triggered by violating memory limits)
11 Runtime error 135 ms 45556 KB Execution killed with signal 11 (could be triggered by violating memory limits)
12 Runtime error 136 ms 42356 KB Execution killed with signal 11 (could be triggered by violating memory limits)
13 Correct 241 ms 27508 KB Output is correct
14 Runtime error 103 ms 38124 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 96 ms 44784 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Runtime error 118 ms 41968 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Correct 139 ms 26356 KB Output is correct
4 Runtime error 116 ms 44812 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Runtime error 112 ms 44876 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 100 ms 43888 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 122 ms 41972 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Correct 135 ms 26356 KB Output is correct
9 Runtime error 71 ms 36464 KB Execution killed with signal 11 (could be triggered by violating memory limits)