Codeforces 240F. TorCoder line segment tree
The line segment tree counts and maintains the number of letters in a certain interval ....
F. TorCoder time limit per test 3 seconds memory limit per test 256 megabytes input input.txt output output.txt
A boy named Leo doesn't miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following problem. He was given a stringS, ConsistingNLowercase English letters, andMQueries. Each query is characterised by a pair of integersLI, Bytes,RI(1 digit ≤ DigitLILimit ≤ limitRILimit ≤ limitN).
We'll consider the letters in the string numbered from 1NFrom left to right, that is,SSignature = SignatureS1S2...SN.
After each query he must swap letters with indexes fromLIToRIRandom sive in stringSSo as to make substring (LI, Bytes,RI) A palindrome. If there are multiple such letter permutations, you shoshould choose the one where string (LI, Bytes,RI) Will be lexicographically minimum. If no such permutation exists, you shoshould ignore the query (that is, not change stringS).
Everybody knows that on TorCoder rounds input line and array size limits never exceed 60, so Leo solved this problem easily. Your task is to solve the problem on a little bit larger limits. Given stringSAndMQueries, print the string that results after applying allMQueries to stringS.
Input
The first input line contains two integersNAndM(1 digit ≤ DigitN, Bytes,MLimit ≤limit 105)-the string length and the number of the queries.
The second line contains stringS, ConsistingNLowercase Latin letters.
Each of the nextMLines contains a pair of integersLI, Bytes,RI(1 digit ≤ DigitLILimit ≤ limitRILimit ≤ limitN)-A query to apply to the string.
Output
In a single line print the result of applyingMQueries to stringS. Print the queries in the order in which they are given in the input.
Sample test (s) input
7 2aabcbaa1 35 7
Output
abacaba
Input
3 2abc1 22 3
Output
abc
Note
A substring (LI, Bytes,RI) 1 bytes ≤ bytesLILimit ≤ limitRILimit ≤ limitN) Of stringSSignature = SignatureS1S2...SNOf lengthNIs a sequence of charactersSLISLILifecycle + lifecycle 1...SRI.
A string is a palindrome, if it reads the same from left to right and from right to left.
StringX1X2...XPIs lexicographically smaller than stringY1Y2...YQ, If eitherPLatency <latencyQAndX1 bytes = bytesY1, bytes,X2 bytes = bytesY2, middle..., middle ,...,XPSignature = SignatureYP, Or exists such numberR(RLatency <latencyP, Bytes,RLatency <latencyQ), ThatX1 bytes = bytesY1, bytes,X2 bytes = bytesY2, middle..., middle ,...,XRSignature = SignatureYRAndXRLatency + latency 1 latency <latencyYRUpload + upload 1.
/*************************************** * ******** Author: CKbossCreated Time: October Monday 07: 29 minutes 48 seconds File Name: CF240F_2.cpp *************************************** * *********/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 100100; int n, m; int sum [28] [maxn <2]; int add [maxn <2]; /// lazychar str [maxn]; int cs [28]; # define lson l, m, rt <1 # define rson m + 1, r, rt <1 | 1 void cls (int rt) {add [rt] =-1; for (int I = 0; I <26; I ++) sum [I] [rt] = 0;} void push_up (int rt) {for (int I = 0; I <26; I ++) sum [I] [rt] = sum [I] [rt <1] + sum [I] [rt <1 | 1];} void push_down (int l, int r, int rt) {if (add [rt]! =-1) {int m = (l + r)/2; int id = add [rt]; cls (rt <1 ); cls (rt <1 | 1); add [rt <1] = add [rt <1 | 1] = add [rt]; sum [id] [rt <1] = m-l + 1; sum [id] [rt <1 | 1] = r-m; add [rt] =-1 ;}} void build (int l, int r, int rt) {add [rt] =-1; if (l = r) {int id = str [l]-'A'; sum [id] [rt] ++; add [rt] = id; return ;} int m = (l + r)/2; build (lson); build (rson); push_up (rt);} void query (int L, int R, int l, int r, int rt) {if (L <= l & r <= R) {for (int I = 0; I <26; I ++) cs [I] + = sum [I] [rt]; return;} push_down (l, r, rt); int m = (l + r)/2; if (L <= m) query (L, R, lson); if (R> m) query (L, R, rson); push_up (rt );} void update (int id, int L, int R, int l, int r, int rt) {if (L <= l & r <= R) {cls (rt); add [rt] = id; sum [id] [rt] = r-l + 1; return;} push_down (l, r, rt ); int m = (l + r)/2; if (L <= m) update (id, L, R, lson); if (R> m) update (id, l, R, rson); push_up (rt);} char res [maxn]; void solve (int L, int R) {int op =-1, odd = 0; memset (cs, 0, sizeof (cs); query (L, R, 1, n, 1); for (int I = 0; I <26; I ++) {if (cs [I] % 2) {odd ++; op = I;} if (odd> 1) return ;} int st = L, en = R; int MID = L + (R-L + 1)/2; for (int I = 0; I <26; I ++) {if (cs [I]) {if (cs [I]/2) {update (I, st, st + cs [I]/2-1,1, n, 1); update (I, en-cs [I]/2 + 1, en, 1, n, 1); st = st + cs [I]/2; en = en-cs [I]/2;} if (cs [I] % 2 = 1) {update (I, MID, MID, 1, n, 1) ;}}}int main({{freopen(input.txt,r,stdin={freopen(output.txt, w, stdout); scanf (% d, & n, & m); scanf (% s, str + 1 ); /// buildbuild (1, n, 1); int L, R; while (m --) {scanf (% d, & L, & R ); solve (L, R) ;}for (int I = 1; I <= n; I ++) {memset (cs, 0, sizeof (cs )); query (I, I, 1, n, 1); for (int j = 0; j <26; j ++) if (cs [j]) {putchar ('A' + j); break ;}} putchar (10); return 0 ;}