题目大意就是给一个字符串,然后多个操作。每次操作能够把每一段区间的字符进行升序或者降序排序,问终于的字符串是如何的。

做法的话就是用线段树维护区间和

  一開始仅仅考虑字符串中字符'a'的情况。如果操作区间[L,R]中有x个'a',那么一次操作后,这x个'a'要么去最左(升序)。要么去最右(降序),我们能够建立一颗线段树来维护这种操作,字符'a'出现的位置值为1,否则为0,那么q次操作后,最后值为1的地方填的就是'a'了。

  然后,在考虑字符'a'和'b'的情况,操作的情况和上面类似,字符'a'和'b'出现的位置值为1。否则为0,q次操作后,假设一个位置的值为1。而且该位置没有填写'a'。那么这个位置就填写'b'。

接下来的情况以此类推,考虑abc。abcd,......,abcd...z。

时间复杂度O(26*(n+q)logn)。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
struct Tree
{
int l,r,sum;
}tree[int(4e5)];
void create(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].sum=0;
if(l==r)
return;
int m=l+r>>1;
create(l,m,k<<1);
create(m+1,r,k<<1|1);
}
void lazy(int k)
{
if(tree[k].l!=tree[k].r&&tree[k].sum!=tree[k<<1].sum+tree[k<<1|1].sum)
{
if(tree[k].sum==0)
tree[k<<1].sum=tree[k<<1|1].sum=0;
else
{
tree[k<<1].sum=tree[k<<1].r-tree[k<<1].l+1;
tree[k<<1|1].sum=tree[k<<1|1].r-tree[k<<1|1].l+1;
}
}
}
void update(int l,int r,bool flag,int k)
{
lazy(k);
if(l==tree[k].l&&r==tree[k].r)
{
tree[k].sum=flag?r-l+1:0;
return;
}
int m=tree[k].l+tree[k].r>>1;
if(r<=m)
update(l,r,flag,k<<1);
else if(l>m)
update(l,r,flag,k<<1|1);
else
{
update(l,m,flag,k<<1);
update(m+1,r,flag,k<<1|1);
}
tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
}
int seek(int l,int r,int k)
{
lazy(k);
if(l==tree[k].l&&r==tree[k].r)
return tree[k].sum;
int m=tree[k].l+tree[k].r>>1;
if(r<=m)
return seek(l,r,k<<1);
if(l>m)
return seek(l,r,k<<1|1);
return seek(l,m,k<<1)+seek(m+1,r,k<<1|1);
}
void update(int l,int r,bool flag)
{
int t=seek(l,r,1);
if(flag)
{
if(l<=l+t-1)
update(l,l+t-1,1,1);
if(l+t<=r)
update(l+t,r,0,1);
}
else
{
if(r-t+1<=r)
update(r-t+1,r,1,1);
if(l<=r-t)
update(l,r-t,0,1);
}
}
struct Box
{
int l,r;
bool flag;
}box[int(1e5)+10];
int ans[int(1e5)+10];
int main()
{
int n,q;
cin>>n>>q;
create(1,n,1);
string s;
cin>>s;
for(int i=0;i<q;i++)
cin>>box[i].l>>box[i].r>>box[i].flag;
for(int i=0;i<26;i++)
{
update(1,n,0,1);
for(int j=0;j<n;j++)
if(s[j]<=i+'a')
update(j+1,j+1,1,1);
for(int j=0;j<q;j++)
update(box[j].l,box[j].r,box[j].flag);
for(int j=0;j<n;j++)
if(ans[j]==0&&seek(j+1,j+1,1)==1)
ans[j]=i+'a';
}
for(int i=0;i<n;i++)
putchar(ans[i]);
}
time limit per test

5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard 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:


codeforces 558 E A Simple Task的更多相关文章

  1. 【30.93%】【codeforces 558E】A Simple Task

    time limit per test5 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  2. 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task

    E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...

  3. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

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

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

  6. A Simple Task CodeForces - 11D

    A Simple Task CodeForces - 11D 题意:输出一个无向图的简单环数量.简单环指无重复边的环.保证图无重边自环. ans[i][j]表示"包含i中的点,以i中第一个点 ...

  7. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

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

随机推荐

  1. spring返回@ResponseBody报406

    HTTP Status 406 - type Status report message description The resource identified by this request is  ...

  2. HQL连接查询和注解

    HQL连接查询和注解 一:HQL连接查询 各种连接查询: 内连接:inner join或join From Entity inner [inner] join [fetch] Entity.prope ...

  3. iOS开发中获取视图在屏幕上显示的位置

    在iOS开发中,我们会经常遇到一个问题,例如,点击一个按钮,弹出一个遮罩层,上面显示一个弹框,弹框显示的位置在按钮附近.如果这个按钮的位置相对于屏幕边缘的距离是固定的,那就容易了,可以直接写死位置.可 ...

  4. 【分享】纯jQuery实现星巴克官网导航栏效果

    前言 大冬天的没得玩,只能和代码玩. 所以就无聊研究了一下星巴克官网,在我看来应该是基本还原吧~ 请各位大神指教! 官网效果图 要写的就是最上方的会闪现的白色条条 效果分析 1.在滚动条往下拉到一定距 ...

  5. File I/O

    File I/O Introduction     We'll start our discussion of the UNIX System by describing the functions ...

  6. SQL Server 在生产环境中这样写存储过程的坑都避免了吗?

    概述 最近因为业务的需求写了一段时间存储过程,发现之前写的存储过程存在一些不严谨的地方,特别是TRY...CATCH中嵌套事务的写法:虽然之前写的并没有错,但是还是埋藏着很大的隐患在里面.希望这篇文章 ...

  7. SICK激光雷达LMS511测量数据说明

    帧结构说明 LMS511的官方手册存在几个版本,在<Laser Measurement Systems of the LMS500 Product Family>的英文手册中,对单次(连续 ...

  8. 【转载】 Java:按值传递还是按引用传递详细解说

    前天在做系统的时候被Java中参数传递问题卡了一下,回头查阅了相关的资料,对参数传递问题有了新的了解和掌握,但是有个问题感觉还是很模糊,就是Java中到底是否只存在值传递,因为在查阅资料时,经常看到有 ...

  9. 实验:ignite查询效率探究

    前面的文章讲到ignite支持扫描查询和sql查询,其sql查询是ignite产品的一个亮点,那么哪一种的查询更适合我们的产品使用呢,往下看: 先分别贴一下扫描查询和sql查询两种查询方式的代码,供参 ...

  10. C语言之for循环

    #include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ int i; for(i=1;i< ...