HDU 4339 Query【线段树】单点更新,动态查询

Query

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3010 Accepted Submission(s): 964


Problem Description
You are given two strings s1[0..l1], s2[0..l2] and Q - number of queries.
Your task is to answer next queries:
1) 1 a i c - you should set i-th character in a-th string to c;
2) 2 i - you should output the greatest j such that for all k (i<=k and k<i+j) s1[k] equals s2[k].

Input
The first line contains T - number of test cases (T<=25).
Next T blocks contain each test.
The first line of test contains s1.
The second line of test contains s2.
The third line of test contains Q.
Next Q lines of test contain each query:
1) 1 a i c (a is 1 or 2, 0<=i, i<length of a-th string, 'a'<=c, c<='z')
2) 2 i (0<=i, i<l1, i<l2)
All characters in strings are from 'a'..'z' (lowercase latin letters).
Q <= 100000.
l1, l2 <= 1000000.

Output
For each test output "Case t:" in a single line, where t is number of test (numbered from 1 to T).
Then for each query "2 i" output in single line one integer j.

Sample Input
      
      
1
aaabba
aabbaa
7
2 0
21
2 2
2
2 3
1
1b
2 0
2 3

Sample Output
      
      
Case 1:
2
1
1
0
4
1


本来还感觉自己学的还可以,结果发现自己连递归都控制不好,一直出错,数组下标都弄混了

最后借鉴学长的思路才解决,自己还是迷迷糊糊的..........


找从某个位置处开始两个字符串连续相同的字符的个数........


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[2][1000005];
int sum[4000005];
void build(int rt,int l,int r)//建树没什么说的
{
	if(l==r)
	{
		sum[rt]=0;
		if(s[0][r]==s[1][r])
		{
			sum[rt]=1;
		}
		return;
	}
	int mid=(l+r)>>1,tp=rt<<1;
	build(tp,l,mid);build(tp|1,mid+1,r);
	sum[rt]=sum[tp]+sum[tp|1];
}

void update(int rt,int l,int r,int i)//更新也没什么说的
{
	if(l==r)
	{
		sum[rt]=0;//醉了,卡死在这了,坐标都混了 
		if(s[0][l]==s[1][l])
		{
			sum[rt]=1;
		}
		return;
	}
	int mid=(l+r)>>1,tp=rt<<1;
	if(i<=mid)
	{
		update(tp,l,mid,i);
	}
	else
	{
		update(tp|1,mid+1,r,i);
	}
	sum[rt]=sum[tp]+sum[tp|1];
}

int find(int rt,int l,int r,int b)
{
	if(l==r)//单个元素的时候直接返回 
	{
		return sum[rt];
	}
	if(b>=l&&b<=r)
	{
		if(sum[rt]==r-l+1)//一段都相同,直接返回
		{
			return r-b+1;
		}
		else
		{
			int mid=(l+r)>>1,tp=rt<<1,res=0;
			if(b<=mid)//查询点在中间点左边,需要讨论
			{
				res+=find(tp,l,mid,b);//先递归计算左半边有效区域
				if(res==mid-b+1)//左边全部为1
				{
					res+=find(tp|1,mid+1,r,mid+1);//则查3右边
				}
			}
			else
			{
				res+=find(tp|1,mid+1,r,b);//否则直接递归右边
			}
			return res;
		}
	}
}

int main()
{
	int t,n,m;
//	freopen("shuju.txt","r",stdin);
	scanf("%d",&t);
	for(int k=1;k<=t;++k)
	{
		memset(s,0,sizeof(s));
		scanf("%s%s%d",s[0]+1,s[1]+1,&m);
		n=min(strlen(s[0]+1),strlen(s[1]+1)); 
		build(1,1,n);
		printf("Case %d:\n",k);
		while(m--)
		{
			int kase;
			scanf("%d",&kase);
			if(kase==1)
			{
				int a,i;char c[2];
				scanf("%d%d%s",&a,&i,c);
				s[a-1][i+1]=c[0];//修改单点 
				update(1,1,n,i+1);//更新树 
			}
			else
			{
				int b;
				scanf("%d",&b);
				printf("%d\n",find(1,1,n,b+1));//
			}
		}
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值