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 ...
随机推荐
- wordpress plugins collection
1/ simple page ordering 4.8星 wordpress的plugins唯一的好处就是命名简单易懂,这款插件从名称就可以看出来,用来对page页面排序的.只需要在后台page中拖拽 ...
- Ref相关的名词解释
NV (NOT-VOLATILE),即非易失性,断电不会丢失的存储信息,包括生产信息.客户信息.产品信息等等. 它们都保存在不同(FLASH)分区,并根据不同的分区提供不同的接口.数据结构和管理机制. ...
- erl_0012 timer:tc 测试模块函数调用运行耗时
timer:tc 可以测试出函数调用耗时,是erlang性能测试的好工具. timer:tc(?MODULE, Fun, [Args]).
- 【转】IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
- 【转】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
iOS模拟器简介 iOS功能简介 iOS模拟器,是在Mac下面开发程序时,开发iOS平台的程序时候,可以使用的辅助工具. 其功能是,帮你模拟iOS平台设备,在模拟器上运行对应的程序,以方便你没有实体设 ...
- android webview 遇到的问题:external/chromium/net/disk_cache/stat_hub.cc:216:
今天也遇到这个问题,界面显示无法访问,Baidu吧,结果有些含糊其词,有的说加网络权限,我看了下我的, 有个 <uses-permission android:name="androi ...
- 嵌入式 GDB调试死锁示例
死锁:一种情形,此时执行程序中两个或多个线程发生永久堵塞(等待),每个线程都在等待被 其他线程占用并堵塞了的资源.例如,如果线程A锁住了记录1并等待记录2,而线程B锁住了记录2并等待记录1,这样两个线 ...
- vector 释放内存 swap
相 信大家看到swap这个词都一定不会感到陌生,甚至会有这样想法:这不就是简单的元素交换嘛.的确,swap交换函数是仅次于Hello word这样老得不能老的词,然而,泛型算法东风,这个小小的玩意儿却 ...
- c#中const、static、readonly的区别
1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...
- 基于XMPP协议的手机多方多端即时通讯方案
一.开发背景 1.国际背景 随着Internet技术的高速发展,即时通信已经成为一种广泛使用的通信方式.1996年Mirabilis公司推出了世界上第一个即时通信系统ICQ,不到10年间,即时通信(I ...