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> ...
随机推荐
- ajax请求基于restFul的WebApi(post、get、delete、put)
近日逛招聘软件,看到部分企业都要求会编写.请求restFul的webapi.正巧这段时间较为清闲,于是乎打开vs准备开撸. 1.何为restFul? restFul是符合rest架构风格的网络API接 ...
- 【评分】Beta 答辩总结
[评分]Beta 答辩总结 总结 按时交 - 有分 晚交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 倒扣本次作业分数 由于前期不够重视,到beta评分才发现有5组的代码提交仅由一人&qu ...
- iOS iCloud云存储数据
https://www.jianshu.com/p/ce8cfaf6030e 2017.11.29 16:05* 字数 452 阅读 302评论 0喜欢 1 因为上一次做了用keychain来持久化存 ...
- 归并排序Python 实现
一.归并排序 -归并排序(MERGE-SORT)是利用归并的思想实现的排序方法,该算法采用经典的分合策略(将问题分(divide)成一些小的问题然后递归求解,而合的阶段则将分的阶段得到的各答案&q ...
- Linux reboot与init 6区别
Reboot与init 6的区别 - flyingcloud_2008的专栏 - CSDN博客https://blog.csdn.net/flyingcloud_2008/article/detail ...
- Druid Monitor开启登录界面
<!-- druid --> <filter> <filter-name>druidWebStatFilter</filter-name> <fi ...
- PHP之常用设计模式
在日常开放中,经常会用到一些设计模式,进行我们代码的优化处理,一个很好的设计思想 1) 工厂模式 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象 ...
- spring boot中的约定优于配置
Spring Boot并不是一个全新的框架,而是将已有的Spring组件整合起来. Spring Boot可以说是遵循约定优于配置这个理念产生的.它的特点是简单.快速和便捷. 既然遵循约定优于配置,则 ...
- Linux启动/停止/重启Mysql数据库
1.查看mysql版本 1)status; 2)select version(); 2.Mysql启动 1)使用 service 启动: service mysqld start (5.0版本是mys ...
- 大白跟着“菜鸟”学node——同名事件
若存在两个同名事件,触发事件时,两个事件监听器的回调函数会被按次序先后调用. 实例来自菜鸟教程: var events=require('events'); var emitter=new event ...