codeforces 558 E A Simple Task
题目大意就是给一个字符串,然后多个操作。每次操作能够把每一段区间的字符进行升序或者降序排序,问终于的字符串是如何的。
做法的话就是用线段树维护区间和
一開始仅仅考虑字符串中字符'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]);
}
5 seconds
512 megabytes
standard input
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.
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 one line, the string S after applying the queries.
10 5
abacdabcda
7 10 0
5 8 1
1 4 0
3 6 0
7 10 1
cbcaaaabdd
10 1
agjucbvdfk
1 10 1
abcdfgjkuv
First sample test explanation:





codeforces 558 E A Simple Task的更多相关文章
- 【30.93%】【codeforces 558E】A Simple Task
time limit per test5 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task
E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...
- Codeforces 558E A Simple Task (计数排序&&线段树优化)
题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...
- 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 ...
- A Simple Task CodeForces - 11D
A Simple Task CodeForces - 11D 题意:输出一个无向图的简单环数量.简单环指无重复边的环.保证图无重边自环. ans[i][j]表示"包含i中的点,以i中第一个点 ...
- Codeforces C. A Simple Task(状态压缩dp)
题目描述: A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 558E A Simple Task(权值线段树)
题目链接 A Simple Task 题意 给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...
- 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 ...
随机推荐
- 深入分析Android动画(二)
上回书说到Android动画的分类以及基本使用,这会书主要说Android属性动画的原理,对于View动画的原理本篇不做深入分析.对于Android动画的基础请看深入分析Android动画(一) 我们 ...
- asp.net后台发送HTTP请求
一.文件流方式(转自:http://blog.csdn.net/u011511086/article/details/53216330) /// 发送请求 /// </summary> / ...
- Spring ioc与aop的理解
一 spring的特点 1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易 ...
- 在LINQ查询中LINQ之Group By的用法
LINQ定义了大约40个查询操作符,如select.from.in.where.group 以及order by,借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数据.Linq有很 ...
- c#控件攻略宝典之ListBox控件
ListBox控件的使用: 1)控件属性 Items SelectedItems SelectioModes 2)数据绑定 DataSoure DisplayMember ValueMenber 3) ...
- ADO.NET中SqlCommand对数据库操作
我们要不断地进行数据库的读写,那么ExecuteNonQuery(),ExecuteReader()与ExecuteScalar()就是我们在对数据库进行操作时要用到的,下面我来依次认识一下: ...
- 新建Vue项目
新建Vue项目 1.安装Vue命令行工具,webpack和git,使用淘宝镜像CNPM.参考http://www.imooc.com/video/12299 2.打开命令行,进入到要存放的目录,win ...
- SQL Server多表同时查询
今天在练sql server发现多条语句同时使用可以多表同时查询,具体操作如下: 代码示例: USE teachingGOSELECT *FROM dbo.teach_classORDER BY cl ...
- [转载] 基于Redis实现分布式消息队列
转载自http://www.linuxidc.com/Linux/2015-05/117661.htm 1.为什么需要消息队列?当系统中出现“生产“和“消费“的速度或稳定性等因素不一致的时候,就需要消 ...
- 设计模式的征途—21.迭代器(Iterator)模式
我们都用过电视机遥控器,通过它我们可以进行开机.关机.换台.改变音量等操作.我们可以将电视机看做一个存储电视频道的集合对象,通过遥控器可以对电视机中的频道集合进行操作,例如返回上一个频道.跳转到下一个 ...