CF558E-A Simple Task-线段树+计数排序
计数排序的原理,只要知道了有几个数比i小,就可以知道i的位置
这道题只有26个字母,搞26颗线段树,然后区间更新
#include <cstdio>
#include <cstring>
#include <algorithm> //using namespace std;
const int maxn = 1e5+; int N,Q;
char line[maxn]; #define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define root 1,1,N struct SegrTree{
int num[maxn<<];
int lazy[maxn<<];
void init()
{
memset(lazy,-,sizeof lazy);
}
void push_up(int rt)
{
num[rt] = num[rt<<] + num[rt<<|];
}
void push_down(int rt,int m)
{
if(lazy[rt] != -)
{
num[rt<<] = (m-(m>>))*lazy[rt] ;
num[rt<<|] = (m>>)*lazy[rt] ;
lazy[rt<<] = lazy[rt<<|] = lazy[rt];
lazy[rt] = -;
}
}
void build(int kind,int rt,int l,int r)
{
if(l == r)
{
num[rt] = line[l-]==kind+'a';
//if(line[l-1]=='c') printf("%c pos:%d \n",kind+'a',l);
return ;
}
int mid = (l+r)>>;
build(kind,lson);
build(kind,rson);
push_up(rt);
}
void update(int L,int R,int d,int rt,int l,int r)
{
if(L <= l && R >= r)
{
num[rt] = (r-l+)*d;
lazy[rt] = d;
return ;
}
push_down(rt,r-l+);
int mid = (l+r)>>;
if(L <= mid) update(L,R,d,lson);
if(R > mid) update(L,R,d,rson);
push_up(rt);
}
int query(int L,int R,int rt,int l,int r)
{
if(L <= l && R >= r)
{
return num[rt];
}
push_down(rt,r-l+);
int mid = (l+r)>>;
int res = ; if(R <= mid) res = query(L,R,lson);
else if(L > mid) res = query(L,R,rson);
else res = query(L,R,lson)+query(L,R,rson); push_up(rt);
return res;
}
}alpha[]; void sort(int l,int r,int k)
{
int pos,cnt;
if(k == )
{
pos = l;
for(int i=;i<;i++)
{
cnt = alpha[i].query(l,r,root);
//printf("[%d,%d]%c k:%d pos:%d cnt:%d\n",l,r,i+'a',k,pos,cnt);
if(cnt)
{
alpha[i].update(l,r,,root);
alpha[i].update(pos,pos+cnt-,,root);
pos += cnt;
}
}
}
else
{
pos = l;
for(int i=;i>=;i--)
{
cnt = alpha[i].query(l,r,root);
//printf("[%d,%d] %c k:%d pos:%d cnt:%d\n",l,r,i+'a',k,pos,cnt);
if(cnt)
{
alpha[i].update(l,r,,root);
alpha[i].update(pos,pos+cnt-,,root);
pos += cnt;
}
}
}
} int main()
{
//freopen("input.txt","r",stdin); scanf("%d%d",&N,&Q);
scanf("%s",line); for(int i=;i<;i++)
{
alpha[i].init();
alpha[i].build(i,root);
//printf("tot num:%c %d\n",i+'a',alpha[i].query(1,N,root));
}
//printf("c:%d c:%d\n",alpha[2].query(1,N,root),alpha[2].query(7,10,root));
for(int i=,l,r,k;i<Q;i++)
{
scanf("%d%d%d",&l,&r,&k);
sort(l,r,k);
}
for(int i=;i<=N;i++)
{
for(int j=;j<;j++) if(alpha[j].query(i,i,root))
{
printf("%c",'a'+j);
}
}
puts("");
}
CF558E-A Simple Task-线段树+计数排序的更多相关文章
- 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 (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- CF558E A simple task 线段树
这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...
- [Codeforces558E]A Simple Task 线段树
链接 题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作. 每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序. 最后输出最终 ...
- 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 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 ...
- CF #312 E. A Simple Task 线段树
题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...
- codeforces 558E A Simple Task 线段树
题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...
随机推荐
- MySQL root密码忘记,原来还有更优雅的解法!
一直以来,对于MySQL root密码的忘记,以为只有一种解法-skip-grant-tables. 问了下群里的大咖,第一反应也是skip-grant-tables.通过搜索引擎简单搜索了下,无论是 ...
- babel-preset-env使用指南
文章概览 babel-preset-env是非常重要且常用的一个插件预设,掌握它的用法以及实现原理非常有必要. 本文主要内容包括:babel-preset-env是什么.入门实例.如何配置以支持特定版 ...
- 【fetch跨域请求】cors
当使用fetch 发起跨域请求时,CORS(跨域资源共享Cross-origin resource sharing) 请求fetch const body = {name:"Good boy ...
- Redux 入门教程(二):中间件与异步操作
上一篇文章,介绍了 Redux 的基本做法:用户发出 Action,Reducer 函数算出新的 State,View 重新渲染. 但是,一个关键问题没有解决:异步操作怎么办?Action 发出以后, ...
- 搜狐畅游一面(c++)
上来是个小姐姐,有点懵.. 1. 介绍 2. 项目 3. 实习 4. 用的协议 tcp和udp的协议 5. select 和epoll(忘了) 6. 数据库的隔离级别, 死锁, 怎么避免死锁 ...
- Volterra方程的不动点
- BAT (中国互联网公司三巨头)
BAT,B=百度.A=阿里巴巴.T=腾讯,是中国互联网公司百度公司(Baidu).阿里巴巴集团(Alibaba).腾讯公司(Tencent)三大互联网公司首字母的缩写.百度总部在北京.阿里巴巴总部在浙 ...
- java 8 jvm 内存配置
jdk8内存参数解析与修改(新的参数) - LikeTech - CSDN博客https://blog.csdn.net/lk7688535/article/details/51767333 Java ...
- js 深度复制deepClone
function isObject(obj) { return typeof obj === 'object' && obj != null; } const deepClone =( ...
- mysql 常用字段类型
tinyint[(m)] [unsigned] [zerofill] 1字节 极小整数,数据类型用于保存一些范围的整数数值范围: 有符号: -128 - 127. 无符号: - 255 特别的: My ...