102642024-03-29 20:41:20szilGénsebészcpp17Wrong answer 0/10098ms31932 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);
	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);
	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);
	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
1Wrong answer3ms1816 KiB
2Wrong answer3ms2280 KiB
subtask20/30
3Wrong answer3ms2240 KiB
4Wrong answer3ms2608 KiB
5Wrong answer3ms2832 KiB
6Wrong answer3ms2940 KiB
7Wrong answer3ms3092 KiB
8Wrong answer25ms11320 KiB
subtask30/30
9Wrong answer3ms2852 KiB
10Wrong answer3ms3128 KiB
11Wrong answer3ms3092 KiB
12Wrong answer3ms3368 KiB
13Wrong answer3ms3796 KiB
14Wrong answer35ms14728 KiB
subtask40/20
15Wrong answer3ms3420 KiB
16Wrong answer3ms3444 KiB
17Wrong answer3ms3532 KiB
18Wrong answer3ms3472 KiB
19Wrong answer43ms17572 KiB
subtask50/20
20Wrong answer4ms4216 KiB
21Wrong answer4ms4616 KiB
22Wrong answer6ms4940 KiB
23Wrong answer8ms6304 KiB
24Wrong answer98ms31932 KiB