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

Source: Internet
Author: User
Tags sort time limit

E. A simple Task time limit per test 5 seconds memory limit per test megabytes input standard input Output standard OU Tput

This task was very simple. Given A string S of length n and q queries each query are on the format I J K which means sort the substring consisting of The characters from I to J in non-decreasing order if k = 1 or in non-increasing order if k = 0.

Output the final string after applying the queries. Input

The first line would contain, integers n, q (1≤n≤105, 0≤q≤50), the length of the string and the number of Qu Eries respectively.

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

Output one line, the string S after applying the queries. Sample Test (s) input

5
abacdabcda
7 0
5 8 1
1 4 0
3 6 0
7 10 1
Output
Cbcaaaabdd
Input
1
AGJUCBVDFK
1 10 1
Output
Abcdfgjkuv
Note

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]=m
	ark[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 ("E.in "," 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;
 }





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.