103692024-04-01 13:55:53szilGénsebészcpp17Elfogadva 100/100187ms16624 KiB
#include <bits/stdc++.h>
#include "grader.h"

#pragma GCC optimize("O3")

using namespace std;
using ll = long long;

constexpr int char_to_index[100] = {
       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,  -1,   0,  -1,   1,  -1,  -1, 
       -1,   2,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,   3,  -1,  -1,  -1,  -1,  -1, 
       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1};

struct Node {
	Node *l, *r;

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

	Node(char x) {
		c = x;
		fill(cnt, cnt+4, 0);
		cnt[char_to_index[x]]++;
		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 < 4; i++) {
		v->cnt[i] = (char_to_index[v->c] == 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 (x == 0) return {nullptr, v};
    if (x == v->siz) return {v, 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) {
	srand(2);
	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[char_to_index[x]];
	root = merge(a, merge(c, d));
	return res;
}

string Eredmeny() {
	string res;
	if (root) print(root, res);
	return res;
}
/*
int main() {
	string s; cin >> s;
	Kezd(s);
	while (true) {
		int op; cin >> op;
		cerr << op << endl;
		if (op == 0) {
			break;
		}
		if (op == 1) {
			int x; char c; cin >> x >> c;
			Beszur(x, c);
		}
		if (op == 2) {
			int x; char c; cin >> x >> c;
			Mutal(x, c);
		}
		if (op == 3) {
			int l, r; cin >> l >> r;
			Kivag(l, r);
		}
		if (op == 4) {
			int l, r, ans; char c; cin >> l >> r >> c >> ans;
			int res = Szamlal(l, r, c);
			// if (ans == res) cout << "Correct\n";
			// else cout << "Incorrect: " << ans << " " << res << "\n";
		}
		if (op == 5) {
			string res = Eredmeny();
			string ans; cin >> ans;
			ans.pop_back();
			// if (ans == res) cout << "Correct\n";
			// else cout << "Incorrect: " << ans << " " << res << "\n";
		}
		cout << Eredmeny() << "\n";
	}
}
*/
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva3ms1876 KiB
2Elfogadva3ms2264 KiB
subtask230/30
3Elfogadva3ms2392 KiB
4Elfogadva3ms2536 KiB
5Elfogadva3ms2624 KiB
6Elfogadva4ms2980 KiB
7Elfogadva6ms3244 KiB
8Elfogadva54ms6984 KiB
subtask330/30
9Elfogadva3ms3116 KiB
10Elfogadva3ms3132 KiB
11Elfogadva3ms3148 KiB
12Elfogadva4ms3160 KiB
13Elfogadva6ms3468 KiB
14Elfogadva72ms8524 KiB
subtask420/20
15Elfogadva3ms3328 KiB
16Elfogadva3ms3444 KiB
17Elfogadva3ms3436 KiB
18Elfogadva3ms3588 KiB
19Elfogadva92ms9912 KiB
subtask520/20
20Elfogadva3ms3636 KiB
21Elfogadva3ms3960 KiB
22Elfogadva4ms4108 KiB
23Elfogadva6ms4888 KiB
24Elfogadva187ms16624 KiB