Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序
题目链接:
http://codeforces.com/problemset/problem/558/E
E. A Simple Task
time limit per test5 secondsmemory limit per test512 megabytes
#### 问题描述
> 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
题意
给你一个长度为n的字符串(只有小写字母),m个更新操作(l,r,type)要么对区间里面的字符串升序排,要么降序排,问m个更新之后最后的字符串是什么
题解
首先,只有26个字母,可以考虑计数排序,其次,当然是要用线段树来维护了,可以开26颗线段树,支持区间更新,区间查询。
对于更新区间先查询一下各个字母多少个,然后再按排完序的结果,26个字母(对应26颗线段树)全部干一发区间更新。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
int sumv[26][maxn<<2],setv[26][maxn<<2];
int n,m;
char str[maxn];
void maintain(int id,int o) {
sumv[id][o]=sumv[id][lson]+sumv[id][rson];
}
void pushdown(int id,int o,int l,int r) {
if(setv[id][o]<0) return;
sumv[id][lson]=(mid-l+1)*setv[id][o];
sumv[id][rson]=(r-mid)*setv[id][o];
setv[id][lson]=setv[id][rson]=setv[id][o];
setv[id][o]=-1;
}
void build(int id,int o,int l,int r) {
if(l==r) {
int idx=str[l]-'a';
if(id==idx) sumv[id][o]=1;
else sumv[id][o]=0;
} else {
build(id,lson,l,mid);
build(id,rson,mid+1,r);
maintain(id,o);
}
}
int ul,ur,uv;
void update(int id,int o,int l,int r) {
if(ul<=l&&r<=ur) {
sumv[id][o]=(r-l+1)*uv;
setv[id][o]=uv;
} else {
pushdown(id,o,l,r);
if(ul<=mid) update(id,lson,l,mid);
if(ur>mid) update(id,rson,mid+1,r);
maintain(id,o);
}
}
int ql,qr,qv;
void query(int id,int o,int l,int r) {
if(ql<=l&&r<=qr) {
qv+=sumv[id][o];
} else {
pushdown(id,o,l,r);
if(ql<=mid) query(id,lson,l,mid);
if(qr>mid) query(id,rson,mid+1,r);
maintain(id,o);
}
}
void init() {
clr(sumv,0);
clr(setv,-1);
}
int main() {
init();
scf("%d%d",&n,&m);
scf("%s",str+1);
rep(i,0,26) build(i,1,1,n);
rep(i,0,m) {
int l,r,type;
scf("%d%d%d",&l,&r,&type);
int lef=l;
if(type==1) {
rep(i,0,26) {
ql=l,qr=r,qv=0;
query(i,1,1,n);
ul=l,ur=r,uv=0;
update(i,1,1,n);
ul=lef,ur=lef+qv-1,uv=1;
if(ul<=ur) update(i,1,1,n);
lef+=qv;
}
} else {
for(int i=25; i>=0; i--) {
ql=l,qr=r,qv=0;
query(i,1,1,n);
ul=l,ur=r,uv=0;
update(i,1,1,n);
ul=lef,ur=lef+qv-1,uv=1;
if(ul<=ur){
// prf("(%d,%d),id:%d\n",ul,ur,i);
update(i,1,1,n);
}
lef+=qv;
}
}
}
for(int i=1;i<=n;i++){
rep(j,0,26){
ql=i,qr=i,qv=0;
query(j,1,1,n);
if(qv>0){
prf("%c",'a'+j);
break;
}
}
}
puts("");
return 0;
}
//end-----------------------------------------------------------------------
/*
10 1
abacdabcda
7 10 0
*/
Notes
以后需要建多颗线段树的时候最好把它们所有的操作都独立开,不要放在一起!因为放在一起反而有可能变麻烦。
Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序的更多相关文章
- 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 线段树 延时标记
E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task
题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次 ...
- 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. ...
- 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 ...
- 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 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- 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. 这题 ...
随机推荐
- vue-cli 3.0 豆瓣api接口使用element做分页
记录自己的学习 大佬绕道谢谢! 豆瓣即将上映接口:https://api.douban.com/v2/movie/coming_soon 本地跨域问题 看我之前的文章:https://www.cnbl ...
- Delphi判断某个类是否实现了某个接口
通过TObject.GetInterface可以获得对象的实例实现某个接口,前提条件是必须实例化对象后才能运行GetInterface 下面的方法可获取类是否实现了某个接口,并返回接口的偏移: fun ...
- django定义模型类-14
目录 1. 定义 字段类型 约束类型 django的模型类定义在应用下的 models.py 文件中. 模型类继承自 django.db.models 包下的 Model 类. 新创建应用 book ...
- python多进程详解和协程
1.由于python多线程适合于多IO操作,但不适合于cpu计算型工作,这时候可以通过多进程实现.python多进程简单实用 # 多进程,可以cpu保持一致,python多线程适合多io.对于高cpu ...
- Java HashMap 源代码分析
Java HashMap jdk 1.8 Java8相对于java7来说HashMap变化比较大,在hash冲突严重的时候java7会退化为链表,Java8会退化为TreeMap 我们先来看一下类图: ...
- Hadoop HA高可用集群搭建(2.7.2)
1.集群规划: 主机名 IP 安装的软件 执行的进程 drguo1 192.168.80.149 j ...
- 20155215 2016-2017-2 《Java程序设计》第3周学习总结
20155215 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 第四章主要讲了类的构建,数组对象,字符串对象的构建与操作等.要学会区分基本类型与类类 ...
- 20155313 实验三《Java面向对象程序设计》实验报告
一.实验内容 XP基础 XP核心实践 相关工具 二.实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器>课程 2.完成实验.撰写实验报 ...
- 20155327李百乾 Exp3 免杀原理与实践
20155327李百乾 Exp3 免杀原理与实践 实践guocheng 一.Msfvenom使用编码器 1.利用(virustota)[https://www.virustotal.com/]检测实验 ...
- iOS上架被拒理由及相关解决方案记录
注:苹果客服中国区电话:4006 701 855 最近公司上线一个电动车工具类项目,被拒无数次,今天上架了,记录一下 01 苹果拒绝理由(内购和后台定位) We noticed that your a ...