题目

Source

http://codeforces.com/problemset/problem/558/E

Description

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 Input

10 5
abacdabcda
7 10 0
5 8 1
1 4 0
3 6 0
7 10 1

10 1
agjucbvdfk
1 10 1

Sample Output

cbcaaaabdd

abcdfgjkuv

分析

题目大概说给一个由26个小写英文字母组成的序列,进行若干次操作,每次将一个区间升序或降序,问序列最后是怎样的。

26个线段树搞。。没什么。。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111111 struct Ret{
int ret[26];
void operator+=(const Ret &r){
for(int i=0; i<26; ++i){
ret[i]+=r.ret[i];
}
}
};
int sum[26][MAXN<<2],tag[26][MAXN<<2];
int N,x,y,z;
Ret query(int i,int j,int k){
if(x<=i && j<=y){
Ret r={0};
for(int t=0; t<26; ++t) r.ret[t]+=sum[t][k];
return r;
}
int mid=i+j>>1;
for(int t=0; t<26; ++t){
if(tag[t][k]){
tag[t][k<<1]=tag[t][k];
tag[t][k<<1|1]=tag[t][k];
if(tag[t][k]==1){
sum[t][k<<1]=mid-i+1;
sum[t][k<<1|1]=j-mid;
}else{
sum[t][k<<1]=0;
sum[t][k<<1|1]=0;
}
tag[t][k]=0;
}
}
Ret r={0};
if(x<=mid) r+=query(i,mid,k<<1);
if(y>mid) r+=query(mid+1,j,k<<1|1);
return r;
}
void update(int i,int j,int k,int flag){
if(x>y) return;
if(x<=i && j<=y){
tag[z][k]=flag;
if(flag==1) sum[z][k]=j-i+1;
else sum[z][k]=0;
return;
}
int mid=i+j>>1;
if(tag[z][k]){
tag[z][k<<1]=tag[z][k];
tag[z][k<<1|1]=tag[z][k];
if(tag[z][k]==1){
sum[z][k<<1]=mid-i+1;
sum[z][k<<1|1]=j-mid;
}else{
sum[z][k<<1]=0;
sum[z][k<<1|1]=0;
}
tag[z][k]=0;
}
if(x<=mid) update(i,mid,k<<1,flag);
if(y>mid) update(mid+1,j,k<<1|1,flag);
sum[z][k]=sum[z][k<<1]+sum[z][k<<1|1];
} char str[MAXN];
int main(){
int n,m;
scanf("%d%d%s",&n,&m,str+1);
for(N=1; N<n; N<<=1);
for(int i=1; i<=n; ++i){
x=i; y=i; z=str[i]-'a';
update(1,N,1,1);
} int a;
while(m--){
scanf("%d%d%d",&x,&y,&a);
Ret r=query(1,N,1);
for(z=0; z<26; ++z){
update(1,N,1,-1);
}
if(a==1){
int now=x;
for(z=0; z<26; ++z){
x=now; y=now+r.ret[z]-1;
update(1,N,1,1);
now+=r.ret[z];
}
}else{
int now=x;
for(z=25; z>=0; --z){
x=now; y=now+r.ret[z]-1;
update(1,N,1,1);
now+=r.ret[z];
}
}
}
for(int i=1; i<=n; ++i){
x=i; y=i;
Ret r=query(1,N,1);
for(int j=0; j<26; ++j){
if(r.ret[j]) putchar(j+'a');
}
}
return 0;
}

Codeforces558E A Simple Task(线段树)的更多相关文章

  1. [Codeforces558E]A Simple Task 线段树

    链接 题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作. 每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序. 最后输出最终 ...

  2. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

  3. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序

    题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...

  4. CodeForces 588E A Simple Task(线段树)

    This task is very simple. Given a string S of length n and q queries each query is on the format i j ...

  5. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记

    E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...

  6. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  7. CF #312 E. A Simple Task 线段树

    题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...

  8. CF558E A simple task 线段树

    这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...

  9. codeforces 558E A Simple Task 线段树

    题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...

随机推荐

  1. jQuery animate动画 stop()方法详解~

    一.动画格式: 格式一:jQueryObject.animate( cssProperties, options ) 格式二:$('#id').animate( styles[, duration ] ...

  2. 搭建haproxy

    1:下载haproxy wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz2:解压,编译,安装 tar zxf hapr ...

  3. C++内存动态分配

    https://www.percona.com/blog/2012/07/05/impact-of-memory-allocators-on-mysql-performance/ https://su ...

  4. js 无线弹窗

    无限弹窗 function fad(action){ alert('); ht = setTimeout() } fad(); fad()能放在 $().ready() 里面,效果也一样

  5. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?

    什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...

  6. Thrift的TCompactProtocol紧凑型二进制协议分析

    Thrift的紧凑型传输协议分析: 用一张图说明一下Thrift的TCompactProtocol中各个数据类型是怎么表示的. 报文格式编码: bool类型: 一个字节. 如果bool型的字段是结构体 ...

  7. shell--3.运算符

    1.注意 原生bash不支持简单的数学运算,但是可以用其它命令来实现如 awk 和expr ,expr最常用 val=`expr 2 + 3` echo "结果 ${val}" # ...

  8. H5案例分享:使用JS判断客户端、浏览器、操作系统类型

    使用JS判断客户端.浏览器.操作系统类型 一.JS判断客户端类型 JS判断客户端是否是iOS或者Android手机移动端 通过判断浏览器的userAgent,用正则来判断手机是否是ios和Androi ...

  9. c++笔记整理

    一:导读 假设编写了一个C++程序,如何让他允许起来呢,这取决于计算机环境和所使用的C++编译器. 1.使用文本编辑器编写程序,并将其保存在文档中,====此就是源代码 2.编译源代码,编译过程就意味 ...

  10. 【原创】Redux 卍解

    Redux 卍解 Redux - Flux设计模式的又一种实现形式. 说起Flux,笔者之前,曾写过一篇<ReFlux细说>的文章,重点对比讲述了Flux的另外两种实现形式:『Facebo ...