题目链接

题意较为简单。

思路:

由于仅仅有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 线段树的更多相关文章

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

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

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

  3. 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task

    E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...

  4. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  5. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

  6. Codeforces 558E A Simple Task(计数排序+线段树优化)

    http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...

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

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

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

随机推荐

  1. 【BZOJ4383】[POI2015]pustynia

    题意: 建议Alt+F4百度一下 题解: 差分约束+线段树优化建图,直接按照拓扑序跑就行了 代码: #include<iostream> #include<cstring> # ...

  2. vue-cli3+typescript+路由懒加载报错问题

    vue-cli3的版本是3.4.1 出现的情况是网页显示正常,但是终端一直提示找不到模块: 如果去掉路由懒加载的方式,就没有报错: 原因是以前我们习惯直接写文件名而不加后缀, 现在使用ts时就需要写v ...

  3. C#调用带结构体指针的C Dll的方法

    在C#中调用C(C++)类的DLL的时候,有时候C的接口函数包含很多参数,而且有的时候这些参数有可能是个结构体,而且有可能是结构体指针,那么在C#到底该如何安全的调用这样的DLL接口函数呢?本文将详细 ...

  4. 对jvm进行gc的时间、数量、jvm停顿时间的监控

    在jdk中一个类可以获得gc的信息: public static void main(String[] args) { List<GarbageCollectorMXBean> garba ...

  5. UVA 11020 - Efficient Solutions(set)

    UVA 11020 - Efficient Solutions 题目链接 题意:每个人有两个属性值(x, y).对于每个人(x,y)而言,当有还有一个人(x', y'),假设他们的属性值满足x' &l ...

  6. OpenCASCADE直线与平面求交

    OpenCASCADE直线与平面求交 在<解析几何>相关的书中都给出了直线和平面的一般方程和参数方程.其中直线的一般方程有点向式形式的. 由于过空间一点可作且只能作一条直线平行于已知直线, ...

  7. poj 2154 Color(polya计数 + 欧拉函数优化)

    http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...

  8. Linux线程相互排斥量--进程共享属性

    多线程中.在相互排斥量和 读写锁的 属性中.都有一个叫 进程共享属性 . 对于相互排斥量,查询和设置这个属性的方法为: pthread_mutexattr_getpshared pthread_mut ...

  9. border:none与border:0的区别

    border:none与border:0的区别体现为两点:一是理论上的性能差异,二是浏览器兼容性的差异. 性能差异: [border:0;]把border设为“0”像素效果等于border-width ...

  10. 使用CSS3制作网站常用的小三角形

    现在在前端开发中,经常会看到一些小三角形,如一些导航的下拉菜单,还有一些聊天信息的气泡模式,很多时候我们都是通过切图片的方法来制作,今天零度给大家分享一个完全通过css3实现的小三角效果. 先上htm ...