codeforces 558E A Simple Task 线段树
题意较为简单。
思路:
由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy。
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if (x>9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<ll, ll> pii;
const int N = 1e5 + 100;
#define lson (id<<1)
#define rson (id<<1|1)
#define L(x) tree[x].l
#define R(x) tree[x].r
#define Hav(x) tree[x].hav
#define Siz(x) tree[x].siz
#define Lazy(x) tree[x].lazy
struct Tree {
struct Node {
int l, r, siz;//siz表示区间长度
int hav;//hav表示这个区间的和
int lazy;//lazy为2表示清空区间 lazy为1表示把区间都变为1
}tree[N << 2];
void build(int l, int r, int id) {
L(id) = l; R(id) = r; Siz(id) = r - l + 1;
Hav(id) = Lazy(id) = 0;
if (l == r)return;
int mid = (l + r) >> 1;
build(l, mid, lson); build(mid + 1, r, rson);
}
void Down(int id) {
if (Lazy(id) == 1) {
Lazy(id) = 0;
Hav(lson) = Siz(lson); Hav(rson) = Siz(rson);
Lazy(lson) = Lazy(rson) = 1;
}
else if (Lazy(id) == 2) {
Lazy(id) = 0;
Hav(lson) = Hav(rson) = 0;
Lazy(lson) = Lazy(rson) = 2;
}
}
void Up(int id) {
Hav(id) = Hav(lson) + Hav(rson);
}
void updata(int l, int r, int val, int id) {
if (l == L(id) && R(id) == r) {
if (val == 1)
Hav(id) = Siz(id);
else Hav(id) = 0;
Lazy(id) = val;
return;
}
Down(id);
int mid = (L(id) + R(id)) >> 1;
if (r <= mid)updata(l, r, val, lson);
else if (mid < l)updata(l, r, val, rson);
else {
updata(l, mid, val, lson);
updata(mid + 1, r, val, rson);
}
Up(id);
}
int query(int l, int r, int id) {
if (l == L(id) && R(id) == r) {
return Hav(id);
}
Down(id);
int mid = (L(id) + R(id)) >> 1, ans = 0;
if (r <= mid)ans = query(l, r, lson);
else if (mid < l)ans = query(l, r, rson);
else {
ans = query(l, mid, lson) + query(mid + 1, r, rson);
}
Up(id);
return ans;
}
};
Tree alph[26];
int n, q;
char s[N];
int sum[26];
int main() {
rd(n); rd(q);
for (int i = 0; i < 26; i++)alph[i].build(1, n, 1);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) {
alph[s[i] - 'a'].updata(i, i, 1, 1);
}
while (q--) {
int l, r, in;
rd(l); rd(r); rd(in);
memset(sum, 0, sizeof sum);
for (int i = 0; i < 26; i++)
{
sum[i] += alph[i].query(l, r, 1);
alph[i].updata(l, r, 2, 1);
}
int tim = 26, i;
if (in)i = 0; else i = 25, in = -1;
for (;tim--; i += in) {
if (sum[i] == 0)continue;
alph[i].updata(l, l + sum[i] - 1, 1, 1);
l += sum[i];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 26; j++)
if (alph[j].query(i, i, 1))
{
putchar(j + 'a'); break;
}
}
return 0;
}
codeforces 558E A Simple Task 线段树的更多相关文章
- 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 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task
E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...
- Codeforces 558E A Simple Task (计数排序&&线段树优化)
题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...
- Codeforces 558E A Simple Task(权值线段树)
题目链接 A Simple Task 题意 给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...
- Codeforces 558E A Simple Task(计数排序+线段树优化)
http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...
- 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 Round #312 (Div. 2) E. A Simple Task 线段树 延时标记
E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...
随机推荐
- DIV+CSS布局中自适应高度的解决方法
div乱跑问题 (文件<DIV+CSS布局中自适应高度的解决方法.rar>) float 是个很危险的东西 得小心使用 本来有一很好的关于CSS+DIV的论坛 不过现在关门了 甚是可 ...
- WPF 获取应用的所有窗口
原文:WPF 获取应用的所有窗口 本文告诉大家如何获取应用内的所有窗口,无论这些窗口有没显示 在 WPF 可以通过 Application.Current.Windows 列举应用的所有窗口 fore ...
- 制作PC端的安装程序
一个多月不写博客了,不造大家有没有想我,(别自恋了,寥寥无几的粉丝,谁会想你),呜呜~~~ 好了,废话少叙,借用郭德纲老板的话,天儿不早了,干点正事儿吧! 一.序 Unity开发者都知道,打包出来的e ...
- C++容器(五):set类型
set类型 map容器是键-值对的集合,好比以任命为键的地址和电话号码.而set容器只是单纯的键的集合.当只想知道一个值是否存在时,使用set容器是最适合. 使用set容器必须包含set头文件: #i ...
- 怎样实如今Windows下编写的代码,直接在Linux下编译
方法一: 怎样实如今Windows7下编写Linux程序.写完程序以后.不用复制文件,直接在Linux(RHEL6.5)机器上编译最新的代码. 1.首先将Windows的代码目录设置为共享目录: 2. ...
- 常见的DNS攻击——偷(劫持)、骗(缓存投毒)、打(DDos)
常见的DNS攻击包括: 1) 域名劫持 通过采用黑客手段控制了域名管理密码和域名管理邮箱,然后将该域名的NS纪录指向到黑客可以控制的DNS服务器,然后通过在该DNS服务器上添加相应域名纪录,从而使网民 ...
- hdoj--1556--Color the ball(模拟&&树状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- C语言基础-第六章
数组和字符串 1.一维数组 数组当中最简单的数据 声明: 类型说明符 数组名[常量表达式] int a[3];说明a的长度为3,那么给a赋值的语句是:a={1,2,3}; 2.多维数组 2.1 二维数 ...
- Struts2的struts.xml的标准配置文档
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- PostgreSQL Replication之第四章 设置异步复制(1)
执行完您的第一个即时恢复(PITR,Point-In-Time-Recovery),我们准备在一个真正的复制设置上工作.在本章,您将学会如何设置异步复制和流.我们的目标是确保您可以实现更高的高可用和更 ...