102662024-03-29 20:55:10szilGénsebészcpp17Runtime error 60/100263ms20732 KiB
#include <bits/stdc++.h>
#include "grader.h"

using namespace std;
using ll = long long;

struct Node {
	Node *l, *r;

	int cnt[26], prior, siz;
	char c;

	Node(char x) {
		c = x;
		fill(cnt, cnt+26, 0);
		cnt[x-'A']++;
		prior = rand();
		siz = 1;
		l = r = nullptr;
	}
};

int get_size(Node *ptr) {
	return ptr ? ptr->siz : 0;
}

void pull(Node *v) {
	v->siz = 1 + get_size(v->l) + get_size(v->r);
	for (int i = 0; i < 26; i++) {
		v->cnt[i] = (v->c-'A' == i);
		if (v->l) v->cnt[i] += v->l->cnt[i];
		if (v->r) v->cnt[i] += v->r->cnt[i];
	}
}

Node *merge(Node *l, Node *r) {
	if (!l || !r) return l ? l : r;
	if (l->prior > r->prior) {
		l->r = merge(l->r, r);
		pull(l);
		return l;
	} else {
		r->l = merge(l, r->l);
		pull(r);
		return r;
	}
}

pair<Node *, Node *> split(Node *v, int x) {
	if (!v) return {nullptr, nullptr};
	if (get_size(v->l) < x) {
		auto [l, r] = split(v->r, x - 1 - get_size(v->l));
		v->r = l;
		pull(v);
		return {v, r};
	} else {
		auto [l, r] = split(v->l, x);
		v->l = r;
		pull(v);
		return {l, v};
	}
}

void print(Node *v, string &res) {
	if (v->l) print(v->l, res);
	res += v->c;
	if (v->r) print(v->r, res);
}

Node *root = nullptr;

void Kezd(string s) {
	for (char c : s) {
		root = merge(root, new Node(c));
	}
}

void Beszur(int i, char x) {
	auto [a, b] = split(root, i);
	root = merge(a, merge(new Node(x), b));
}

void Mutal(int i, char x) {
	auto [a, b] = split(root, i-1);
	auto [c, d] = split(b, 1);
	root = merge(a, merge(new Node(x), d));
}

void Kivag(int i, int j) {
	auto [a, b] = split(root, i-1);
	auto [c, d] = split(b, j-i+1);
	root = merge(a, d);
}

int Szamlal(int i, int j, char x) {
	auto [a, b] = split(root, i-1);
	auto [c, d] = split(b, j-i+1);
	int res = c->cnt[x-'A'];
	root = merge(a, merge(c, d));
	return res;
}

string Eredmeny() {
	string res;
	print(root, res);
	return res;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1816 KiB
2Runtime error3ms2420 KiB
subtask230/30
3Accepted3ms2384 KiB
4Accepted4ms2612 KiB
5Accepted4ms2936 KiB
6Accepted4ms2720 KiB
7Accepted8ms3088 KiB
8Accepted141ms11976 KiB
subtask330/30
9Accepted3ms3752 KiB
10Accepted4ms3780 KiB
11Accepted4ms4004 KiB
12Accepted4ms4120 KiB
13Accepted8ms4500 KiB
14Accepted195ms16444 KiB
subtask40/20
15Accepted3ms4996 KiB
16Runtime error3ms5236 KiB
17Accepted3ms5256 KiB
18Accepted3ms5524 KiB
19Time limit exceeded247ms20732 KiB
subtask50/20
20Runtime error4ms7316 KiB
21Runtime error6ms7636 KiB
22Runtime error6ms7992 KiB
23Runtime error10ms9460 KiB
24Time limit exceeded263ms20680 KiB