hdu 4046 2011北京赛区网络赛G 线段树 ***
还带这么做的,卧槽,15分钟就被A了的题,居然没搞出来
若某位是1,则前两个为wb,这位就是w
#include<cstdio>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=;
int n,m,sum[maxn<<];
char str[maxn];
int num[maxn];
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=num[l];
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int m=(l+r)>>;
int ans=;
if(L<=m) ans+=query(L,R,lson);
if(R>m) ans+=query(L,R,rson);
return ans;
}
void update(int p,int val,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=val;
return;
}
int m=(l+r)>>;
if(p<=m) update(p,val,lson);
else update(p,val,rson);
pushup(rt);
}
int main()
{
int t,T;
scanf("%d",&T);
for(int t=;t<=T;t++)
{
printf("Case %d:\n",t);
scanf("%d%d%s",&n,&m,str+);
memset(num,,sizeof(num));
for(int i=;i<=n;i++)
if(str[i-]=='w'&&str[i-]=='b'&&str[i]=='w') num[i]=;
build(,n,);
int k,a,b;
char ch[];
while (m--)
{
scanf("%d",&k);
if(k==)
{
scanf("%d%d",&a,&b);
a++;b++; //字符串从1开始,所以下标都加1
if(b-a<) printf("0\n");
else printf("%d\n",query(a+,b,,n,));
}
else
{
scanf("%d%s",&a,ch);
a++;
if(ch[]==str[a]) continue;//修改的和以前一样,这不用任何操作
if(a>=)
{
if(str[a-]=='w'&&str[a-]=='b'&&str[a]=='w')
update(a,,,n,);
if(str[a-]=='w'&&str[a-]=='b'&&str[a]=='b')
update(a,,,n,);
}
if(a>=&&a+<=n)
{
if(str[a-]=='w'&&str[a]=='b'&&str[a+]=='w')
update(a+,,,n,);
if(str[a-]=='w'&&str[a]=='w'&&str[a+]=='w')
update(a+,,,n,);
}
if(a+<=n)
{
if(str[a]=='w'&&str[a+]=='b'&&str[a+]=='w')
update(a+,,,n,);
if(str[a]=='b'&&str[a+]=='b'&&str[a+]=='w')
update(a+,,,n,);
}
str[a]=ch[];
}
}
}
return ;
}
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root 1,n,1
#define mid ((l+r)>>1)
#define ll long long
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
using namespace std;
const int MAXN=;
int sum[MAXN<<],lsum[MAXN<<],rsum[MAXN<<],lc[MAXN<<],rc[MAXN<<];
int n,m,tt;
char s[MAXN];
void pushup(int l,int r,int rt)
{
//printf("%d %d %d %d\n",l,r,rt,mid);
/*if(mid==3)
{
printf("%s\n",s+1);
printf("%c %c %c\n",s[mid-1],s[mid],s[mid+1]);
}*/
sum[rt]=sum[rt<<]+sum[rt<<|];
if(/*(s[mid]=='b'&&s[mid-1]=='w'&&s[mid+1]=='w')||*/(s[mid]=='w'&&s[mid+]=='b'&&s[mid+]=='w')&&mid+<=n)
{
sum[rt]+=;
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=;
return;
}
build(lson);
build(rson);
pushup(l,r,rt);
}
void update(int pos,int val,int l,int r,int rt)
{
if(l==r)
{
return;
}
update(pos,val,lson);
update(pos,val,rson);
pushup(l,r,rt);
}
int query(int L,int R,int l,int r,int rt)
{ //printf("%d %d %d %d %d\n",L,R,l,r,rt);
if(L<=l&&r<=R)
{
return sum[rt];
}
if(R<=mid) return query(L,R,lson);
if(L>mid) return query(L,R,rson); int ta,tb;
ta=query(L,R,lson);
tb=query(L,R,rson);
int ans;
ans=max(ta,tb);
if(/*(s[mid]=='b'&&s[mid-1]=='w'&&s[mid+1]=='w')||*/(s[mid]=='w'&&s[mid+]=='b'&&s[mid+]=='w')&&mid+<=n) //同上
{
ans+=;
}
return ans;
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
int ca=;
while(tt--)
{
printf("Case %d:\n",ca++);
scanf("%d%d",&n,&m);
scanf("%s",s+);
s[]='k';
build(root);
while(m--)
{
int op;
scanf("%d",&op);
if(op==)
{
int l,r;
scanf("%d%d",&l,&r);
//printf("%d %d\n",l,r);
l++,r++;
int w=query(l,r,root);
printf("%d\n",w);
}
else
{
int x,val;
char c[];
scanf("%d%s",&x,c);
x++;
if(c[]!=s[x]) s[x]=c[],update(x,val,root); }
}
}
}
TLE代码
hdu 4046 2011北京赛区网络赛G 线段树 ***的更多相关文章
- hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***
不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...
- hdu 4044 2011北京赛区网络赛E 树形dp ****
专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...
- hdu 4050 2011北京赛区网络赛K 概率dp ***
题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...
- hdu 4049 2011北京赛区网络赛J 状压dp ***
cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...
- hdu 4045 2011北京赛区网络赛F 组合数+斯特林数 ***
插板法基础知识 斯特林数见百科 #include<iostream> #include<cmath> #include<cstdio> #include<cs ...
- hdu 4043 2011北京赛区网络赛D 概率+大数 **
推出公式为:P = A(2n,n)/(2^(2n)*n!) 但是不会大数,学完java再补
- hdu 4041 2011北京赛区网络赛B 搜索 ***
直接在字符串上搜索,注意逗号的处理 #include<cstdio> #include<iostream> #include<algorithm> #include ...
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- hdu 4035 2011成都赛区网络赛E 概率dp ****
太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...
随机推荐
- Js 的 this 是什么
this指的是调用函数的那个对象,谁调用函数,谁就是this 更多: http://www.cnblogs.com/justany/archive/2012/11/01/the_keyword_thi ...
- NFV技术中遇到的新名词
NUMA topology:Non-Uniform Memory Access (NUMA) is a computer system architecture that is used with m ...
- git stash简介
原文:http://gitbook.liuhui998.com/4_5.html 一.基本操作 当你正在做一项复杂的工作时, 发现了一个和当前工作不相关但是又很讨厌的bug. 你这时想先修复bug再做 ...
- Hadoop2.2.0环境下Sqoop1.99.3安装
本文转载自http://blog.csdn.net/liuwenbo0920/article/details/40504045 1.安装准备工作: 已经装好的hadoop环境是hadoop 2.2.0 ...
- SharePoint2010母版页想要的定制
查找<div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle"用style="disp ...
- ios Swift 一些注意事项
func test(one:NSString) -> NSString{ return "aaa" } func test(one:Int) -> NSString{ ...
- delphi的取整函数round、trunc、ceil和floor
delphi的取整函数round.trunc.ceil和floor 首先引入math单元 uses math; 1.Round(四舍六入五留双) 功能说明:对一个实数进行四舍五入.(按照银行家算法) ...
- RTX闪退(打开闪退,收发文件闪退)
之前遇到RTX只要一打开就闪退的情况,覆盖重装了RTX不管用,换了一个位置安装,然后问题解决了 又遇到一个问题,收文件或发文件就闪退,覆盖重装了不管用,换了位置安装还是不管用,清理垃圾·清理注册表不管 ...
- iOS MD5加密实现方法
使用方法 先导入头文件 #import "MD5.h" //md5测试 NSString * md1= [MD5 md5:" ]; NSLog(@"32bit= ...
- poj 2389.Bull Math 解题报告
题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次 ...