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.

Examples
Input
10 5
abacdabcda
7 10 0
5 8 1
1 4 0
3 6 0
7 10 1
Output
cbcaaaabdd
Input
10 1
agjucbvdfk
1 10 1
Output
abcdfgjkuv
Note
First sample test explanation:

 

题意:

给出一个字符串,两种操作,操作0,将l-r区间降序排序,操作1,将l-r区间升序排序。

输出经过所有操作后的字符串

题解:

建26棵线段树,区间查询个数,点修改,对于每个操作,升序从1-26计数排序,降序从26-1计数排序

最后单点查询

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
using namespace std; struct oper
{
int l,r;
} op[]; struct seg_tree
{
struct node
{
int l,r,lazy,sum;
} tr[]; void push_up(int root)
{
tr[root].sum=tr[lson].sum+tr[rson].sum;
} void push_down(int root)
{
int mid=(tr[root].l+tr[root].r)>>;
tr[lson].sum=(mid-tr[root].l+)*tr[root].lazy;
tr[lson].lazy=tr[root].lazy;
tr[rson].sum=(tr[root].r-mid)*tr[root].lazy;
tr[rson].lazy=tr[root].lazy;
tr[root].lazy=-;
} void build(int root,int l,int r)
{
if(l==r)
{
tr[root].l=l;
tr[root].r=r;
tr[root].lazy=-;
return ;
} int mid=(l+r)>>;
tr[root].l=l;
tr[root].r=r;
tr[root].lazy=-;
build(lson,l,mid);
build(rson,mid+,r);
} void update(int root,int l,int r,int val)
{
if(l>r)
{
return ;
}
if(tr[root].l==l&&tr[root].r==r)
{
tr[root].sum=(tr[root].r-tr[root].l+)*val;
tr[root].lazy=val;
return ;
}
if(~tr[root].lazy)
{
push_down(root);
}
int mid=(tr[root].l+tr[root].r)>>;
if(l>mid)
{
update(rson,l,r,val);
}
else
{
if(mid>=r)
{
update(lson,l,r,val);
}
else
{
update(lson,l,mid,val);
update(rson,mid+,r,val);
}
}
push_up(root);
} int query(int root,int l,int r)
{
if(tr[root].l==l&&tr[root].r==r)
{
return tr[root].sum;
}
if(~tr[root].lazy)
{
push_down(root);
}
int mid=(tr[root].l+tr[root].r)>>;
if(l>mid)
{
return query(rson,l,r);
}
else
{
if(mid>=r)
{
return query(lson,l,r);
}
else
{
return query(lson,l,mid)+query(rson,mid+,r);
}
}
} } tree[]; char c[]; int main()
{
int n,m;
scanf("%d %d",&n,&m);
scanf("%s",c+);
for(register int i=; i<; ++i)
{
tree[i].build(,,n);
}
for(register int i=; i<=n; ++i)
{
tree[c[i]-'a'].update(,i,i,);
}
int l,r,kd,he,ta;
for(; m--;)
{
scanf("%d%d%d",&l,&r,&kd);
ta=l-;
if(kd)
{
for(register int i=; i<; ++i)
{
he=ta+;
ta=he+tree[i].query(,l,r)-;
op[i].l=he;
op[i].r=ta;
}
}
else
{
for(register int i=; i>=; --i)
{
he=ta+;
ta=he+tree[i].query(,l,r)-;
op[i].l=he;
op[i].r=ta;
}
}
for(register int i=; i<; ++i)
{
if(op[i].l<=op[i].r)
{
tree[i].update(,op[i].l,op[i].r,);
tree[i].update(,l,op[i].l-,);
tree[i].update(,op[i].r+,r,);
}
}
} for(register int i=; i<=n; ++i)
{
for(register int j=; j<; ++j)
{
if(tree[j].query(,i,i))
{
putchar(j+);
break;
}
}
}
}

 

 

 

 

CodeForces 588E A Simple Task(线段树)的更多相关文章

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

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

  2. codeforces 558E A Simple Task 线段树

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

  3. 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 ...

  4. 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 ...

  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. CF #312 E. A Simple Task 线段树

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

  7. CF558E A simple task 线段树

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

  8. [Codeforces558E]A Simple Task 线段树

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

  9. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

随机推荐

  1. Python 模块 - jieba

    安装 jieba pip3 install jieba jieba 支持三种分词模式: 精确模式:将句子最精确地切开,适合文本分析 全模式:把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不 ...

  2. Python模块学习 ---- datetime

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于time模块, ...

  3. C语言学习笔记---好用的函数memcpy与memset

    这个主要用于我个人的学习笔记,便于以后查询,顺便分享给大家. 想必在用C的时候难免会与数组,指针,内存这几样东西打交道,先以数组为例,例如有一个数组int a[5] = {1, 2, 3, 4, 5} ...

  4. CentOS7.6安装稳定版Nginx

    官网地址:http://nginx.org/en/linux_packages.html#RHEL-CentOS 需先安装依赖:sudo yum install -y yum-utils 安装开始: ...

  5. linux weather

  6. 星型打分插件 bootstrap-rating-input

    最近帮人实现一个打分的功能,发现bootstrap-rating-input是个简单又好用的星型打分,我对其做了些定制,添加了分值说明,并修改了样式,毕竟 bootstrap 自身的黑色五角星还是不够 ...

  7. Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库

    1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...

  8. leetcode419

    public class Solution { public int CountBattleships(char[,] board) { );//3行 );//4列 ; ; i < row; i ...

  9. 转 Jquery实际应用,判断radio,selelct,checkbox是否选中及选中的值

    jquery取radio单选按钮的值   $("input[name='items']:checked").val();  另:判断radio是否选中并取得选中的值  如下所示: ...

  10. ROS解决网页断流现象 (转)

    “pppoe-client”接口,将“MAX MTU”和“MAX MRU”都设置成“” /ip firewall mangle add action=change-mss chain=forward ...