题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串

思路:

该题数据规模很大 排序是关键想到计数排序,根据计数排序原理,由只有26个小写字母,需要统计区间字母的个数,还需要更新区间,想到用线段树优化,对于每个字母建一个线段树维护各字母在区间的个数。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = 1000000007;
struct tree{
int num,l,r,lazy;
}t[30][N<<2];
int qnum[N],n,q;
char ch[N];
void pushup(int i,int rt){
t[i][rt].num=t[i][rt<<1].num+t[i][rt<<1|1].num;
}
void pushdown(int i,int rt){
if(t[i][rt].lazy!=-1){
t[i][rt<<1].num=(t[i][rt<<1].r-t[i][rt<<1].l+1)*t[i][rt].lazy;
t[i][rt<<1].lazy=t[i][rt].lazy;
t[i][rt<<1|1].num=(t[i][rt<<1|1].r-t[i][rt<<1|1].l+1)*t[i][rt].lazy;
t[i][rt<<1|1].lazy=t[i][rt].lazy;
t[i][rt].lazy=-1;
}
}
void build(int i,int l,int r,int rt){
t[i][rt].l=l;
t[i][rt].r=r;
t[i][rt].lazy=-1;
int m=(l+r)>>1;
if(l==r){
t[i][rt].num=(ch[l-1]=='a'+i);
return;
}
build(i,lson);
build(i,rson);
pushup(i,rt);
}
void update(int i,int v,int l,int r,int rt){
if(t[i][rt].l>=l&&t[i][rt].r<=r){
t[i][rt].num=(t[i][rt].r-t[i][rt].l+1)*v;
t[i][rt].lazy=v;
return;
}
pushdown(i,rt);
int m=(t[i][rt].l+t[i][rt].r)>>1;
if(l<=m)update(i,v,l,r,rt<<1);
if(r>m)update(i,v,l,r,rt<<1|1);
pushup(i,rt);
}
int query(int i,int l,int r,int rt){
if(t[i][rt].l>=l&&t[i][rt].r<=r)
return t[i][rt].num;
pushdown(i,rt);
int total=0;
int mid=(t[i][rt].l+t[i][rt].r)>>1;
if(l<=mid)total+=query(i,l,r,rt<<1);
if(r>mid)total+=query(i,l,r,rt<<1|1);
pushup(i,rt);
return total;
}
void solve(){
int l,r,w;
for(int i=0;i<26;++i)
build(i,1,n,1);
while(q--){
scanf("%d%d%d",&l,&r,&w);
for(int i=0;i<26;++i){
qnum[i]=query(i,l,r,1);
update(i,0,l,r,1);
}
if(w==1){
int pos=l;
for(int i=0;i<26;++i)
{
if(qnum[i]>0){
update(i,1,pos,pos+qnum[i]-1,1);
pos+=qnum[i];
}
}
}
else{
int pos=l;
for(int i=25;i>=0;--i)
{
if(qnum[i]>0){
update(i,1,pos,pos+qnum[i]-1,1);
pos+=qnum[i];
}
}
}
}
for(int i=0;i<n;++i)
for(int j=0;j<26;++j)
{
if(query(j,i+1,i+1,1)){
ch[i]='a'+j;
break;
}
}
printf("%s\n",ch);
}
int main()
{
scanf("%d%d",&n,&q);
scanf("%s",ch);
solve();
return 0;
}

  

CodeForces 558E(计数排序+线段树优化)的更多相关文章

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

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

  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/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...

  4. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  5. Nowcoder Hash Function ( 拓扑排序 && 线段树优化建图 )

    题目链接 题意 : 给出一个哈希表.其避免冲突的方法是线性探测再散列.现在问你给出的哈希表是否合法.如果合法则输出所有元素插入的顺序.如果有多解则输出字典序最小的那一个.如果不合法则输出 -1 分析 ...

  6. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  7. CodeForces 786B Legacy(线段树优化建图+最短路)

    [题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...

  8. Codeforces 786B Legacy(线段树优化建图)

    题目链接  Legacy 首先对于输入的$n$,建立一棵线段树. 显然线段树有大概$2n$个结点,每个节点对应一段区间 我们把这$2n$个结点加入我们的无向图中,一起跑最短路. 具体连边方案: 我们把 ...

  9. Educational Codeforces Round 69 E - Culture Code (最短路计数+线段树优化建图)

    题意:有n个空心物品,每个物品有外部体积outi和内部体积ini,如果ini>outj,那么j就可以套在i里面.现在我们要选出n个物品的一个子集,这个子集内的k个物品全部套在一起,且剩下的物品都 ...

随机推荐

  1. Win7 64 安装Visual Studio 2010和SQL Server 2008 R2

    1. 在MSDN,我告诉你下载安装文件,VS 2010 不论32位还是64位都是同一个文件,cn_visual_studio_2010_ultimate_x86_dvd_532347.iso.SQL下 ...

  2. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  3. 《HTTP权威指南》笔记

    http://blog.csdn.net/sunorry?viewmode=contents有些笔记 MIME 类型是一种文本标记,表示一种主要的对象类型和一个特定的子类型,中间由一条斜杠来分隔:te ...

  4. iOS开发--_weak typeof(self) weakSelf = self

    _weak typeof(self) weakSelf = self;  (一)内存管理原则  1.默认strong,可选weak.strong下不管成员变量还是property,每次使用指针指向一个 ...

  5. Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  6. Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  7. ScannerTest-------double string

    import java.util.*; public class ScannerTest { public static void main(String[] args){ Scanner scann ...

  8. [iOS]如何给Label或者TextView赋HTML数据

    // // ViewController.m // text // // Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights ...

  9. [iOS]解决模拟器无法输入中文问题

    第一步:设置schem 菜单项 -> Product-> Scheme -> Edit Scheme ->  然后在弹出的界面里 选择OPtion 项, 设置 Applicat ...

  10. Android Include标签

    编程的世界有的时候很微妙,有的时候就好像是在解决一个哲学问题,Android开发的时候,所有的布局,颜色,等(其实这些都可以称之为资源,Android中的资源是指非代码部分,如图片.音频.视频.字符等 ...