题意:一个长度为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. SDUT1586 计算组合数(组合数)

    这个题数据量小,不容易超时. #include<stdio.h> long long fac(int n) { ; ; i <= n ; i++) { m = i*m; } retu ...

  2. hdu2013

    http://acm.hdu.edu.cn/showproblem.php?pid=2013 #include<iostream> #include<stdio.h> #inc ...

  3. 日期工具类TimeUnit

    import java.util.concurrent.TimeUnit; 2 3 public class TimeUnitDemo { 4 private TimeUnit timeUnit =T ...

  4. 自动装配【Spring autowire】

    public class AutoWiringDao { private String daoName; public void setDaoName(String daoName) { this.d ...

  5. 李洪强iOS开发之initWithFrame,initWithCoder和aweakFormNib

    1 initWithFrame 通过代码创建控件的话用这个方法设置  2 initWithCoder(先执行) 与从xib加载有关系的 在此方法里面设置原有子控件的值是不行的,因为还没有连好线  3 ...

  6. lintcode :数组剔除元素后的乘积

    题目: 数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 样例 给出 ...

  7. Using the Repository Pattern with ASP.NET MVC and Entity Framework

    原文:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and ...

  8. 261. Graph Valid Tree

    题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...

  9. EINTR错误

    慢系统调用(slow system call):此术语适用于那些可能永远阻塞的系统调用.永远阻塞的系统调用是指调用有可能永远无法返回,多数网络支持函数都属于这一类.如:若没有客户连接到服务器上,那么服 ...

  10. android4.4内核移植

    01 init/目录下Kconfig修改: 956行添加: config PANIC_TIMEOUT int "Default panic timeout" help Set de ...