题意:给你一个长度为n的字符串,n <= 100000,有q个查询 q <= 100000,每个查询,i,j,k,代表将从i到j的字符重新排序,如果k==1,就升序,如果k==0,就降序
分析:裸的复杂度太高,肯定是过不了的,注意到只有26个字母,也就是可以log(n)的查询出来一段区间每个字母有多少个,查询出来之后,如果k==1就从’a’到’z’枚举查询出来的字母个数,依次放在这个区间即可,k==0,同理。在这一步,需要区间修改。复杂度:q*log(n)
http://codeforces.com/contest/558/problem/E
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
struct Node {
int x,y;
int num[26];
int lazy;
}t[MAXN<<2];
int n,m;
char s[MAXN];
void Push_Up(int rt) {
for(int i = 0 ; i < 26 ; i++) {
t[rt].num[i] = t[rt<<1].num[i] + t[rt<<1|1].num[i];
}
}
void Push_Down(int rt) {
if(t[rt].lazy != -1) {
t[rt<<1].lazy = t[rt<<1|1].lazy = t[rt].lazy;
memset(t[rt<<1].num,0,sizeof(t[rt<<1].num));
memset(t[rt<<1|1].num,0,sizeof(t[rt<<1|1].num));
t[rt<<1].num[t[rt].lazy] = t[rt<<1].y - t[rt<<1].x + 1;
t[rt<<1|1].num[t[rt].lazy] = t[rt<<1|1].y - t[rt<<1|1].x + 1;
t[rt].lazy = -1;
}
}
void Build(int x,int y,int rt) {
t[rt].x = x ; t[rt].y = y; t[rt].lazy = -1;
if(x == y) {
memset(t[rt].num,0,sizeof(t[rt].num));
t[rt].num[s[x]-'a'] ++;
return ;
}
int mid = (x + y) >> 1;
Build(x,mid,rt<<1);
Build(mid+1,y,rt<<1|1);
Push_Up(rt);
}
void Update(int rt,int left,int right,int lazy) {
if(t[rt].x >= left && t[rt].y <= right) {
memset(t[rt].num,0,sizeof(t[rt].num));
t[rt].lazy = lazy;
t[rt].num[lazy] = t[rt].y - t[rt].x + 1;
return ;
}
int mid = (t[rt].x + t[rt].y) >> 1;
Push_Down(rt);
if(mid >= left) {
Update(rt<<1,left,right,lazy);
}
if(mid < right) {
Update(rt<<1|1,left,right,lazy);
}
Push_Up(rt);
}
int temp_num[26];
void Query(int rt,int left,int right) {
if(t[rt].x >= left && t[rt].y <= right) {
for(int i = 0 ; i < 26 ; i ++) {
temp_num[i] += t[rt].num[i];
}
return ;
}
int mid = (t[rt].x + t[rt].y) >> 1;
Push_Down(rt);
if(mid >= left) {
Query(rt<<1,left,right);
}
if(mid < right) {
Query(rt<<1|1,left,right);
}
Push_Up(rt);
}
void Tree_print(int rt) {
if(t[rt].x == t[rt].y) {
for(int i = 0 ; i < 26 ; i++) {
if(t[rt].num[i]) printf("%c",i+'a');
}
return;
}
Push_Down(rt);
Tree_print(rt<<1);
Tree_print(rt<<1|1);
Push_Up(rt);
}
void input() {
memset(t,0,sizeof(t));
scanf("%s",s+1);
Build(1,n,1);
int x,y,r;
for(int i = 1 ; i <= m ; i++) {
scanf("%d %d %d",&x,&y,&r);
memset(temp_num,0,sizeof(temp_num));
Query(1,x,y);
if(r == 1) {
for(int i = 0 ; i < 26 ; i ++) {
if(temp_num[i]) {
Update(1,x,x+temp_num[i]-1,i);
x = x + temp_num[i];
}
}
}
else {
for(int i = 0 ; i < 26 ; i ++) {
if(temp_num[i]) {
Update(1,y-temp_num[i]+1,y,i);
y = y - temp_num[i];
}
}
}
}
Tree_print(1);
puts("");
}
void solve() {
}
int main(void) {
while(~scanf("%d %d",&n,&m)) {
input();
solve();
}
return 0;
}