题目链接:http://codeforces.com/contest/558/problem/E

题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序。

题解:建立26棵线段树,类似计数排序思想。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + ;
  4. struct SegTree {
  5. int lazy[], sum[], l, r;
  6. }T[N << ];
  7. int a[][N];
  8.  
  9. void pushup(int p, int c) {
  10. T[p].sum[c] = T[p << ].sum[c] + T[(p << )|].sum[c];
  11. }
  12.  
  13. void pushdown(int p, int c) {
  14. if(T[p].lazy[c] != -) {
  15. T[p << ].sum[c] = T[p].lazy[c]*(T[p << ].r - T[p << ].l + );
  16. T[(p << )|].sum[c] = T[p].lazy[c]*(T[(p << )|].r - T[(p << )|].l + );
  17. T[p << ].lazy[c] = T[(p << )|].lazy[c] = T[p].lazy[c];
  18. T[p].lazy[c] = -;
  19. }
  20. }
  21.  
  22. void build(int p, int c, int l, int r) {
  23. int mid = (l + r) >> ;
  24. T[p].l = l, T[p].r = r, T[p].lazy[c] = -;
  25. if(l == r) {
  26. T[p].sum[c] = a[c][l];
  27. return ;
  28. }
  29. build(p << , c, l, mid);
  30. build((p << )|, c, mid + , r);
  31. pushup(p, c);
  32. }
  33.  
  34. int query(int p, int c, int l, int r) {
  35. int mid = (T[p].l + T[p].r) >> ;
  36. if(T[p].l == l && T[p].r == r) {
  37. return T[p].sum[c];
  38. }
  39. pushdown(p, c);
  40. if(r <= mid) {
  41. return query(p << , c, l, r);
  42. } else if(l > mid) {
  43. return query((p << )|, c, l, r);
  44. } else {
  45. return query(p << , c, l, mid) + query((p << )|, c, mid + , r);
  46. }
  47. pushup(p, c);
  48. }
  49.  
  50. void update(int p, int c, int l, int r, int val) {
  51. int mid = (T[p].l + T[p].r) >> ;
  52. if(T[p].l == l && T[p].r == r) {
  53. T[p].lazy[c] = val;
  54. T[p].sum[c] = val * (r - l + );
  55. return ;
  56. }
  57. pushdown(p, c);
  58. if(r <= mid) {
  59. update(p << , c, l, r, val);
  60. } else if(l > mid) {
  61. update((p << )|, c, l, r, val);
  62. } else {
  63. update(p << , c, l, mid, val), update((p << )|, c, mid + , r, val);
  64. }
  65. pushup(p, c);
  66. }
  67. char str[N];
  68.  
  69. int main()
  70. {
  71. int n, m;
  72. scanf("%d %d", &n, &m);
  73. scanf("%s", str);
  74. for(int i = ; i < n; ++i) {
  75. a[str[i] - 'a'][i + ] = ;
  76. }
  77. for(int i = ; i < ; ++i) {
  78. build(, i, , n);
  79. }
  80. while(m--) {
  81. int l, r, c;
  82. scanf("%d %d %d", &l, &r, &c);
  83. if(c) {
  84. int x = l, y = r;
  85. for(int i = ; i < ; ++i) {
  86. if(x > y)
  87. break;
  88. int num = query(, i, l, r);
  89. if(!num)
  90. continue;
  91. update(, i, l, r, );
  92. update(, i, x, x + num - , );
  93. x = x + num;
  94. }
  95. } else {
  96. int x = l, y = r;
  97. for(int i = ; i >= ; --i) {
  98. if(x > y)
  99. break;
  100. int num = query(, i, l, r);
  101. if(!num)
  102. continue;
  103. update(, i, l, r, );
  104. update(, i, x, x + num - , );
  105. x = x + num;
  106. }
  107. }
  108. }
  109. for(int i = ; i <= n; ++i) {
  110. for(int j = ; j < ; ++j) {
  111. if(query(, j, i, i)) {
  112. putchar(char(j + 'a'));
  113. break;
  114. }
  115. }
  116. }
  117. putchar('\n');
  118. return ;
  119. }

Codeforces 588E. A Simple Task (线段树+计数排序思想)的更多相关文章

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

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

  3. codeforces 558E A Simple Task 线段树

    题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...

  4. [Codeforces558E]A Simple Task 线段树

    链接 题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作. 每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序. 最后输出最终 ...

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

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

  7. CF #312 E. A Simple Task 线段树

    题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...

  8. CF558E A simple task 线段树

    这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...

  9. CF558E-A Simple Task-线段树+计数排序

    计数排序的原理,只要知道了有几个数比i小,就可以知道i的位置 这道题只有26个字母,搞26颗线段树,然后区间更新 #include <cstdio> #include <cstrin ...

随机推荐

  1. hdu4638 group 树状数组

    连接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:就给给你n个数(大小在1-n里),然后给你连续的可以构成一个块,再给你N个询问,每个询问一个l ...

  2. ADO与ADO.NET的区别与介绍

    1. ADO与ADO.NET简介ADO与ADO.NET既有相似也有区别,他们都能够编写对数据库服务器中的数据进行访问和操作的应用程序,并且易于使用.高速度.低内存支出和占用磁盘空间较少,支持用于建立基 ...

  3. [转] POJ计算几何

    转自:http://blog.csdn.net/tyger/article/details/4480029 计算几何题的特点与做题要领:1.大部分不会很难,少部分题目思路很巧妙2.做计算几何题目,模板 ...

  4. 如何寻找google公司主导的开源项目

    在googlecode页面的搜索框中:搜索 label:google ,结果列表中就会显示所有开源软件列表.或者直接点击这个连接:http://code.google.com/hosting/sear ...

  5. 在python中如何设置当前工作目录

    import osos.chdir('要设置的当前目录') >>> import os >>> os.getcwd() 'D:\\Python27' >> ...

  6. 配置apache以fastcgi运行php

    apache默认是用自带的mod_php模块运行php,现在我们介绍使用fastcgi来执行php脚本.先说下fastcgi的优点: Fastcgi的优点: 从稳定性上看, fastcgi是以独立的进 ...

  7. 三种map的循环

    for(Map.Entry<String, List> entry : map.entrySet()) { System.out.println(entry.getKey()); List ...

  8. [Everyday Mathematics]20150228

    试证: $$\bex \int_0^\infty \sin\sex{x^3+\frac{\pi}{4}}\rd x =\frac{\sqrt{6}+\sqrt{2}}{4}\int_0^\infty ...

  9. Red and Black ---路线问题

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  10. CAT XQX --- 省市三级级联实现说明

    最终效果: 满足要求, 上代码 : 1.   需要调用这个控件 的地方:添加引用,因为里面写着逻辑呢..... <script type="text/javascript" ...