Panda

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3343    Accepted Submission(s): 1075

Problem Description
When I wrote down this letter, you may have been on the airplane to U.S. 

We have known for 15 years, which has exceeded one-fifth of my whole life. I still remember the first time we went to the movies, the first time we went for a walk together. I still remember the smiling face you wore when you were dressing in front of the mirror.
I love your smile and your shining eyes. When you are with me, every second is wonderful.

The more expectation I had, the more disappointment I got. You said you would like to go to U.S.I know what you really meant. I respect your decision. Gravitation is not responsible for people falling in love. I will always be your best friend. I know the way
is difficult. Every minute thinking of giving up, thinking of the reason why you have held on for so long, just keep going on. Whenever you’re having a bad day, remember this: I LOVE YOU.

I will keep waiting, until you come back. Look into my eyes and you will see what you mean to me.

There are two most fortunate stories in my life: one is finally the time I love you exhausted. the other is that long time ago on a particular day I met you.

Saerdna.



It comes back to several years ago. I still remember your immature face.

The yellowed picture under the table might evoke the countless memory. The boy will keep the last appointment with the girl, miss the heavy rain in those years, miss the love in those years. Having tried to conquer the world, only to find that in the end, you
are the world. I want to tell you I didn’t forget. Starry night, I will hold you tightly. 



Saerdna loves Panda so much, and also you know that Panda has two colors, black and white.

Saerdna wants to share his love with Panda, so he writes a love letter by just black and white.

The love letter is too long and Panda has not that much time to see the whole letter.

But it's easy to read the letter, because Saerdna hides his love in the letter by using the three continuous key words that are white, black and white.

But Panda doesn't know how many Saerdna's love there are in the letter.

Can you help Panda?
 
Input
An integer T means the number of test cases T<=100

For each test case:

First line is two integers n, m

n means the length of the letter, m means the query of the Panda. n<=50000,m<=10000

The next line has n characters 'b' or 'w', 'b' means black, 'w' means white.

The next m lines 

Each line has two type

Type 0: answer how many love between L and R. (0<=L<=R<n)

Type 1: change the kth character to ch(0<=k<n and ch is ‘b’ or ‘w’)
 
Output
For each test case, output the case number first.

The answer of the question.
 


Sample Input
  1. 2
  2. 5 2
  3. bwbwb
  4. 0 0 4
  5. 0 1 3
  6. 5 5
  7. wbwbw
  8. 0 0 4
  9. 0 0 2
  10. 0 2 4
  11. 1 2 b
  12. 0 0 4
 
Sample Output
  1. Case 1:
  2. 1
  3. 1
  4. Case 2:
  5. 2
  6. 1
  7. 1
  8. 0
 

题目大意:

文章开头是一篇凄美的情书,深得我心~然后他们之间的约定便是信中特殊格式的字符串,wbw。题目要求去统计区间之内有多少个这样的字符串。





解题思路:

这不就是区间求和吗?自然而然就联想到了树状数组。隐形的看不见的数组a[]已经淡化了。如果要写出来,以第一组字符串为例就是这个样子:(字符串下标从1开始)





a[1]=0,a[2]=0,a[3]=0,a[4]=1,a[5]=0;

代表从位置i开始,与向前的i-1和i-2能否构成一个特殊的wbw。c[]数组就是对a[]的加和,但是代码中可以不用写出来。WA是因为,没注意每次查询之后进行字符的更新,坑。





源代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5. #include<stack>
  6. #include<queue>
  7. #include<vector>
  8. #include<deque>
  9. #include<map>
  10. #include<set>
  11. #include<algorithm>
  12. #include<string>
  13. #include<iomanip>
  14. #include<cstdlib>
  15. #include<cmath>
  16. #include<sstream>
  17. #include<ctime>
  18. using namespace std;
  19.  
  20. typedef long long ll;
  21. #define eps 1e-6
  22. #define e exp(1.0)
  23. #define pi acos(-1.0)
  24. const int MAXN = 50010;
  25. const int INF = 0x3f3f3f3f;
  26.  
  27. int c[MAXN];
  28. int n,m;
  29. int lowbit(int x)
  30. {
  31. return x&(-x);
  32. }
  33.  
  34. void add(int x, int num)
  35. {
  36. while(x<=n)
  37. {
  38. c[x]+=num;
  39. x+=lowbit(x);
  40. }
  41. }
  42.  
  43. int sum(int x)
  44. {
  45. int res=0;
  46. while(x>0)
  47. {
  48. res+=c[x];
  49. x-=lowbit(x);
  50. }
  51. return res;
  52. }
  53.  
  54. int main()
  55. {
  56. int t;
  57. char s[MAXN];
  58. int i,j;
  59. int o,l,r,k;
  60. char ch;
  61. scanf("%d",&t);
  62. for(j=1;j<=t;j++)
  63. {
  64. scanf("%d%d",&n,&m);
  65. scanf("%s",s);
  66. memset(c,0,sizeof(c));
  67. for(i=2;i<n;i++)
  68. if(s[i]=='w'&&s[i-1]=='b'&&s[i-2]=='w')
  69. add(i+1,1);
  70. printf("Case %d:\n", j);
  71. for(i=0;i<m;i++)
  72. {
  73. scanf("%d",&o);
  74. if(o==0)
  75. {
  76. scanf("%d %d",&l,&r);
  77. l++,r++;
  78. if(l>r)
  79. swap(l,r);
  80. if(r-l<2)
  81. printf("0\n");
  82. else
  83. printf("%d\n",sum(r)-sum(l+1));
  84. }
  85. if(o==1)
  86. {
  87. scanf("%d %c",&k,&ch);
  88. if(s[k]==ch)
  89. continue;
  90. if(k-2>=0&&k<=n-1)
  91. {
  92. if(s[k]=='w'&&s[k-1]=='b'&&s[k-2]=='w')
  93. add(k+1,-1);
  94. if(ch=='w'&&s[k-1]=='b'&&s[k-2]=='w')
  95. add(k+1,1);
  96. }
  97. if(k-1>=0&&k+1<=n-1)
  98. {
  99. if(s[k-1]=='w'&&s[k]=='b'&&s[k+1]=='w')
  100. add(k+2,-1);
  101. if(s[k-1]=='w'&&ch=='b'&&s[k+1]=='w')
  102. add(k+2,1);
  103. }
  104. if(k+2<=n-1)
  105. {
  106. if(s[k]=='w'&&s[k+1]=='b'&&s[k+2]=='w')
  107. add(k+3,-1);
  108. if(ch=='w'&&s[k+1]=='b'&&s[k+2]=='w')
  109. add(k+3,1);
  110. }
  111. s[k]=ch;
  112. }
  113. }
  114. }
  115. return 0;
  116. }




HDU4046--Panda(树状数组)的更多相关文章

  1. hdu 4046 Panda 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  4. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  7. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  8. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  9. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

随机推荐

  1. LeetCode 561. Array Partition I (数组分隔之一)

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  2. JDBC的基本用法

    一.编程步骤 1.加载驱动 Class forName("com.mysql.jdbc.Driver"):mysql驱动 Class forName("oralce.jd ...

  3. swift之函数式编程(四)

    文章内容来自<Functional Programing in Swift>,具体内容请到书中查阅 Map, Filter, Reduce Functions that take func ...

  4. 移动端click事件延迟300ms的原因以及解决办法[转载]

    原文:http://www.bubuko.com/infodetail-822565.html 这要追溯至 2007 年初.苹果公司在发布首款 iPhone 前夕,遇到一个问题 —— 当时的网站都是为 ...

  5. HTML学习笔记 CSS文本及字体及连接及列表(a标签使用及缩进) 案例 第七节 (原创)参考使用表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 详细图解window环境mongodb下载、安装、配置与使用

    到官网下载最新版面mongodb安装包,(32位版本的已经取消了,只有64位的) 官网地址: https://www.mongodb.com/download-center#community 下载完 ...

  7. 开发环境MAPLAB下使用仿真器ICD2程序下载流程

    程序下载流程 一.    连接示意图 二.    下载步骤 1.目标板电源断开 2.将仿真器下载端口与电路板JTAG端口有效连接 3.启动MPLAB软件 4.点击MAPLAB软件上方Programme ...

  8. 自定义Base16加密

                                                                                              自定义Base16加 ...

  9. rewirte 规则

    Nginx Rewrite Rewirte 规则也称为规则重写,主要功能是实现浏览器访问 HTTP URL 的跳转,其正则 表达式是基于 Perl 语言.通常而言,几乎所有的 WEB 服务器均可以支持 ...

  10. C# 程序集安装与卸载

    下面我们来实现程序集的安装,代码如下: /// <summary>/// 程序集安装与卸载/// </summary>/// <param name="asse ...