链接

题意:给定一个长度不超过 \(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. 一些标签库的学习和do的讨论

    JSTL 核心标签库http://www.cnblogs.com/lihuiyy/archive/2012/02/24/2366806.html .do的讨论http://bbs.csdn.net/t ...

  2. VS10的一个问题

    今天遇到一个问题,LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏.转一下网上的解决办法http://bbs.csdn.net/topics/390 ...

  3. MAC下搭建appium UI自动化环境

    参考资料: http://qa.blog.163.com/blog/static/190147002201510161119832/ http://blog.csdn.net/liuchunming0 ...

  4. 紫书 例题 10-15 UVa 1638(递推)

    从大到小安排杆子 分三种情况 (1)插到最左边,那么左边看到了杆子会多一个 (2)插到最右边,那么右边看到了杆子会多一个 (3)插到中间边,那么不影响左边和右边看到的杆子数 具体看代码 #includ ...

  5. ArcGIS api for javascript——地图配置-定制缩放动画

    描述 本例展示了当用户放大或缩小地图时如何定义地图的动画.zoomDuration和zoomRate是Dojo动画属性,他们确定了动画的duration和帧刷新的rate .这些属性单位是毫秒,zoo ...

  6. 基于CC2530的ZigBee转以太网网关的设计与实现

    *已刊登至:<无线电>8月刊 物联网技术的实现中,无线技术是必不可少的部分. 近年无线技术的发展,将ZigBee推入人们的视线中.那么ZigBee是如何的一种技术呢?带着疑问.我查询了它的 ...

  7. URAL 1614. National Project “Trams” (图论大YY)

    1614. National Project "Trams" Time limit: 0.5 second Memory limit: 64 MB President has de ...

  8. CentOS7 启动[root@localhost ~]# systemctl start docker Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for de

    1).在linux虚拟机上安装docker步骤:1.检查内核版本,必须是3.10及以上uname ‐r2.安装dockeryum install docker3.输入y确认安装4.启动docker[r ...

  9. 写了个关于tomcat项目部署脚本 shell

    #!/bin/bash tomcat_project_path=" /server/tomcat" tomcat_cp_path="/root/tools/apache- ...

  10. HDU 4349 Xiao Ming's Hope 组合数学

    题意:给你n,问在C(n,1),C(n,2)...C(n,n)中有多少个奇数. 比赛的时候打表看出规律,这里给一个数学上的说明. Lucas定理:A,B非负整数,p是质数,A,B化为p进制分别为a[n ...