题目

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. cinnamon桌面安装在其他目录下

    cinnamon桌面还不错,不过默认只能安装在/usr目录下 有很多脚本中写死了是/usr目录 编译时如下模块需要打补丁: 1.cinnamon中,需要执行 sed -i 's|usr/share|u ...

  2. wcf第4步之原生调用简单封装

    public interface IDemoServiceChannel : IDemoService, System.ServiceModel.IClientChannel { } public p ...

  3. thinkphp 后台权限列表

    核心代码: // 检测用户权限权限 public function admin_priv($action){ $action_list = session('user.action_list'); i ...

  4. PHP mkdir 0777权限问题

    在linux系统中,即使我们使用root帐号去手工执行php命令: mkdir('test', 0777); 结果文件的权限依然为: drwxr-xr-x 2 root root 4096 Jun 1 ...

  5. 父类方法返回子类实例:PHP延迟静态绑定

    案例分析 先前的PHP项目中,看到类似于以下的一段代码: <?php class DBHandler { public function get() { } } class MySQLHandl ...

  6. MongoDB Windows环境安装及配置

    MongoDB一般安装 1.首先到官网(http://www.mongodb.org/downloads )下载合适的安装包,目前的最新版本为2.6 安装包有zip和msi格式的,这里推荐下载zip格 ...

  7. Asp.Net Core--自定义基于策略的授权

    翻译如下: 在封面下,角色授权和声明授权使用需求,需求的处理程序和预配置的策略. 这些构建块允许您在代码中表示授权评估,从而允许更丰富,可重用和容易测试的授权结构. 授权策略由一个或多个需求组成,并在 ...

  8. eclipse for php现有项目不能导入问题

    1.少了.project文件 解决办法:创建一个新项目,然后将新项目文件夹下的.project文件复制到将要导入的文件夹中.

  9. python基础一

    1.1 Python优点 1.简单.优雅.明确 2.强大的模块三方库 3.易移植 4.面向对象 5.可扩展(c\java\c#...) 1.2 Python缺点 1.代码不能加密 2.速度慢   1. ...

  10. ASP.NET MVC中的两个Action之间值的传递--TempData

    一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...