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 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(线段树)的更多相关文章
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- codeforces 558E A Simple Task 线段树
题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...
- 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 ...
- 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 ...
- 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 ...
- CF #312 E. A Simple Task 线段树
题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...
- CF558E A simple task 线段树
这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...
- [Codeforces558E]A Simple Task 线段树
链接 题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作. 每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序. 最后输出最终 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
随机推荐
- HTML5的离线存储有几种方式?
localStorage长期存储数据,浏览器关闭后数据不丢失: sessionStorage数据在浏览器关闭后自动删除.
- 查看Android内存,cpu
转自https://testerhome.com/topics/2583 一.查看内存 查看Android应用内存: adb shell dumpsys meminfo 1.查看详细的内存: adb ...
- libvirt- Virsh 所有命令详单
help 打印帮助 attach-device 从一个XML文件附加装置 attach-disk 附加磁盘设备 attach-interface 获 ...
- PHP如何知道一个类中所有的方法
当我们使用一个类时既没有源码也没有文档时(尤其是php扩展提供的类,比如mysqli,Redis类),我们该怎么知道这个类中提供了哪些方法,以及每个方法该怎么使用呢,此时就该PHP中强大的反射登场了, ...
- keil中结构体跨文件调用
在a.h中: 定义了, struct ABC{ short a; short b; ```}; 在a.c中(#include "a.h"): 声明了, struct ABC stc ...
- c++Builder Delphi XML 解析例子
XMLDocument; Xml.XMLIntf.hpp Xml.XMLDoc.hpp #include "Xml.Win.msxmldom.hpp" XMLDocument控件 ...
- hibernate事务配置Aop aop:advisor模式
<!-- 使用HibernateTransactionManager管理hibernate事务 --> <bean id="txManager" class=&q ...
- 封装basedao
package com.huawei.common; import java.sql.ResultSet;import java.sql.SQLException; public interface ...
- Linux实战教学笔记23:Inotify事件监控工具
第二十三节 inotify事件监控工具 标签(空格分隔): Linux实战教学笔记-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载, ...
- 【codeforces 1025E】Colored Cubes 【构造】
题意 有一个n*n的棋盘和m个棋子,每个棋子有一个初始位置和一个目标位置,每次移动只能选择一个棋子移动到他相邻的格子,并且花费一秒钟.请你找出一个移动的方法,使得在10800步内将所有棋子移动到目标位 ...