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
outputstandard output
This task is very simple. Given a string S of length n and q queries each query is on the format i j k which means sort the substring consisting of the characters from i to j in non-decreasing order if k = 1 or in non-increasing order if k = 0.
Output the final string after applying the queries.
Input
The first line will contain two integers n, q (1 ≤ n ≤ 105, 0 ≤ q ≤ 50 000), the length of the string and the number of queries respectively.
Next line contains a string S itself. It contains only lowercase English letters.
Next q lines will contain three integers each i, j, k (1 ≤ i ≤ j ≤ n, ).
Output
Output one line, the string S after applying the queries.
Sample test(s)
input
10 5
abacdabcda
7 10 0
5 8 1
1 4 0
3 6 0
7 10 1
output
cbcaaaabdd
input
10 1
agjucbvdfk
1 10 1
output
abcdfgjkuv
Note
First sample test explanation:
题意,给一段字符串。然后一系列改动i,j之间的字符串从小到大排序,或从大到小排序。维护26的线段树。i,j改动。仅仅须要查询一个a - z的个数,按计数排序的原理,更新一下线段树就能够了。总的复杂度为o(26 * q * log(n));速度有点慢,用伸展树应该快些。由于是成段更新,所以要用延时标记,记-1不更新,0全置0,1全置1,仅仅有这点小技巧就能够了。
#define N 100005
#define M 100005
#define maxn 205
#define SZ 26
#define MOD 1000000000000000007
#define lson (now<<1)
#define rson (now<<1|1)
int n,q,ss,ee,k,num[SZ];
char str[N];
struct node{
int c,sum,l,r;
};
node tree[SZ][N*4];
void pushDown(int tn,int now){
if(tree[tn][now].c != -1){
tree[tn][lson].c = tree[tn][rson].c = tree[tn][now].c;
tree[tn][lson].sum = tree[tn][lson].c*(tree[tn][lson].r-tree[tn][lson].l + 1);
tree[tn][rson].sum = tree[tn][rson].c*(tree[tn][rson].r-tree[tn][rson].l + 1);
tree[tn][now].c = -1;
}
}
void pushUp(int tn,int now){
tree[tn][now].sum = tree[tn][lson].sum + tree[tn][rson].sum ;
}
void buildTree(int l,int r,int now){
FI(SZ)
tree[i][now].c = -1,tree[i][now].l = l,tree[i][now].r = r,tree[i][now].sum = 0;
if(l >= r){
return ;
}
int mid = (l+r)>>1;
buildTree(l,mid,lson);
buildTree(mid+1,r,rson);
}
void updateTree(int l,int r,int now,int s,int e,int tn,int c){
if(s <= l && e>= r){
tree[tn][now].c = c;
tree[tn][now].sum = tree[tn][now].c*(tree[tn][now].r - tree[tn][now].l + 1);
return ;
}
pushDown(tn,now);
int mid = (l+r)>>1;
if(s <= mid) updateTree(l,mid,lson,s,e,tn,c);
if(e > mid) updateTree(mid+1,r,rson,s,e,tn,c);
pushUp(tn,now);
}
int queryTree(int l,int r,int now,int s,int e,int tn){
if(s <= l && e>= r){
return tree[tn][now].sum;
}
pushDown(tn,now);
int mid = (l+r)>>1;
int ans = 0;
if(s <= mid) ans += queryTree(l,mid,lson,s,e,tn);
if(e > mid) ans += queryTree(mid+1,r,rson,s,e,tn);
pushUp(tn,now);
return ans;
}
void outputStr(){
FI(n){
FJ(SZ){
if(queryTree(1,n,1,i+1,i+1,j)){
printf("%c",j+'a');
break;
}
}
}
printf("\n");
}
int main()
{
while(S2(n,q)!=EOF)
{
SS(str);
buildTree(1,n,1);
FI(n){
updateTree(1,n,1,i+1,i+1,str[i] - 'a',1);
}
FI(q){
S2(ss,ee);S(k);
FJ(SZ)
num[j] = queryTree(1,n,1,ss,ee,j);
FJ(SZ)
updateTree(1,n,1,ss,ee,j,0);
if(k){
int sum = 0;
FJ(SZ){
if(num[j])
updateTree(1,n,1,ss + sum,ss + sum + num[j] - 1,j,1);
sum += num[j];
}
}
else {
int sum = 0;
for(int j = SZ - 1;j>=0;j--){
if(num[j])
updateTree(1,n,1,ss + sum,ss + sum + num[j] - 1,j,1);
sum += num[j];
}
}
}
outputStr();
}
return 0;
}
Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记的更多相关文章
- 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
题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
随机推荐
- 监控web服务(http,本地 / 远程监控nginx)
监控 httpd 服务一: #!/bin/bash #描述: 秒级别监控 http 服务 while [ 1 -lt 2 ] do sleep 10 ai=`netstat -ntl | grep & ...
- 洛谷 P1209 [USACO1.3]修理牛棚 Barn Repair
P1209 [USACO1.3]修理牛棚 Barn Repair 题目描述 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. ...
- View 和 ViewGroup 的 hasFocusable
在 android 中.焦点的获取和事件几乎相同,有一个分发机制.一般来说View 树上上层节点的 ViewGroup 比底层节点的 View 有更高的优先级获取焦点.这体如今 ViewGroup 有 ...
- FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity
自Android3.2之后,TabActibvity被弃用(Deprecated).取而代之的是FragmentActivity.由于Fragment比Activiy更灵活.消耗的资源更小.全然可以满 ...
- 服务器性能监控tips
一.tops 第一行 当前时间/已运行时间/登录用户数/最近 5 10 15分钟平均负载(平均进程数 cat /proc/loadavg) 除了前3个数字表示平均进程数量外,后面的1个分数,分母表示系 ...
- 字典(dictionary)与映射(map)
1. 字典:key-value 键值对 反转字典:reverse_dict = dict(zip(D.values(), D.keys())) 前提要保证 D 的 value 不会出现重复,因为字典反 ...
- HtmlHelper的扩展分页方法
一.新建一个空MVC项目,命名为MVCAppPager 二.新建一个文件夹PageHelper,在文件夹下新建接口IPageList以及实现类PageList IPageList接口: public ...
- shrio 加密/编码
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- linux大于2T的磁盘使用GPT分区的方法分享
(parted)表示在parted中输入的命令,其他为自动打印的信息 1.首先类似fdisk一样,先选择要分区的硬盘,此处为/dev/sdb ey: parted /dev/sdb 2.选择了/dev ...
- java中replaceAll反斜杠\ or java中replaceAll 括号[
java中replaceAll反斜杠\ String s=new String("this is a \\"); s.replaceAll("\\",&qu ...