题目

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. nginx 499 状态码优化

    在grafana界面中发现不少499的状态码,在网上了解到出现499的原因大体都是说服务端处理时间过长,客户端主动关闭了连接.   既然原因可能是服务端处理时间太长了,看一下upstream_resp ...

  2. URL Schemes

    APP 被唤醒离不开对URL Schemes的认知. 苹果选择沙盒来保障用户的隐私和安全,但沙盒也阻碍了应用间合理的信息共享,于是有了 URL Schemes 这个解决办法. URL Schemes ...

  3. hzwer模拟赛 虫洞

    [题目描述] N个虫洞,M条单向跃迁路径.从一个虫洞沿跃迁路径到另一个虫洞需要消耗一定量的燃料和1单位时间.虫洞有白洞和黑洞之分.设一条跃迁路径两端的虫洞质量差为delta. 1.从白洞跃迁到黑洞,消 ...

  4. linux下实现在程序运行时的函数替换(热补丁)

    声明:以下的代码成果,是参考了网上的injso技术,在本文的最后会给出地址,同时非常感谢injso技术原作者的分享. 但是injso文章中的代码存在一些问题,所以后面出现的代码是经过作者修改和检测的. ...

  5. JSP动作元素之include

    采用include指令导入的页面输入静态导入,采用<jsp:include-/>指令属于动态导入. 语法格式如下: <jsp:include page="{relative ...

  6. ZOJ 3703 Happy Programming Contest

    偏方记录背包里的物品.....每个背包的价值+0.01 Happy Programming Contest Time Limit: 2 Seconds      Memory Limit: 65536 ...

  7. 关于requirejs

    24718-12042010 00001h6wzKLpfo3gmjJ8xoTPw5mQvY YA8vwka9tH!vibaUKS4FIDIkUfy!!f 3C"rQCIRbShpSlDcFT ...

  8. Matlab学习笔记(一)—— 三维图形绘制

    这学期公选课选的是MATLAB,所以准备把这学期所学习的整理到博客上,作为记录,哇咔咔~ 一.三维函数图: x=cos(t), y=sin(t), z=t %≤t ≤*pi t=:*pi; %t的取值 ...

  9. url和urn和uri

    1.需求 理清三者的关系 2.例子 3.说明 url是资源的位置(包含scheme),urn表示资源的名字.url是唯一的.urn不是. 他们2个都是uri的子集 参考资料:https://danie ...

  10. [Keygen]IntelliJ IDEA 14.1.7

    IntelliJ IDEA 14.1.7 Keygen package com.candy.keygen.intelliJIdea; import java.math.BigInteger; impo ...