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. OD 实验(十) - 对一个 VB 程序的逆向

    前话: VB 程序用 OD 进行逆向的话,可以先查找相关的变量和字符串,以寻找突破口 变量: __vbaVarTstEq __vbaVarCompEq __vbaVarTstNe __vbaVarCo ...

  2. 第九章 消息总线: Spring Cloud Bus

    在微服务架构的系统中, 我们通常会使用轻量级的消息代理来构建一个共用的消息主题让系统中所有微服务实例都连接上来, 由于该主题中产生的消息会被所有实例监听和消费, 所以我们称它为消息总线. 在总线上的各 ...

  3. centos 6.3 kickstart 装机卡在 unsupported hardware detected 页面

    起因 最近厂里一批 dell 新机器 centos 6.3 装机卡在 Unsupported Hardware Detected 页面,要人肉点击 OK 才能继续装机: Unsupported Har ...

  4. 前端调试利器---nproxy

    前言:习惯了在windows环境中使用Fiddler的童鞋们,是不是感觉它的网络重定向功能很酷,Fiddler能按照你设置的规制捕获网络请求,再指向本地文件,如拦截你的js文件到本地,就能很快的调试线 ...

  5. vue组件重新加载(刷新)

    vue组件重新加载(刷新) 第一种方法:利用v-if控制router-view,在根组件APP.vue中实现一个刷新方法 <template> <router-view v-if=& ...

  6. leetcode575

    public class Solution { public int DistributeCandies(int[] candies) { var dic = new Dictionary<in ...

  7. Variant

    class RTL_DELPHIRETURN Variant: public TVarData Variant转换为字符串 System::Variants::VarToStr VariantArra ...

  8. UML建模之时序图(Sequence Diagram)<转>

    UML建模之时序图(Sequence Diagram)   一.时序图简介(Brief introduction) 二.时序图元素(Sequence Diagram Elements) 角色(Acto ...

  9. AMF_OBJECT 数据结构浅析

    组织的比较散,主要是标记一下有关 AMF_OBJECT 数据组织结构.其标识嵌套结束则为 0x 00 00 09 原始数据结构已知: key=“0123456”: Value 的值是一个结构体如下: ...

  10. 发生在阿里云 SLB 4 层的一次故障记录

    阿里云 SLB 与 ECS 之间发生故事.环境如下: SLB api-node: 该 SLB 后端接着 10 台节点服务器 SLB sql-node: 该 SLB 后端接着 2 台节点服务器 问题描述 ...