E. A Simple Task
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
This task is very simple. Given a string S of length n and q queries
each query is 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 will contain two integers n,?q (1?≤?n?≤?105, 0?≤?q?≤?50?000),
the length of the string and the number of queries respectively.
Next line contains a string S itself. It contains only lowercase English letters.
Next q lines will 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
10 5 abacdabcda 7 10 0 5 8 1 1 4 0 3 6 0 7 10 1
output
cbcaaaabdd
input
10 1 agjucbvdfk 1 10 1
output
abcdfgjkuv
Note
First sample test explanation:
计数排序,关键是如何维护
计算发现建26棵线段树,复杂度为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-b)/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("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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。