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.被杀死,回到结点 ...
随机推荐
- ThinkPHP在Ubuntu上 : mkdir的使用
问题: 环境:Ubuntu系统,xampp1.8.3-5建站工具 描述:以root的身份运行.Apache的 DocumentRoot = "/var/www" .thinkphp ...
- C#的is和as操作符来进行强制类型转换&&值类型的拆箱、装箱
if(o is Employee) { Employee e=(Employee)o; //在if语句剩余的部分中使用e; } Employee e=o as Employee; if(e!=null ...
- java 调用webservice的各种方法总结
java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...
- 第十天 多进程、协程(multiprocessing、greenlet、gevent、gevent.monkey、select、selector)
1.多进程实现方式(类似于多线程) import multiprocessing import time,threading def thread_run():#定义一个线程函数 print(&quo ...
- CentOS 7部署flume
CentOS 7部署flume 准备工作: 安装java并设置java环境变量,在`/etc/profile`中加入 export JAVA_HOME=/usr/java/jdk1.8.0_65 ex ...
- Java for LeetCode 217 Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- MFC Initinstance中DoModal()返回-1
新建一个基于对话框的MFC应用程序,在App的Initinstance中调用对话框DoModal()来显示对话框,这是框架的内容,应用程序框架生成的全部是正常的. 当把我对话框的资源文件提取到一个资源 ...
- 【hadoop2.6.0】安装+例子运行
由于下载的是hadoop的最新版,网上各种杂七杂八的东西都不适用.好在官网上说的也够清楚了.如果有人看这篇文章的话,最大的忠告就是看官网. 官网2.6.0的安装教程:http://hadoop.apa ...
- PHP安全编程:不要让不相关的人看到报错信息
没有不会犯错的开发者,PHP的错误报告功 能可以协助你确认和定位这些错误,可以提供的这些错误的详细描述,但如果被恶意攻击者看到,这就不妙了.不能让大众看到报错信息,这一点很重要.做到这一 点很容易,只 ...
- iOS进阶面试题----多线程
1 多线程是什么 多线程是个复杂的概念,按字面意思是同步完成多 项任务,提高了资源的使用效率,从硬件.操作系统.应用软件不同的角度去看,多线程被赋予不同的内涵,对于硬件,现在市面上多数的CPU都是多核 ...