CF 240F TorCoder (line segment tree)

Source: Internet
Author: User

Question: Give a string with m operations. Each time you give a range, you can re-adjust the range to a text-back sequence. If there are multiple operations, select the smallest Lexicographic Order. If not, do not operate. The final output of the string is just getting started. I feel like a magical question. In fact, I have to think more about it myself. Think about it. It's actually quite simple. First, count the number of letters in the interval, and then compare the parity of the Interval Length to determine whether the operation can be performed. The smallest Lexicographic Order of the return string is clearly determined. You only need to enumerate 26 letters from small to large. So the method is: the number of letters in the statistical interval of the Line Segment tree, and then determine whether the operation can be performed based on the parity of the Interval Length. If you can perform this operation, the symmetric interval is updated. Note: If the Interval Length is odd, then there must be only one letter with an odd number, so it must be in the middle, then the other letters are updated on both sides in alphabetical order. The approximate complexity of each update is that the query is 26 * lgn, and the update is 26 * lgn, probably ran 2.5 s [cpp] # include <iostream> # include <cstdio> # include <cstring> # define mem (a, B) memset (a, B, sizeof (a) # define N 100005 # define lson step <1 # define rson step <1 | 1 using namespace std; struct Seg_tree {int left, right; int cnt [26], lazy;} L [N <2]; int n, m; char str [N]; int cnt [26]; void push_up (int step) {for (int I = 0; I <26; I ++) L [step]. cnt [I] = L [lson]. cnt [I] + L [rso N]. cnt [I];} void update (int step, int l, int r, int k); void push_down (int step) {if (L [step]. lazy! =-1) {int l = L [step]. left, r = L [step]. right, m = (l + r)> 1; update (lson, l, m, L [step]. lazy); update (rson, m + 1, r, L [step]. lazy); L [step]. lazy =-1 ;}} void bulid (int step, int l, int r) {L [step]. left = l; L [step]. right = r; L [step]. lazy =-1; mem (L [step]. cnt, 0); if (l = r) {L [step]. cnt [str [l]-'a'] ++; L [step]. lazy = str [l]-'A'; return;} int m = (l + r)> 1; bulid (lson, l, m); bulid (rson, m + 1, r); push_up (step);} void query (int st Ep, int l, int r) {if (L [step]. left = l & L [step]. right = r) {for (int I = 0; I <26; I ++) cnt [I] + = L [step]. cnt [I]; return;} int m = (L [step]. left + L [step]. right)> 1; push_down (step); if (r <= m) query (lson, l, r); else if (l> m) query (rson, l, r); else {query (lson, l, m); query (rson, m + 1, r) ;}} void update (int step, int l, int r, int k) {if (L [step]. left = l & L [step]. right = r) {mem (L [step]. cnt, 0); L [step]. cnt [k] = r-l + 1; L [step ]. Lazy = k; return;} push_down (step); int m = (L [step]. left + L [step]. right)> 1; if (r <= m) update (lson, l, r, k); else if (l> m) update (rson, l, r, k); else {update (lson, l, m, k); update (rson, m + 1, r, k);} push_up (step );} void slove (int step) {if (L [step]. left = L [step]. right) {putchar (L [step]. lazy + 'A'); return;} push_down (step); slove (lson); slove (rson);} int main () {freopen ("input.txt ", "r", stdin); freopen ("ou Tput.txt "," w ", stdout); scanf (" % d % s ", & n, & m, str + 1); bulid (1, 1, n ); while (m --) {int l, r; scanf ("% d", & l, & r); mem (cnt, 0); query (1, l, r); if (r-l + 1) & 1) {int k = 0, p; for (int I = 0; I <26; I ++) if (cnt [I] & 1) k ++, p = I; if (k = 1) {int a = l, B = r; cnt [p] --; for (int I = 0; I <26; I ++) {if (cnt [I]) {update (1,, a + cnt [I]/2-1, I); update (1, B-cnt [I]/2 + 1, B, I ); a + = cnt [I]/2; B-= cnt [I]/2;} update (1, a, B, p );}} else {int k = 0; for (in T I = 0; I <26; I ++) if (cnt [I] & 1) {k = 1; break;} if (! K) {int a = l, B = r; for (int I = 0; I <26; I ++) {if (cnt [I]) {update (1, a, a + cnt [I]/2-1, I); update (1, B-cnt [I]/2 + 1, B, I ); a + = cnt [I]/2; B-= cnt [I]/2 ;}}} slove (1); putchar ('\ n '); return 0 ;}# include <iostream >#include <cstdio >#include <cstring> # define mem (a, B) memset (a, B, sizeof ()) # define N 100005 # define lson step <1 # define rson step <1 | 1 using namespace std; struct Seg_tree {int left, right; int cnt [26], lazy;} L [N <2]; int n, m; char str [N]; int cnt [26]; void push_up (int step) {for (int I = 0; I <26; I ++) L [step]. cnt [I] = L [lson]. cnt [I] + L [rson]. cnt [I];} void update (int step, int l, int r, int k); void push_down (int step) {if (L [step]. lazy! =-1) {int l = L [step]. left, r = L [step]. right, m = (l + r)> 1; update (lson, l, m, L [step]. lazy); update (rson, m + 1, r, L [step]. lazy); L [step]. lazy =-1 ;}} void bulid (int step, int l, int r) {L [step]. left = l; L [step]. right = r; L [step]. lazy =-1; mem (L [step]. cnt, 0); if (l = r) {L [step]. cnt [str [l]-'a'] ++; L [step]. lazy = str [l]-'A'; return;} int m = (l + r)> 1; bulid (lson, l, m); bulid (rson, m + 1, r); push_up (step);} void query (int step, I Nt l, int r) {if (L [step]. left = l & L [step]. right = r) {for (int I = 0; I <26; I ++) cnt [I] + = L [step]. cnt [I]; return;} int m = (L [step]. left + L [step]. right)> 1; push_down (step); if (r <= m) query (lson, l, r); else if (l> m) query (rson, l, r); else {query (lson, l, m); query (rson, m + 1, r) ;}} void update (int step, int l, int r, int k) {if (L [step]. left = l & L [step]. right = r) {mem (L [step]. cnt, 0); L [step]. cnt [k] = r-l + 1; L [step]. lazy = K; return;} push_down (step); int m = (L [step]. left + L [step]. right)> 1; if (r <= m) update (lson, l, r, k); else if (l> m) update (rson, l, r, k); else {update (lson, l, m, k); update (rson, m + 1, r, k);} push_up (step );} void slove (int step) {if (L [step]. left = L [step]. right) {putchar (L [step]. lazy + 'A'); return;} push_down (step); slove (lson); slove (rson);} int main () {freopen ("input.txt ", "r", stdin); freopen ("output.txt ", "W", stdout); scanf ("% d % s", & n, & m, str + 1); bulid (1, 1, n ); while (m --) {int l, r; scanf ("% d", & l, & r); mem (cnt, 0); query (1, l, r); if (r-l + 1) & 1) {int k = 0, p; for (int I = 0; I <26; I ++) if (cnt [I] & 1) k ++, p = I; if (k = 1) {int a = l, B = r; cnt [p] --; for (int I = 0; I <26; I ++) {if (cnt [I]) {update (1,, a + cnt [I]/2-1, I); update (1, B-cnt [I]/2 + 1, B, I ); a + = cnt [I]/2; B-= cnt [I]/2;} update (1, a, B, p );}} else {int k = 0; for (int I = 0; I <26; I ++) if (cnt [I] & 1) {k = 1; break;} if (! K) {int a = l, B = r; for (int I = 0; I <26; I ++) {if (cnt [I]) {update (1, a, a + cnt [I]/2-1, I); update (1, B-cnt [I]/2 + 1, B, I ); a + = cnt [I]/2; B-= cnt [I]/2 ;}}} slove (1); putchar ('\ n '); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.