http://codeforces.com/problemset/problem/558/E
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.
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 one line, the string S after applying the queries.
10 5 abacdabcda 7 10 0 5 8 1 1 4 0 3 6 0 7 10 1
cbcaaaabdd
10 1 agjucbvdfk 1 10 1
abcdfgjkuv
First sample test explanation:
給你一個串和很多區間排序變換的操作,問最終的串是什麼。
咋看不好做,原來是用線段樹,稍微想一下就很簡單了,用26棵線段樹可以查詢區間的當前字母的個數,然後賦值爲0,再按順序放進去。所以時間是50000*log(1e5)
#include<bits/stdc++.h>
using namespace std;
char s[100005<<2];
int sum[30][100005<<2];
int a[30][100005<<2];
int ss[100005<<2];
int t[30];
void pushup(int rt,int k)
{
sum[k][rt]=(sum[k][rt*2]+sum[k][rt*2+1]);
}
void pushdown(int rt,int m,int k)
{
if(a[k][rt]!=-1)
{
a[k][rt*2]=a[k][rt];
a[k][rt*2+1]=a[k][rt];
sum[k][rt*2]=a[k][rt]*(m-(m>>1));
sum[k][rt<<1|1]=a[k][rt]*(m>>1);
a[k][rt]=-1;
}
}
void build(int l,int r,int rt,int k)
{
a[k][rt]=-1;
if(l==r)
{
sum[k][rt]=0;
return ;
}
int m=(l+r)>>1;
build(l,m,rt*2,k);
build(m+1,r,rt<<1|1,k);
pushup(rt,k);
}
int query(int L,int R,int l,int r,int rt,int k)
{
if(L<=l&&R>=r)
{
return sum[k][rt];
}
pushdown(rt,r-l+1,k);
int m=(l+r)>>1;
int ans=0;
if(L<=m) ans+=query(L,R,l,m,rt*2,k);
if(R>m) ans+=query(L,R,m+1,r,rt<<1|1,k);
return ans;
}
void update(int L,int R,int add,int l,int r,int rt,int k)
{
if(L<=l&&R>=r)
{
sum[k][rt]=add*(r-l+1);
a[k][rt]=add;
return ;
}
pushdown(rt,r-l+1,k);
int m=(l+r)>>1;
if(L<=m) update(L,R,add,l,m,rt*2,k);
if(R>m) update(L,R,add,m+1,r,rt<<1|1,k);
pushup(rt,k);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int i=1; i<=n; i++)
{
ss[i]=s[i]-'a';
}
for(int i=0; i<26; i++)
{
build(1,n,1,i);
}
for(int i=1; i<=n; i++)
{
update(i,i,1,1,n,1,ss[i]);
}
for(int i=0; i<m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
memset(t,0,sizeof(t));
if(c==0)
{
for(int i=0; i<26; i++)
{
t[i]=query(a,b,1,n,1,i);
update(a,b,0,1,n,1,i);
}
for(int i=25; i>=0; i--)
{
if(t[i]==0) continue;
update(a,a+t[i]-1,1,1,n,1,i);
a=a+t[i];
}
}
else
{
for(int i=0; i<26; i++)
{
t[i]=query(a,b,1,n,1,i);
update(a,b,0,1,n,1,i);
}
for(int i=0; i<26; i++)
{
if(t[i]==0) continue;
update(a,a+t[i]-1,1,1,n,1,i);
a=a+t[i];
}
}
}
for(int i=1; i<=n; i++)
{
for(int j=0; j<26; j++)
{
if(query(i,i,1,n,1,j))
{
printf("%c",j+'a');
}
}
}
printf("\n");
return 0;
}