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. 导出文件名带时间信息的dmp文件

    exp system/orcl@orcl owner=aixm file=d:\aixm%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2% ...

  2. 大端模式、小端模式和C#反转

    A.C#大端模式和小端模式. 小端(little-endian)模式:低地址上存放低字节,高地址上存放高字节. 如0x11223344→ byte[] numBytes = new byte[]{ 0 ...

  3. "网页安全政策"(Content Security Policy,缩写 CSP)的来历

    作者:阿里聚安全链接:https://www.zhihu.com/question/21979782/answer/122682029来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  4. Service和Thread的关系

    Service确实是运行在主线程里的,也就是说如果你在Service里编写了非常耗时的代码,程序必定有问题. Android的后台就是指,它的运行是完全不依赖UI的.即使Activity被销毁,或者程 ...

  5. Set8087CW

    Set8087CWThis example accesses the Floating Point Unit (FPU) control register. Try turning floating ...

  6. 跟着太白老师学python 10day 函数的动态参数 *args, **kwargs, 形参的位置顺序

    1. *args 接收实参的位置参数, **kwargs接收实参的关键字参数 def func(*args, **kwargs): print(args, kwargs) func(1, 2, 3, ...

  7. js 的eval()方法 计算某个字符串,并执行其中的的 JavaScript 代码;

    定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval(string) 参数 描述 string 必需.要计算的字符串,其中含有要计算的 Java ...

  8. SData:优雅的数据交换方案

    SData的网址是https://github.com/knat/SData. 数据交换方案可以分为两类:有纲要(schema)的和无纲要的.有纲要的数据交换方案有Google的Protocol Bu ...

  9. 使用JavaScript弹出Confirm对话框

    方法1: 这个比较简单,一句话: <a href="error.htm" onclick="javascript:return confirm('are you s ...

  10. 使apk具有system权限

    使apk具有system权限的方法:   方法一:   1. 在应用程序的AndroidManifest.xml中的manifest节点中加入   android:sharedUserId=" ...