题目链接:http://codeforces.com/problemset/problem/558/E

给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子。

对于字符串排序,计数排序是比一般的排序要快的,但是仍然不能解决本问题。

建立26个线段树,用于统计某个字符在某个区间的情况。

那么如果对【L,R】排序,则先统计所有字符在其中的情况,并且清空该区间,根据每个字符的数量,从a到z去填充应该在的小区间。

 #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#include <numeric>
#include <cassert> using namespace std; const int N=1e5+; char s[N]; struct SegmentTree {
#define M N*4
int val[M];
int flag[M];
void up(int rt) {
val[rt]=val[rt<<]+val[rt<<|];
}
void build(int l,int r,int rt) {
if (l==r) {
val[rt]=;
flag[rt]=-;
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
up(rt);
}
void down(int rt,int m=) {
if (flag[rt]!=-) {
flag[rt<<]=flag[rt];
flag[rt<<|]=flag[rt];
if (flag[rt]==){
val[rt<<]=m-(m>>);
val[rt<<|]=(m>>);
}
else {
val[rt<<]=;
val[rt<<|]=;
}
flag[rt]=-;
}
}
void update(int p,int c,int l,int r,int rt) {
if (l==r) {
val[rt]=c;
return;
}
down(rt,r-l+);
int m=(l+r)>>;
if (p<=m)
update(p,c,l,m,rt<<);
else
update(p,c,m+,r,rt<<|);
up(rt);
}
void update(int L,int R,bool c,int l,int r,int rt) {
if (L<=l&&r<=R) {
val[rt]=c?(r-l+):;
flag[rt]=c;
return;
}
down(rt,r-l+);
int m=(l+r)>>;
if (L<=m) update(L,R,c,l,m,rt<<);
if (R>m) update(L,R,c,m+,r,rt<<|);
up(rt);
}
int query(int p,int l,int r,int rt) {
if (l==r) {
return val[rt];
}
down(rt,r-l+);
int m=(l+r>>);
if (p<=m) return query(p,l,m,rt<<);
else return query(p,m+,r,rt<<|);
}
int query(int L,int R,int l,int r,int rt) {
if (L<=l&&r<=R) {
return val[rt];
}
down(rt,r-l+);
int m=(l+r)>>;
int ret=;
if (L<=m) ret+=query(L,R,l,m,rt<<);
if (R>m) ret+=query(L,R,m+,r,rt<<|);
return ret;
}
#undef M
}seg[]; int main() {
int n,q;
scanf("%d %d",&n,&q);
scanf("%s",s+);
for (int i=;i<;i++)
seg[i].build(,n,);
for (int i=;i<=n;i++) {
int p=s[i]-'a';
seg[p].update(i,,,n,);
//cout<<p<<" xxx"<<endl;
}
while (q--) {
int l,r,t;
scanf("%d %d %d",&l,&r,&t);
int cnt[];
for (int i=;i<;i++) {
cnt[i]=seg[i].query(l,r,,n,);
if (cnt[i]>){
seg[i].update(l,r,,,n,);
}
}
if (t==) {
for (int i=;i<;l+=cnt[i++]){
if (cnt[i]==) continue;
seg[i].update(l,l+cnt[i]-,,,n,);
}
}
else {
for (int i=;i>=;l+=cnt[i--]){
if (cnt[i]==) continue;
seg[i].update(l,l+cnt[i]-,,,n,);
}
}
}
for (int i=;i<=n;i++) {
for (int j=;j<;j++){
if (seg[j].query(i,,n,)) {
s[i]='a'+j;
break;
}
}
}
s[n+]=;
printf("%s\n",s+);
return ;
}

CF #312 E. 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 Round #312 (Div. 2) E. A Simple Task 线段树 延时标记

    E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...

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

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

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

  6. CF#312 558e A Simple Task

    ~~~题面~~~ 题解: 观察到字母只有26个,因此考虑对这26个字母分别维护,每个线段树维护一个字母,如果一个线段树的某个叶节点有值,表示当前叶节点所在位置的字母是现在这个线段树代表的字母. 那么对 ...

  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 线段树

    链接 题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作. 每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序. 最后输出最终 ...

随机推荐

  1. JS入门(四)

    接之前一篇的函数.写之前的函数的时候讲的比较笼统,在这重新写一下函数的内容. 函数: 之前提过,函数就是代码复用的一种机制或是将代码封装成功能的代码段.函数的声明在这边就不多提了,因为相对来说比较简单 ...

  2. 重回博客 谈一谈Node中的异步和单线程

    重回博客,这个帐号之前注册后就只发了一篇博客.听朋友建议,决定一周两次更新. 第一篇谈论一下最近想的比较多的异步的问题. 传统多线程异步 传统的异步是多线程的,当要同时做两件事的时候,他们是执行在不同 ...

  3. 2435: [Noi2011]道路修建

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2188  Solved: 639[Submit][Status ...

  4. android-自定义广告轮播Banner(无限循环实现)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...

  5. C#编写代码:求三个数中的最大数

    static void Main(string[] args)        {            float x, y, z, temp;            Console.Write(&q ...

  6. ThoughtWorks 一道面试题及解法

    前两天面试ThoughtWorks,有一道家庭作业题,题目如下: Problem Two: Conference Track Management You are planning a big pro ...

  7. 面试 -- ListView对其指定的子Item进行单独的刷新

    1,最近在面试题目的时候突然看到了问题"ListView对其指定的子Item进行单独的刷新",当时感觉一脸懵逼,还是第一次听到这个问题,也就是不能使用adapter.notifyD ...

  8. ubuntu如何进入local、bin目录

    回到home目录,输入命令:cd /usr/local 若要进入bin目录,输入命令:cd /usr/local/bin

  9. Java实现非递归删除目录

    最近在学C#的文件系统, 发现C#的文件系统貌似比java的东西少一点, 居然连删除目录都直接做好封装了, 想到学java的时候还要自己写递归删除, 好像没写过非递归的,就在网上查了下, 关于非递归删 ...

  10. java学习笔记----运算符

    一.算数运算符 特别说明: 加 ,减 ,乘 ,除 与数学运算一致 取余符号看被除数 自加(减)运算:++a,--a;先做自加(自减)运算在做其他运算 a++,a--;先做其他运算在做自加(自减)运算 ...