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.被杀死,回到结点 ...
随机推荐
- 通过nsenter连接docker容器
通常连接Docker容器并与其进行交互有四种方法.详情见:https://github.com/berresch/Docker-Enter-Demo,下面摘录nsenter连接的方式. 查看是否安装n ...
- perl运行其他程序的5种方法
1.使用system函数 运行成功,返回0,运行失败则返回非负整数 system("cmd"); 2.使用qx my $cmd1=qx/date/; 3.使用`` 与qx等效 4. ...
- [Android]如何获取当前用户设置的时区
方法:TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);获取的值如GMT+08:00,GMT-04:00,EDT 另附:国家码查询 ...
- C++与C#对比学习:类初始化
类和柏拉图的理念世界 我们知道面向对象编程中到处是一个个的类,但类只是个概念性的东西,不是个实体,不占内存,你没实例化之前也不能用它.只有把类实例化成一个对象后,它才是一个真正存在的实体.占有内存,能 ...
- JavaScript高级程序设计学习笔记--基本概念
1.语句 ECMAScript中的语句以一个分号结尾:如果省略分号,则由解析器确定语句的结尾,如下例所示: var sum=a+b //即使没有分号也是有效的语句--推荐 var diff=a-b; ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- $.each 和$(selector).each()的区别
$.each() 对数组或对对象内容进行循环处理 jQuery.each( collection, callback(indexInArray, valueOfElement) ) collectio ...
- nginx配置负载
一.系统优化 1.修改/etc/sysctl.conf,优化tcp连接数 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 120 ...
- struts.xml配置详解
struts.xml是我们在开发中利用率最高的文件,也是Struts2中最重要的配置文件. 一下分别介绍一下几个struts.xml中常用到的标签 1.<include> 利用includ ...
- jvm学习
一.jps主要用来输出JVM中运行的进程状态信息 jps [options] [hostid] 如果不指定hostid就默认为当前主机或服务器. Jps -ml 二.jstack主要用来查看某个Jav ...