E. A Simple Task

题目连接:

http://www.codeforces.com/contest/558/problem/E

Description

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 Input

10 5

abacdabcda

7 10 0

5 8 1

1 4 0

3 6 0

7 10 1

Sample Output

cbcaaaabdd

Hint

题意

给你n个字符,一共俩操作,l到r区间升序排序,l到r降序排序

字符集26.

让你输出最后的字符样子。

题解:

考虑计数排序,我们用线段树维护计数排序就好了,这样复杂度是26*q*logn

感觉自己智商还是涨了一波……

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 5e5+7;
  4. struct Seg{
  5. typedef int SgTreeDataType;
  6. struct treenode
  7. {
  8. int L , R ;
  9. SgTreeDataType sum , lazy;
  10. void update(SgTreeDataType v)
  11. {
  12. sum = (R-L+1)*v;
  13. lazy = v;
  14. }
  15. };
  16. treenode tree[maxn];
  17. inline void push_down(int o)
  18. {
  19. SgTreeDataType lazyval = tree[o].lazy;
  20. if(lazyval==-1)return;
  21. tree[2*o].update(lazyval) ; tree[2*o+1].update(lazyval);
  22. tree[o].lazy = -1;
  23. }
  24. inline void push_up(int o)
  25. {
  26. tree[o].sum = tree[2*o].sum + tree[2*o+1].sum;
  27. }
  28. inline void build_tree(int L , int R , int o)
  29. {
  30. tree[o].L = L , tree[o].R = R,tree[o].sum = 0,tree[o].lazy = -1;
  31. if (R > L)
  32. {
  33. int mid = (L+R) >> 1;
  34. build_tree(L,mid,o*2);
  35. build_tree(mid+1,R,o*2+1);
  36. }
  37. }
  38. inline void update(int QL,int QR,SgTreeDataType v,int o)
  39. {
  40. int L = tree[o].L , R = tree[o].R;
  41. if (QL <= L && R <= QR) tree[o].update(v);
  42. else
  43. {
  44. push_down(o);
  45. int mid = (L+R)>>1;
  46. if (QL <= mid) update(QL,QR,v,o*2);
  47. if (QR > mid) update(QL,QR,v,o*2+1);
  48. push_up(o);
  49. }
  50. }
  51. inline SgTreeDataType query(int QL,int QR,int o)
  52. {
  53. int L = tree[o].L , R = tree[o].R;
  54. if (QL <= L && R <= QR) return tree[o].sum;
  55. else
  56. {
  57. push_down(o);
  58. int mid = (L+R)>>1;
  59. SgTreeDataType res = 0;
  60. if (QL <= mid) res += query(QL,QR,2*o);
  61. if (QR > mid) res += query(QL,QR,2*o+1);
  62. push_up(o);
  63. return res;
  64. }
  65. }
  66. }T[26];
  67. char s[maxn];
  68. int cnt[26];
  69. int main()
  70. {
  71. int n,q;
  72. scanf("%d%d",&n,&q);
  73. scanf("%s",s+1);
  74. for(int i=0;i<26;i++)
  75. T[i].build_tree(1,n,1);
  76. for(int i=1;i<=n;i++)
  77. T[s[i]-'a'].update(i,i,1,1);
  78. for(int i=1;i<=q;i++)
  79. {
  80. int op,a,b;
  81. scanf("%d%d%d",&a,&b,&op);
  82. if(op==1)
  83. {
  84. for(int i=0;i<26;i++)
  85. cnt[i]=T[i].query(a,b,1);
  86. for(int i=0;i<26;i++)
  87. T[i].update(a,b,0,1);
  88. int l=a;
  89. for(int i=0;i<26;i++)
  90. T[i].update(l,l+cnt[i]-1,1,1),l+=cnt[i];
  91. }
  92. else
  93. {
  94. for(int i=25;i>=0;i--)
  95. cnt[i]=T[i].query(a,b,1);
  96. for(int i=25;i>=0;i--)
  97. T[i].update(a,b,0,1);
  98. int l=a;
  99. for(int i=25;i>=0;i--)
  100. T[i].update(l,l+cnt[i]-1,1,1),l+=cnt[i];
  101. }
  102. }
  103. for(int i=1;i<=n;i++)
  104. for(int j=0;j<26;j++)
  105. if(T[j].query(i,i,1))
  106. printf("%c",j+'a');
  107. return 0;
  108. }

Codeforces Round #312 (Div. 2) E. 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 Round #312 (Div. 2) E. A Simple Task 线段树 延时标记

    E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...

  3. Codeforces Round #312 (Div. 2) E. A Simple Task

    题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次 ...

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

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

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

  7. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

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

  9. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

随机推荐

  1. Codeforces Round #540 Tanya and Candies 预处理

    http://codeforces.com/contest/1118/problem/B 题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法. 思路:预处理奇数和 ...

  2. 流媒体技术学习笔记之(十二)Linux(Ubuntu)环境运行EasyDarwin

    Debug问题??? ./easydarwin -c ./easydarwin.xml & //这样的话是80端口 ./easydarwin -c ./easydarwin.xml -d // ...

  3. javascript命名空间

    命名空间:命名空间有助于减少程序中所需要的全局变量的数量,并且同时还有助于避免命名冲突或过长的名字前缀. 板栗: var MYAPP = MYAPP || {}; MYAPP.namespace = ...

  4. composer "Failed to decode zlib stream"

    dockerFile 中安装composer.... RUN curl -s -f -L -o /tmp/installer.php https://raw.githubusercontent.com ...

  5. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

  6. CTEX windedt 打开中文tex乱码问题

    % !TEX encoding = System % !TEX program = pdflatex % !TEX encoding = System% !TEX program = pdflatex ...

  7. C型USB能阻止危险充电器通过USB传播恶意软件

    C型USB能阻止危险充电器通过USB传播恶意软件 C型USB设备(USB Type-C)的新型身份验证协议可以保护用户免受潜在的充电器损坏的风险,这种新型的USB还能减少被恶意软件的风险.基于密码的认 ...

  8. Dream------Hadoop--网络拓扑与Hadoop--摘抄

    两个节点在一个本地网络中被称为“彼此的近邻”是什么意思?在高容量数据处理中,限制因素是我们在节点间 传送数据的速率-----带宽很稀缺.这个想法便是将两个节点间的带宽作为距离的衡量标准.   衡量节点 ...

  9. Nginx是什么,有什么优点?为什么选择Nginx做web服务器软件?(经典经典)

    1.基础知识 代理服务器:    一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端.应用比如:GoAgent,FQ神器.    一个完整的代理请求过程为:客 ...

  10. 【windows核心编程】HideProcess

    A Basic Windows DKOM Rootkit Pt 1 https://www.landhb.me/posts/v9eRa/a-basic-windows-dkom-rootkit-pt- ...