CF 558E (A simple task-counting sort + line tree)

Source: Internet
Author: User

E. A simple tasktime limit per test5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

This task was very simple. Given a stringSof lengthNandQQueries each query was on the formatI JkWhich means sort the substring consisting of the characters fromIToJIn non-decreasing order if k. =?1or in non-increasing order if k. =?0.

Output the final string after applying the queries.


The First line would contain, Integers  n ,? Q   ( 1?≤? n ? ≤?105 ,  0?≤? q ? ≤?50?000 ), the length of the string and the number of queries respectively.

< Span class= "TEX-FONT-STYLE-BF" style= "Font-weight:bold" >next line contains a string  S  itself. It contains only lowercase 中文版 letters.

Next Q lines would contain three integers each i,? J,? k (1?≤? ) I? ≤? j. ≤? n,).


Output one line, the string S after applying the queries.

Sample Test (s) input
5ABACDABCDA7 10 05 8 11 4 03 6 07 10 1

First sample test explanation:

Counting sequencing, the key is how to maintain

Calculation found 26 tree segment, the complexity of O (M*26LOGN)

#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (o<<1) #define Rson ((o<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)); #define INF (2139062143) #define F (100000007) #define MAXN (400000+10) #define MAXQ (50000+10) typedef Long Long Ll;ll mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} int N,q;int L,r,v,_ans;class STree{public:int MARK[MAXN],TREE[MAXN]; Stree () {mem (Mark) mem (tree)}void pushdown (int o,int l,int R) {if (l<r) if (Mark[o]) Mark[lson]=mark[rson]=mark[o], mark[o]=0;} void maintain (int o,int l,int R) {if (Mark[o]) tree[o]= (r-l+1) * (mark[o]-1); else if (l<r) Tree[o]=tree[lson]+tree[rson ];} void set (int o,int l,int R) {if (l<=l&&r<=r) {mark[o]=v;maintain (o,l,r); return;} Pushdown (o,l,r); int m= (L+R) >>1;if (l<=m) set (LSON,L,M); else maintain (lson,l,m); if (m<r) set (RSON,M+1,R); else maintain (rson,m+1,r); maintain (o,l,r);} void get_sum (int o,int l,int R) {if (l<=l&&r<=r) {_ans+=tree[o];return;} if (Mark[o]) {_ans+= (min (r,r)-max (l,l) +1) * (mark[o]-1); return;} int m= (L+R) >>1;if (l<=m) get_sum (lson,l,m), if (m<r) get_sum (rson,m+1,r);}} S[26];char s[maxn];int Cnt[26]={0};int Main () {//freopen ("", "R", stdin);//freopen (". Out", "w", stdout); scanf ("%d% d%s ", &n,&q,s); Rep (i,n) {l=r=i+1,v=2; s[s[i]-' A '].set (1,1,n);} for (qcase,q) {int b;scanf ("%d%d%d", &l,& r,&b); v=1; Rep (i,26) {_ans=0; S[i].get_sum (1,1,n); Cnt[i]=_ans; S[i].set (1,1,n);} V=2;if (b)//increasing{rep (i,26) {if (!cnt[i]) continue;r=l+cnt[i]-1; S[i].set (1,1,n); l=r+1;} } else {Rep (i,26) {if (!cnt[i]) continue;l=r-cnt[i]+1; S[i].set (1,1,n); r=l-1;} }}for (I,n) {Rep (j,26) {l=r=i;_ans=0; S[j].get_sum (1,1,n); if (_ans) {s[i-1]= ' a ' +j;break;}}} printf ("%s\n", s); return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CF 558E (A simple task-counting sort + line tree)

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: 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.