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;
}

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

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

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

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

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

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

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

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

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

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

  8. 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. 这题 ...

  9. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

随机推荐

  1. [转载]Web API OData Inlinecount not working

    http://stackoverflow.com/questions/15422831/web-api-odata-inlinecount-not-working

  2. iOS设置tableViewCell之间的间距(去掉UItableview headerview黏性)

    经常在项目中遇到自定义cell的情况,而且要求cell之间有间距,但是系统没有提供改变cell间距的方法,怎么办? 方法1:自定义cell的时候加一个背景View,使其距离contentView的上下 ...

  3. 利用VBS下载EXE文件手法记录

    1.信息来源 疑似朝鲜通过鱼叉攻击韩国统一部记者的APT事件整理 https://mp.weixin.qq.com/s/4IFV31MBNbANnCVaJj7ZPQ https://twitter.c ...

  4. 【Python】如何基于Python写一个TCP反向连接后门

    首发安全客 如何基于Python写一个TCP反向连接后门 https://www.anquanke.com/post/id/92401 0x0 介绍 在Linux系统做未授权测试,我们须准备一个安全的 ...

  5. Nagios配置文件nagios.cfg详解

    这里开始要讲一些Nagios的配置. 首先要看看目前Nagios的主配置路径下有哪些文件.[root@nagios etc]# ll总用量 152-rwxrwxr-x. 1 nagios nagios ...

  6. 纠正几个NANDFLASH很容易犯的错误【转】

    转自:https://blog.csdn.net/pengrui18/article/details/32337297 今天在看别人如何根据物理地址计算NANDFLASH的列地址(column add ...

  7. Linux内核入门(六)—— __attribute__ 机制【转】

    转自:https://blog.csdn.net/yunsongice/article/details/5538020 GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__ ...

  8. Linux监控重要进程的实现方法

    Linux监控重要进程的实现方法 不管后台服务程序写的多么健壮,还是可能会出现core dump等程序异常退出的情况,但是一般情况下需要在无 人为干预情况下,能够自动重新启动,保证服务进程能够服务用户 ...

  9. apollo配置相关

    一.运维 1. 数据库ConfigDB,PortalDB 2. 基础服务 :Config-Service,(Meta-Server),Admin-Service,Portal 3. 应用在SIT.UA ...

  10. 字符串前面加u、r、b的含义

    u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. 一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u:但是中文, ...