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 ...
随机推荐
- 使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)
使用Kafka的同学都知道,我们每次创建Kafka主题(Topic)的时候可以指定分区数和副本数等信息,如果将这些属性配置到server.properties文件中,以后调用Java API生成的主题 ...
- HTML元素margin、padding的默认值
HTML元素margin.padding的默认值 element margin(单位像素) padding html 0 0 body 8 0 div 0 0 h1 21 0 h2 19 0 19 0 ...
- java基础 (记事本编写hello world,path,classpath,java的注释符)
一:java的基本信息 jre 是指java运行环境,jdk 是指 java 开发工具集(并且里面是自带有jre运行环境的) jvm是指java的虚拟机 java的源代码的后缀名是 .java (例如 ...
- HDFS 整体把握
对于HDFS这样一个分布式文件系统,它的目的是为了实现在多台廉价X86服务器上实现大文件存储. HDFS 是仿造GFS 设计出来的. 如图所示, 这种实现方案是一种采取有一个中心节点, 多个数 ...
- UVA 11383 Golden Tiger Claw 金虎爪(KM算法)
题意: 给一个n*n的矩阵,每个格子中有正整数w[i][j],试为每行和每列分别确定一个数字row[i]和col[i],使得任意格子w[i][j]<=row[i]+col[j]恒成立.先输row ...
- HDU 5353 Average 糖果分配(模拟,图)
题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路: 明显每个人最多只能多于平均值2个 ...
- WWDC 2015 - 概记
WWDC 2015已经过去快一个月了,今年似乎没有像去年那样变化巨大,一切都在慢慢演进,iOS.Mac OS.watchOS都变得越来越好. 新的三大平台的发布,iOS 9/Mac OS EL Cap ...
- Python [Leetcode 121]Best Time to Buy and Sell Stock
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. If you ...
- linux 开机自动启动脚本方法
通过现场对这次天津iptv demo项目的调测.对iptv这套系统有了更好的认识和理解.由于iptv本身需要安装许多服务.而现场实施中有没有把这些需要启动服务的脚本加入到开 机自动运行中.如果服务器重 ...
- 在Linux系统中如何装rpm,deb,tar.gz,tar.bz2,apt,bin 格式的文件
首先安装 系统自带的 alien 包 :终端 -su-输入密码 -进入ROOT 用户 - sudo apt-get install alien 这样 alien 包 就装上去了 !(if alien ...