链接

题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作。

每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序。

最后输出最终的字符串。

题解

发现只有小写英文字母,26个,直接建26棵线段树,排序的时候,直接统计每个字母的个数暴力安排即可

好像见过值域 \(10^9\) 的,不过不知道在哪了,先留个坑,等看到了或者记起来了再补

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
using namespace std;
typedef long long ll;
inline int read(){char c;int w;
while(!isdigit(c=getchar()));w=c&15;
while(isdigit(c=getchar()))w=w*10+(c&15);return w;
}
inline char smax(int&x,const int&y){return x<y?x=y,1:0;}
inline char smin(int&x,const int&y){return x>y?x=y,1:0;}
const int N=1e5+5;
struct node{
int ls,rs,w,ch;
}t[N*120];
int rt[26],cnt;
inline void add(int&o,int w,int len){if(!o)o=++cnt;t[o].ch=w,t[o].w=len*w;}
inline void pushdown(int&o,int len){
if(~t[o].ch){
add(t[o].ls,t[o].ch,len-(len>>1));
add(t[o].rs,t[o].ch,len>>1);
t[o].ch=-1;
}
}
inline void pushup(int o){t[o].w=t[t[o].ls].w+t[t[o].rs].w;}
inline void update(int&o,int l,int r,int x,int y){
pushdown(o,r-l+1);
if(x<=l&&r<=y)return add(o,1,r-l+1);
if(!o)t[o=++cnt].ch=-1;int mid=l+r>>1;
if(x<=mid)update(t[o].ls,l,mid,x,y);if(y>mid)update(t[o].rs,mid+1,r,x,y);
pushup(o);
}
inline void ins(int&o,int l,int r,int x){
if(!o)t[o=++cnt].ch=-1;++t[o].w;
if(l==r)return;int mid=l+r>>1;
x<=mid?ins(t[o].ls,l,mid,x):ins(t[o].rs,mid+1,r,x);
}
inline int query(int&o,int l,int r,int x,int y){
if(!o||x>r||y<l)return 0;pushdown(o,r-l+1);
if(x<=l&&r<=y){int ans=t[o].w;return t[o].w=t[o].ch=0,ans;}
int mid=l+r>>1,ans=query(t[o].ls,l,mid,x,y)+query(t[o].rs,mid+1,r,x,y);
return pushup(o),ans;
}
char s[N];
int c[26];
int main(){
int n=read(),q=read();
scanf("%s",s+1);
REP(i,1,n)ins(rt[s[i]-'a'],1,n,i);
while(q--){
int l=read(),r=read(),op=read();
REP(i,0,25)c[i]=query(rt[i],1,n,l,r);
if(op){
int p=l;
REP(i,0,25)if(c[i])update(rt[i],1,n,p,p+c[i]-1),p+=c[i];
}
else{
int p=r;
REP(i,0,25)if(c[i])update(rt[i],1,n,p-c[i]+1,p),p-=c[i];
}
}
REP(i,1,n)REP(j,0,25)if(query(rt[j],1,n,i,i)){putchar(j+'a');break;} return 0;
}

[Codeforces558E]A Simple Task 线段树的更多相关文章

  1. 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 ...

  2. 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 ...

  3. CodeForces 588E A Simple Task(线段树)

    This task is very simple. Given a string S of length n and q queries each query is on the format i j ...

  4. 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 ...

  5. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  6. CF #312 E. A Simple Task 线段树

    题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...

  7. CF558E A simple task 线段树

    这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...

  8. codeforces 558E A Simple Task 线段树

    题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...

  9. Codeforces558E A Simple Task(线段树)

    题目 Source http://codeforces.com/problemset/problem/558/E Description This task is very simple. Given ...

随机推荐

  1. 版本控制器:SVN(精讲)

    版本控制器:SVN 1 开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目 ...

  2. C#篇(二)——属性的实质

    属性的内部实现其实就是方法 我们平时写的代码: class Student { private int age; public int Age { get { return age; } set { ...

  3. VS 代码打包工具

    源代码下载地址 https://github.com/loresoft/msbuildtasks

  4. Swift学习笔记(2):错误处理

    目录: Error do-catch 断言 Error 在 Swift 中,错误用符合 Error 协议的类型的值来表示.这个空协议表明该类型可以用于错误处理异常. Swift 的枚举类型尤为适合构建 ...

  5. Saying Good-bye to Cambridge Again

    Saying Good-bye to Cambridge Again Very quietly I take my leave,      As quietly as I came here;     ...

  6. CDH5.14操作指南

    1.Cdh 5.14介绍 2.版本发布概要 3.安装要求 4.Cloudera Manager介绍 5.Cloudera 安装 6.Cloudera 管理 7.Cloudera Navigator C ...

  7. Remmina:一个 Linux 下功能丰富的远程桌面共享工具(转载)

    Remmina:一个 Linux 下功能丰富的远程桌面共享工具 作者: Aaron Kili 译者: LCTT geekpi | 2017-05-10 09:05   评论: 2 收藏: 4 Remm ...

  8. 洛谷 P4180 【模板】严格次小生成树[BJWC2010] LCT

    首次采用了压行,感觉还不错. Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #i ...

  9. windows下用winscp的root连接ubuntu“拒绝访问”的解决方法

    转载:https://www.cnblogs.com/weizhxa/p/10098640.html 解决: 1.修改ssh配置文件:sudo vim etc/ssh/sshd_config 在#Pe ...

  10. Oracle11g密码忘记处理方法

    c:\>sqlplus /nolog sql>connect / as sysdba sql>alter user 用户名 identified by 密码;(注意在这里输入的密码是 ...