Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E
题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序。
题解:建立26棵线段树,类似计数排序思想。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + ;
struct SegTree {
int lazy[], sum[], l, r;
}T[N << ];
int a[][N]; void pushup(int p, int c) {
T[p].sum[c] = T[p << ].sum[c] + T[(p << )|].sum[c];
} void pushdown(int p, int c) {
if(T[p].lazy[c] != -) {
T[p << ].sum[c] = T[p].lazy[c]*(T[p << ].r - T[p << ].l + );
T[(p << )|].sum[c] = T[p].lazy[c]*(T[(p << )|].r - T[(p << )|].l + );
T[p << ].lazy[c] = T[(p << )|].lazy[c] = T[p].lazy[c];
T[p].lazy[c] = -;
}
} void build(int p, int c, int l, int r) {
int mid = (l + r) >> ;
T[p].l = l, T[p].r = r, T[p].lazy[c] = -;
if(l == r) {
T[p].sum[c] = a[c][l];
return ;
}
build(p << , c, l, mid);
build((p << )|, c, mid + , r);
pushup(p, c);
} int query(int p, int c, int l, int r) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
return T[p].sum[c];
}
pushdown(p, c);
if(r <= mid) {
return query(p << , c, l, r);
} else if(l > mid) {
return query((p << )|, c, l, r);
} else {
return query(p << , c, l, mid) + query((p << )|, c, mid + , r);
}
pushup(p, c);
} void update(int p, int c, int l, int r, int val) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
T[p].lazy[c] = val;
T[p].sum[c] = val * (r - l + );
return ;
}
pushdown(p, c);
if(r <= mid) {
update(p << , c, l, r, val);
} else if(l > mid) {
update((p << )|, c, l, r, val);
} else {
update(p << , c, l, mid, val), update((p << )|, c, mid + , r, val);
}
pushup(p, c);
}
char str[N]; int main()
{
int n, m;
scanf("%d %d", &n, &m);
scanf("%s", str);
for(int i = ; i < n; ++i) {
a[str[i] - 'a'][i + ] = ;
}
for(int i = ; i < ; ++i) {
build(, i, , n);
}
while(m--) {
int l, r, c;
scanf("%d %d %d", &l, &r, &c);
if(c) {
int x = l, y = r;
for(int i = ; i < ; ++i) {
if(x > y)
break;
int num = query(, i, l, r);
if(!num)
continue;
update(, i, l, r, );
update(, i, x, x + num - , );
x = x + num;
}
} else {
int x = l, y = r;
for(int i = ; i >= ; --i) {
if(x > y)
break;
int num = query(, i, l, r);
if(!num)
continue;
update(, i, l, r, );
update(, i, x, x + num - , );
x = x + num;
}
}
}
for(int i = ; i <= n; ++i) {
for(int j = ; j < ; ++j) {
if(query(, j, i, i)) {
putchar(char(j + 'a'));
break;
}
}
}
putchar('\n');
return ;
}
Codeforces 588E. 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(线段树)
This task is very simple. Given a string S of length n and q queries each query is on the format i j ...
- codeforces 558E A Simple Task 线段树
题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...
- [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 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 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...
- CF558E A simple task 线段树
这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...
- CF558E-A Simple Task-线段树+计数排序
计数排序的原理,只要知道了有几个数比i小,就可以知道i的位置 这道题只有26个字母,搞26颗线段树,然后区间更新 #include <cstdio> #include <cstrin ...
随机推荐
- 51nod1199 Money out of Thin Air
链剖即可.其实就是利用了链剖后子树都在一段连续的区间内所以可以做到O(logn)查询和修改. 线段树细节打错了..要专心!肉眼差错都能找出一堆出来显然是不行的!. #include<cstdio ...
- HDU 4006 The kth great number【优先队列】
题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数 用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了 另外这个维护队列只有k个元素的时候需要注意一下 ...
- PHP学习笔记05——面向对象
<?php //1. 类的声明(包括构造方法.析构方法) class PersonA { var $name; //成员属性,用var声明 public $age; //当有其他修饰的时候,就不 ...
- Android init.rc文件格式解析
/***************************************************************************** * Android init.rc文件格式 ...
- PDO防注入原理分析以及使用PDO的注意事项 (转)
我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下两个问题: 为什么要使用PDO而不是mysql_connect? 为何PDO能防注入? 使用PDO防注入的时候应该特 ...
- 【自动化测试】Selenium 2.0 学习笔记
定位下拉框元素,要记得先把鼠标挪过去再进行定位. 定位元素用的是find_element_by.... python的模块化还要去思考下 unittest框架使用的时候,py文件命名不要用unitte ...
- 【解题报告】PKU 2826 An Easy Problem?!
原题链接:http://poj.org/problem?id=2826 一题很蛋疼的一题.目前为止还有一个问题我没搞清楚,问题注在代码中. 题目大意: 外面下雨了,农民Johnoson的bull(?? ...
- mysql:mysql_query(): Unable to save result set
解决方式 方式1: 原因:数据表索引损坏 方案:使用repair table 表名; 方式2: 原因:打开表的数据量太大,内存不够. 方案:配置my.ini 调整内存能解决
- <Liunx常用命令一>之TOP
一:作用 ----->TOP命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况. ----->TOP是一个动态显示过程,即可以通过用 ...
- ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
use mysql mysql> select host, user from user; 将相应用户数据表中的host字段改成'%': update user set host='%' whe ...