103152024-03-30 17:24:58szilGénsebészcpp17Time limit exceeded 80/100232ms17788 KiB
#include <bits/stdc++.h>
#include "grader.h"

#pragma GCC optimize("O3,unroll-loops")

using namespace std;
using ll = long long;

mt19937 rng(1111);

int char_to_index(char x) {
	if (x == 'A') return 0;
	if (x == 'C') return 1;
	if (x == 'G') return 2;
	if (x == 'T') return 3;
	throw 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 = rng();
		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);
	fill(v->cnt, v->cnt+4, 0);
	v->cnt[char_to_index(v->c)] = 1;
	if (v->l) {
		for (int i = 0; i < 4; i++) {
			v->cnt[i] += v->l->cnt[i];
		}
	}
	if (v->r) {
		for (int i = 0; i < 4; i++) {
			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 (x == 0) return {nullptr, v};
	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};
	}
}

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[char_to_index(x)];
	root = merge(a, merge(c, d));
	return res;
}

string Eredmeny() {
	if (!root) return "";
	string res;
	stack<Node *> st;
	Node *v = root;
	while (v) {
		st.push(v);
		v = v->l;
	}
	auto next = [&](){
		Node *u = st.top();
		if (u->r) {
			u = u->r;
			while (u) {
				st.push(u);
				u = u->l;
			}
		} else {
			while (true) {
				st.pop();
				if (st.empty()) return;
				Node *par = st.top();
				if (par->l == u) return;
				u = par;
			}
		}
	};
	while (!st.empty()) {
		res += st.top()->c;
		next();
	}
	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";
	}
}
*/
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1884 KiB
2Accepted3ms2324 KiB
subtask230/30
3Accepted3ms2356 KiB
4Accepted3ms2572 KiB
5Accepted4ms2928 KiB
6Accepted4ms2888 KiB
7Accepted7ms3292 KiB
8Accepted67ms6908 KiB
subtask330/30
9Accepted3ms3004 KiB
10Accepted3ms3284 KiB
11Accepted4ms3368 KiB
12Accepted4ms3652 KiB
13Accepted7ms4016 KiB
14Accepted90ms9104 KiB
subtask420/20
15Accepted3ms4020 KiB
16Accepted3ms4256 KiB
17Accepted3ms4296 KiB
18Accepted3ms4300 KiB
19Accepted115ms10764 KiB
subtask50/20
20Accepted3ms4584 KiB
21Accepted4ms4952 KiB
22Accepted4ms5372 KiB
23Accepted7ms6048 KiB
24Time limit exceeded232ms17788 KiB