E. A Simple Task

题目连接:

http://www.codeforces.com/contest/558/problem/E

Description

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 Input

10 5

abacdabcda

7 10 0

5 8 1

1 4 0

3 6 0

7 10 1

Sample Output

cbcaaaabdd

Hint

题意

给你n个字符,一共俩操作,l到r区间升序排序,l到r降序排序

字符集26.

让你输出最后的字符样子。

题解:

考虑计数排序,我们用线段树维护计数排序就好了,这样复杂度是26*q*logn

感觉自己智商还是涨了一波……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
struct Seg{
typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , lazy;
void update(SgTreeDataType v)
{
sum = (R-L+1)*v;
lazy = v;
}
}; treenode tree[maxn]; inline void push_down(int o)
{
SgTreeDataType lazyval = tree[o].lazy;
if(lazyval==-1)return;
tree[2*o].update(lazyval) ; tree[2*o+1].update(lazyval);
tree[o].lazy = -1;
} inline void push_up(int o)
{
tree[o].sum = tree[2*o].sum + tree[2*o+1].sum;
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = 0,tree[o].lazy = -1;
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
}
} inline void update(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].update(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) update(QL,QR,v,o*2);
if (QR > mid) update(QL,QR,v,o*2+1);
push_up(o);
}
} inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res = 0;
if (QL <= mid) res += query(QL,QR,2*o);
if (QR > mid) res += query(QL,QR,2*o+1);
push_up(o);
return res;
}
}
}T[26]; char s[maxn];
int cnt[26];
int main()
{
int n,q;
scanf("%d%d",&n,&q);
scanf("%s",s+1);
for(int i=0;i<26;i++)
T[i].build_tree(1,n,1);
for(int i=1;i<=n;i++)
T[s[i]-'a'].update(i,i,1,1);
for(int i=1;i<=q;i++)
{
int op,a,b;
scanf("%d%d%d",&a,&b,&op);
if(op==1)
{
for(int i=0;i<26;i++)
cnt[i]=T[i].query(a,b,1);
for(int i=0;i<26;i++)
T[i].update(a,b,0,1);
int l=a;
for(int i=0;i<26;i++)
T[i].update(l,l+cnt[i]-1,1,1),l+=cnt[i];
}
else
{
for(int i=25;i>=0;i--)
cnt[i]=T[i].query(a,b,1);
for(int i=25;i>=0;i--)
T[i].update(a,b,0,1);
int l=a;
for(int i=25;i>=0;i--)
T[i].update(l,l+cnt[i]-1,1,1),l+=cnt[i];
}
}
for(int i=1;i<=n;i++)
for(int j=0;j<26;j++)
if(T[j].query(i,i,1))
printf("%c",j+'a');
return 0;
}

最新文章

  1. Node.js 安装配置
  2. document.documentElement.clientHeight 和 $(window).height() 无法正确获取页面可视区高度
  3. iframe显示错误页面
  4. [Effective JavaScript 笔记] 第8条:尽量少用全局对象
  5. (06)odoo报表
  6. mysql学习之-三种安装方式与版本介绍
  7. Oracle数据库中文乱码问题
  8. javascript_04 数据类型
  9. C++之vector用法
  10. java web 实现验证码
  11. Intellij IDEA 新建一个EJB工程(三)
  12. AjaxUpLoad.js使用实现文件上传
  13. A Tour of Go Concurrency
  14. struts2 I18n问题 国际化
  15. nslookup命令详解
  16. js中冒泡事件
  17. 单文件文件上传到服务器(HTML5+js+Java)
  18. jQuery与别的js框架冲突
  19. AOP及spring AOP的使用
  20. Spring MVC CORS 跨域

热门文章

  1. 一致性哈希算法介绍,及java实现
  2. Java SSM框架之MyBatis3(二)MyBatis之Mapper代理的开发方式
  3. html5 canvas 多个填充渐变形状
  4. 数链剖分(树的统计Count )
  5. JS种this的四种用法
  6. ruby http爬虫中的 :body 用法问题
  7. 【网络编程】使用getnameinfo()/getaddrinfo()/InetPton()
  8. linux,mac安装sentry
  9. Python学习三|列表、字典、元组、集合的特点以及类的一些定义
  10. 文字小于12px时,设置line-height不居中问题