CSES - TCR Contest - Results
Submission details
Task:Reversals
Sender:Kalle Luopajärvi
Submission time:2017-11-21 18:21:12 +0200
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.56 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:80:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < s.size(); ++i) {
                            ^

Code

#include <bits/stdc++.h>
using namespace std;
typedef struct node* pnode;
struct node {
	pnode l,r;
	int pr,c;
	int rev = 0;
	char q;
	node(char w) {
		l=0;r=0;c=1;pr=rand();
		q = w;

	}
};
// Returns the size of the subtree t
int cnt(pnode t) {
	if (t) return t->c;
	return 0;
}
// Updates the size of the subtree t
void upd(pnode t) {
	if (t) t->c=cnt(t->l)+cnt(t->r)+1;
}
// Put lazy updates here
void push(pnode t) {
	if (t) {
		if(t->rev) {
			t->rev = 0;
			if(t->l) {
				t->l->rev ^= 1;
			}
			if(t->r) {
				t->r->rev ^= 1;
			}
			swap(t->l, t->r);
		}
	}// Lazy update
}
// Merges trees l and r into tree t
void merg(pnode& t, pnode l, pnode r) {
	push(l);push(r);
	if (!l) t=r;
	else if(!r) t=l;
	else {
		if (l->pr>r->pr) {
			merg(l->r, l->r, r);t=l;
		}
		else {
			merg(r->l, l, r->l);t=r;
		}
	}
	upd(t);
}
// Splits tree t into trees l and r
// Size of tree l will be k
void split(pnode t, pnode& l, pnode& r, int k) {
	if (!t) {
		l=0;r=0;return;
	}
	else {
		push(t);
		if (cnt(t->l)>=k) {
			split(t->l, l, t->l, k);r=t;
		}
		else {
			split(t->r, t->r, r, k-cnt(t->l)-1);l=t;
		}
	}
	upd(t);
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;
	cin>>n>>m;
	string s;
	cin>>s;
	pnode root;
	root = new node(s[0]);
	for(int i = 1; i < s.size(); ++i) {
		merg(root, root, new node(s[i]));
	}
	for(int i = 0; i < m; ++i) {
		int a,b;
		cin>>a>>b;
		pnode le, mi, ri;
		split(root, le, mi, a-1);
		split(mi, mi, ri, b-a+1);
		mi->rev ^= 1;
		merg(le, le, mi);
		merg(le, le, ri);
	}
	for(int i = 0; i < n; ++i) {
		pnode q;
		split(root, q, root, 1);
		cout<<q->q;
	}
	cout<<'\n';
}

Test details

Test 1

Verdict: ACCEPTED

input
50 100
pplcmurzajsxlqqcrxewfhzqyihkzp...

correct output
fpuwlmatkzbhksppmjxpwurcvsdxcz...

user output
fpuwlmatkzbhksppmjxpwurcvsdxcz...

Test 2

Verdict: ACCEPTED

input
500000 100000
slsmyuezdrenskmgkwxpcfzistssmu...

correct output
slsmyuezvdfzhssyoofpsnsagrrzri...

user output
slsmyuezvdfzhssyoofpsnsagrrzri...