hdu_4046_Panda(树状数组)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4046
题意:一大堆篇幅介绍,跳过直奔主题,让你寻找给定区间的“wbw”的个数。
题解:直接上树状数组,改变字符后对应改变3个值就行,注意的是询问 [l,r],应该对应输出的是[l+1,r]。因为sum[l+1]记录了a[l-1],a[l],a[l+1]。
#include<cstdio>
const int maxn=;
int tree[maxn],n;
char a[maxn];
void fre(){freopen("c:\\acm\\input.txt","r",stdin);}
inline void add(int x,int v){for(;x<=n;x+=x&-x)tree[x]+=v;}
inline int ask(int x){int an=;for(;x;x-=x&-x)an+=tree[x];return an;}
void change(int x,char y){
int cg[],after[];
for(int i=;i<=;i++){
if(x+i-<)cg[i]=;
else if(a[x+i-]=='w'&&a[x+i-]=='b'&&a[x+i]=='w')cg[i]=;
else cg[i]=;
}
a[x]=y;
for(int i=;i<=;i++){
if(x+i-<)after[i]=;
else if(a[x+i-]=='w'&&a[x+i-]=='b'&&a[x+i]=='w')after[i]=;
else after[i]=;
}
for(int i=;i<=;i++){
int kk=after[i]-cg[i];
if(kk!=)add(x+i,kk);
}
}
int main(){
//fre();
int t,ic=,m,cmd;
scanf("%d",&t);
while(t--){
printf("Case %d:\n",ic++);
scanf("%d%d",&n,&m);
scanf("%s",a+);
for(int i=;i<=n;i++)tree[i]=;
for(int i=;i<=n;i++)if(a[i]=='w'&&a[i-]=='b'&&a[i-]=='w')add(i,);
for(int i=;i<=m;i++){
scanf("%d",&cmd);
if(cmd==){
int x,y;
scanf("%d%d",&x,&y);
x++,y++;
if(y-x<){printf("0\n");continue;}
if(y-x==){if(a[x]=='w'&&a[x+]=='b'&&a[x+]=='w')printf("1\n");else printf("0\n");}
else printf("%d\n",ask(y)-ask(x+));
}else{
int x;
char y[];
scanf("%d%s",&x,y);
x++;
if(a[x]==y[])continue;
else change(x,y[]);
}
}
}
return ;
}
hdu_4046_Panda(树状数组)的更多相关文章
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
随机推荐
- Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义
$# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示 ...
- Tsung测试统计报告说明
Tsung测试统计报告说明 ********************** report.html *********************** 1.主要统计信息 Tsung统计数据是平均每十秒重 ...
- 读取Zip文件内容
第一步,上次文件并保存到服务器目录下 /// <summary> /// 上传压缩文件 /// </summary> protected void UploadZip() { ...
- php中的curl常用例子
1.基本请求 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com"); ...
- String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别
string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“ ” 这样的字符就返回fa ...
- magento数据查询
一.查询出所有的数据: 1.以mysql查询语句在magento里执行,以此来查询你所需要的语句! $read = Mage::getSingleton('core/resource')->ge ...
- FZU 1502 Letter Deletion(DP)
Description You are given two words (each word consists of upper-case English letters). Try to delet ...
- 2016年团体程序设计天梯赛-决赛 L1-2. I Love GPLT(5)
这道超级简单的题目没有任何输入. 你只需要把这句很重要的话 —— “I Love GPLT”——竖着输出就可以了. 所谓“竖着输出”,是指每个字符占一行(包括空格),即每行只能有1个字符和回车. #i ...
- “this”总结
this 的几种情况: function a(){ alert(this); } 1.直接调用a()时,this指的是window对象. 2.对象调用 var obj = { a : a}obj.a( ...
- KindeEditor图片上传插件用法
因业务需要找了款插件 KindeEditor编辑器确认挺好用,但无奈技术有限,上传配置不知,故问度娘! 图片上传对于部分新手来说有时候是一件非常头疼的事,今天来分享一下项目中使用到的这个插件Kinde ...