[Codeforces558E]A Simple Task 线段树
题意:给定一个长度不超过 \(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 线段树的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- CF #312 E. A Simple Task 线段树
题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...
- CF558E A simple task 线段树
这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...
- codeforces 558E A Simple Task 线段树
题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...
- Codeforces558E A Simple Task(线段树)
题目 Source http://codeforces.com/problemset/problem/558/E Description This task is very simple. Given ...
随机推荐
- 19.允许重复的unordered_map
#include <string> #include <iostream> //查询性能最高 //允许重复的,hash_map #include <unordered_m ...
- java9新特性-15-全新的HTTP 客户端API
1.官方Feature 110: HTTP 2 Client 2.使用说明 HTTP,用于传输网页的协议,早在1997年就被采用在目前的1.1版本中.直到2015年,HTTP2才成为标准. H ...
- dedecms4张关键表解析之2
4张核心表的具体情况: 1.第一张表:dede_arctype 栏目表 字段解析: topid:上一级的id(0表示为顶级,1表示为下一级....) typename: 栏目名称 typedir:栏 ...
- UI Framework-1: Aura Client API
Client API The Aura Client API is an API Aura uses to communicate with the client application using ...
- js中return 、return false 、return true、break、continue区别
在开发中不熟悉这三者区别的同学,一般都知道return可以中止,但会根据字面意思觉得return true 中止当前函数执行,但其后的函数还会继续执行.return false 中止当前函数执行,其后 ...
- NOIP2013 货车运输 倍增
问题描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能 ...
- Android组件Activity初探
1.Activity是什么 Activity是Android系统中的四大组件之一,在MVC模式中属于C控制层 M(Model 模型):Model是应用程序的主体对象. V(View 视图) ...
- Java实现断点续传。
http://www.cnblogs.com/liaojie970/p/5013790.html
- Java NIO和IO的主要差别
我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景.以及它们怎样影响您的代码设计. Java NIO和IO的主要差别 下表总结了Java N ...
- quick 3.3载入Spine问题
近期项目要升级到Quick 3.3, 而且使用了Spine作为动画编辑器, 在此把升级过程中.有关quick 3.3 载入Spine遇到的坑在此记录一下. 1.Spine版本号问题 首先Quick 3 ...