题意:一个长度为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. django --fields.E304 错误解决方案

    今天在同一个表里,有多个不同的用户集时出现. fields.E304: Field name <field name> clashes with accessor for <fiel ...

  2. MySQL数据导出导入【转】

    MySQL基础 关于MySQL数据导出导入的文章,目的有二: 1.备忘 2.供开发人员测试 工具 mysqlmysqldump 应用举例 导出 导出全库备份到本地的目录 mysqldump -u$US ...

  3. 超实用js代码段一

    1: 过滤首尾空格trim.2:过滤左边空格ltrim    3:过滤右边空格    一:用正则方法写成三个函数. <script type="text/javascript" ...

  4. lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字

    题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成.  样例 ...

  5. [topcoder] EllysNumberGuessing

    http://community.topcoder.com/stat?c=problem_statement&pm=12975 简单题 #include <cstdlib> #in ...

  6. 在Tomcat中配置数据源

    使用工具:TOMCAT 7.0.52.IntelliJ IDEA 13.0.2.JSF 2.0+.SqlServer.jtds-1.2.5.jar 搞了好久都没成功,开始使用注解引入DataSourc ...

  7. 实现微信好友列表的php代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Lists of network protocols

    https://en.wikipedia.org/wiki/Lists_of_network_protocols Protocol stack: List of network protocol st ...

  9. Apache httpd + tomcat 简单集群

    集群其实很简单,我们就来说一下httpd+tomcat集群都要注意哪些部分: 首先使用的东西有 apache-tomcat-8.0.32      下载地址: http://tomcat.apache ...

  10. hdu 4143 A Simple Problem (变形)

    题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n),  y = (a+b)/ ...