102632024-03-29 20:39:11szilGénsebészcpp17Wrong answer 0/10097ms33136 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;
		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 answer3ms1812 KiB
2Wrong answer3ms2240 KiB
subtask20/30
3Wrong answer3ms2360 KiB
4Wrong answer3ms2364 KiB
5Wrong answer3ms2468 KiB
6Wrong answer3ms2640 KiB
7Wrong answer3ms2936 KiB
8Wrong answer25ms11800 KiB
subtask30/30
9Wrong answer3ms3164 KiB
10Wrong answer3ms3512 KiB
11Wrong answer3ms3388 KiB
12Wrong answer3ms3600 KiB
13Wrong answer3ms3764 KiB
14Wrong answer34ms15068 KiB
subtask40/20
15Wrong answer3ms3804 KiB
16Wrong answer3ms3768 KiB
17Wrong answer3ms3756 KiB
18Wrong answer3ms3904 KiB
19Wrong answer43ms18124 KiB
subtask50/20
20Wrong answer4ms4752 KiB
21Wrong answer4ms5100 KiB
22Wrong answer6ms5628 KiB
23Wrong answer8ms7168 KiB
24Wrong answer97ms33136 KiB