题目

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. 前端MVC框架、类库、UI框架选择

    CSS预处理器sass(基于Ruby服务端版)less(客户端版:基于js; 服务端版:基于nodejs) 前端UI框架JqueryMiniUI: http://www.miniui.com/(适用于 ...

  2. BerkeleyDB库简介

    BerkeleyDB库简介 BerkeleyDB(简称为BDB)是一种以key-value为结构的嵌入式数据库引擎: 嵌入式:bdb提供了一系列应用程序接口(API),调用这些接口很简单,应用程序和b ...

  3. FindWindowEx用法

    函数原型:HWND FindWindowEx(HWND hwndParent,HWND hwndChildAfter,LPCTSTR lpszClass,LPCTSTR lpszWindow): 参数 ...

  4. 前端开发者进阶之函数柯里化Currying

    穆乙:http://www.cnblogs.com/pigtail/p/3447660.html 在计算机科学中,柯里化(英语:Currying),又译为卡瑞化或加里化,是把接受多个参数的函数变换成接 ...

  5. 用mixin引入模块后, 方法重名的解析方法

    关于mixin, 经常被问到一个问题是, 方法查找是如何处理的? 特别地, 如果类, 父类, 以及类所包含的mixin中, 都定义有相同名字的方法时, 会发生什么? 答案是, ruby首先会从对象的直 ...

  6. 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?

    file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...

  7. 《征服 C 指针》摘录4:函数 与 指针

    一.指向函数的指针 函数名可以在表达式中被解读成“指向函数的指针”,因此,正如代码清单 2-2 的实验那样,写成 func 就可以取得指向函数的指针. “指向函数的指针”本质上也是指针(地址),所以可 ...

  8. ReactiveCocoa源码拆分解析(三)

    (整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 这一章节主要讨论信号的“冷”与“热” 在RAC的世界 ...

  9. C#高级编程笔记 Day 7, 2016年9月 19日 (泛型)

    1.协变和抗变 泛型接口的协变 如果泛型类型用 out  关键字标注,泛型接口就是协变的.这也意味着返回类型只能是 T. 接口IIndex 与类型T 是协变的,并从一个制度索引器中返回这个类型. pu ...

  10. CentOS 7.0,启用iptables防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...