E. A Simple Task

time limit per test5 seconds

memory limit per test512 megabytes

inputstandard input

outputstandard output

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 test(s)

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:

题意,给一段字符串。然后一系列改动i,j之间的字符串从小到大排序,或从大到小排序。维护26的线段树。i,j改动。仅仅须要查询一个a - z的个数,按计数排序的原理,更新一下线段树就能够了。总的复杂度为o(26 * q * log(n));速度有点慢,用伸展树应该快些。由于是成段更新,所以要用延时标记,记-1不更新,0全置0,1全置1,仅仅有这点小技巧就能够了。

#define N 100005
#define M 100005
#define maxn 205
#define SZ 26
#define MOD 1000000000000000007
#define lson (now<<1)
#define rson (now<<1|1)
int n,q,ss,ee,k,num[SZ];
char str[N];
struct node{
int c,sum,l,r;
};
node tree[SZ][N*4];
void pushDown(int tn,int now){
if(tree[tn][now].c != -1){
tree[tn][lson].c = tree[tn][rson].c = tree[tn][now].c;
tree[tn][lson].sum = tree[tn][lson].c*(tree[tn][lson].r-tree[tn][lson].l + 1);
tree[tn][rson].sum = tree[tn][rson].c*(tree[tn][rson].r-tree[tn][rson].l + 1);
tree[tn][now].c = -1;
}
}
void pushUp(int tn,int now){
tree[tn][now].sum = tree[tn][lson].sum + tree[tn][rson].sum ;
}
void buildTree(int l,int r,int now){
FI(SZ)
tree[i][now].c = -1,tree[i][now].l = l,tree[i][now].r = r,tree[i][now].sum = 0;
if(l >= r){
return ;
}
int mid = (l+r)>>1;
buildTree(l,mid,lson);
buildTree(mid+1,r,rson);
}
void updateTree(int l,int r,int now,int s,int e,int tn,int c){
if(s <= l && e>= r){
tree[tn][now].c = c;
tree[tn][now].sum = tree[tn][now].c*(tree[tn][now].r - tree[tn][now].l + 1);
return ;
}
pushDown(tn,now);
int mid = (l+r)>>1;
if(s <= mid) updateTree(l,mid,lson,s,e,tn,c);
if(e > mid) updateTree(mid+1,r,rson,s,e,tn,c);
pushUp(tn,now);
}
int queryTree(int l,int r,int now,int s,int e,int tn){
if(s <= l && e>= r){
return tree[tn][now].sum;
}
pushDown(tn,now);
int mid = (l+r)>>1;
int ans = 0;
if(s <= mid) ans += queryTree(l,mid,lson,s,e,tn);
if(e > mid) ans += queryTree(mid+1,r,rson,s,e,tn);
pushUp(tn,now);
return ans;
}
void outputStr(){
FI(n){
FJ(SZ){
if(queryTree(1,n,1,i+1,i+1,j)){
printf("%c",j+'a');
break;
}
}
}
printf("\n");
}
int main()
{
while(S2(n,q)!=EOF)
{
SS(str);
buildTree(1,n,1);
FI(n){
updateTree(1,n,1,i+1,i+1,str[i] - 'a',1);
}
FI(q){
S2(ss,ee);S(k);
FJ(SZ)
num[j] = queryTree(1,n,1,ss,ee,j);
FJ(SZ)
updateTree(1,n,1,ss,ee,j,0);
if(k){
int sum = 0;
FJ(SZ){
if(num[j])
updateTree(1,n,1,ss + sum,ss + sum + num[j] - 1,j,1);
sum += num[j];
}
}
else {
int sum = 0;
for(int j = SZ - 1;j>=0;j--){
if(num[j])
updateTree(1,n,1,ss + sum,ss + sum + num[j] - 1,j,1);
sum += num[j];
}
}
}
outputStr();
}
return 0;
}

Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记的更多相关文章

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

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

  3. Codeforces Round #312 (Div. 2) E. A Simple Task

    题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次 ...

  4. Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

  5. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  6. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  7. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  8. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  9. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

随机推荐

  1. centeros安装jdk

    准备工作: java se下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...

  2. 【Loadrunner】Vugen录制脚本为空的解决办法

    1. ie -> 工具 -> Internet选项 - 高级 - 勾除“启动第三方浏览器扩展选项”,然后重启电脑 2. 还有一种可能,就是机器上安装了多个浏览器,从而导致出现问题,解决方法 ...

  3. HDU 4939 Stupid Tower Defense 简单DP

    题意: 地图为长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后格子造成y点伤害,蓝塔可以使通过单位长度的时间增加z秒. 让你安排塔的排 ...

  4. ZJU 1346 Comparing Your Heroes 状态压缩DP 拓扑排序的计数

    做多校的时候遇见一个求拓扑排序数量的题,就顺便来写了一下. 题意: 你有个朋友是KOF的狂热粉丝,他有一个对其中英雄的强弱比较,让你根据这些比较关系来给这些英雄排名.问一共有多少种排名方式. 思路: ...

  5. linux下oracle11G DG搭建(三):环绕备库搭建操作

    linux下oracle11G DG搭建(三):环绕备库搭建操作 环境 名称 主库 备库 主机名 bjsrv shsrv 软件版本号 RedHat Enterprise5.5.Oracle 11g 1 ...

  6. 同学们,OpenCV出3.0了,速去围观!

    OpenCV3.0 OpenCV > NEWS > OpenCV 3.0 2015-06-04 With a great pleasure and great relief OpenCV ...

  7. 怎样给UINavigationBar加入button?

    Mads Mobæk:给UINavigationBar加入button的演示样例代码 1 2 3 4 5 6 7 8 UIBarButtonItem *rightButton = [[UIBarBut ...

  8. ant安装配置问题:ANT_HOME is set incorrectly or ant could not be located. Please set ANT_HOME.

    项目用到ant 1.去官网下载:http://ant.apache.org/bindownload.cgi 2.解压至安装C盘 3.设置ANT_HOME.PATh.CLASSPATH ANT_HOME ...

  9. thinkphp5项目--企业单车网站(二)

    thinkphp5项目--企业单车网站(二) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  10. hdoj--5333--Dancing Stars on Me(水题)

    Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...