题目

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. ubuntu/var/log/下各个日志文件

    ubuntu/var/log/下各个日志文件 本文简单介绍ubuntu/var/log/下各个日志文件,方便出现错误的时候查询相应的log   /var/log/alternatives.log-更新 ...

  2. Bash 中的 _ 是不是环境变量

    首先,我们想到的会是 export(等价于 declare -x)命令: $ export | grep 'declare -x _=' 没有找到,那么结论就是 _ 不是环境变量?当然没那么简单,否则 ...

  3. Here String 中不该进行分词

    我们知道,在 Shell 中,一个变量在被展开后,如果它没有被双引号包围起来,那么它展开后的值还会进行一次分词(word splitting,或者叫拆词,分词这个术语已经被搜索引擎相关技术占用了)操作 ...

  4. sql clear dblog

    USE [master]GOALTER DATABASE ndFlightPolicy SET RECOVERY SIMPLE WITH NO_WAITGOALTER DATABASE ndFligh ...

  5. How to generate UML Diagrams from Java code in Eclipse

    UML diagrams compliment inline documentation ( javadoc ) and allow to better explore / understand a ...

  6. .net错误日志记录(log4)

    Log4 web.config <!--这段放前面--> <configSections> <section name="log4net" type= ...

  7. mongo副本集搭建及服务器复用方案

    比较常见的mongodb副本集搭建是有:常规节点.数据副本.仲裁节点组成,也就是需要三台服务器组建.常规节点即数据的主存储节点,数据副本是主存储节点的从属节点,它定期去主节点获取更新日志来更新自己.仲 ...

  8. js string 转 int Number()

    var numString = '122'; var numberInt = Number(numString); var res = numberInt/2; 结果: res = 61

  9. .NET 委托

    委托类型定义      C#编译器处理委托时,先自动产生一个派生自System.MulticastDelegate的密封类.这个类与它的基类System.Delegate一起为委托提供必要的基础设施, ...

  10. Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)

    在IDEA中将项目部署到本地Tomcat下进行运行并验证整合结果 (1).点击如下图所示的下拉按钮,弹出Edit Configurations...后点击该项. (2).跳出如下界面后,点击红框内的& ...