码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 题目4339 Query(线段树,单点更新)

时间:2015-07-31 16:14:34      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

Query

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


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 2 1 2 2 2 3 1 1 2 b 2 0 2 3
 

Sample Output
Case 1: 2 1 0 1 4 1
 

Source
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4338 4331 4332 4333 4334 
题目大意:两种操作,1 2,操作为1时输入 a b c ,第a个字符串的第b位变成c,操作为2时,输入a ,询问从第a位起两个字符串有多少位是相等的
ac代码
#include<stdio.h>
#include<string.h>
#define min(a,b) (a>b?b:a)
int node[1000010<<2];
char s1[1000010],s2[1000010];
void pushup(int tr)
{
	node[tr]=node[tr<<1]+node[tr<<1|1];
}
void build_tr(int l,int r,int tr)
{
	if(l==r)
	{
		if(s1[l]==s2[l])
		{
			node[tr]=1;
		}
		else
			node[tr]=0;
		return;
	}
	int mid=(l+r)>>1;
	build_tr(l,mid,tr<<1);
	build_tr(mid+1,r,tr<<1|1);
	pushup(tr);
}
void update(int pos,int l,int r,int tr)
{
	if(l==r)
	{
		if(s1[l]==s2[l])
		{
			node[tr]=1;
		}
		else
			node[tr]=0;
		return;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
	{
		update(pos,l,mid,tr<<1);
	}
	else
		update(pos,mid+1,r,tr<<1|1);
	pushup(tr);
}
int query(int pos,int l,int r,int tr)
{
	if(l==r)
		return node[tr];
	if(pos>=l&&pos<=r)
	{
		if(node[tr]==r-l+1)
		{
			return r-pos+1;
		}
		else
		{
			int mid=(l+r)>>1;
			int ans=0;
			if(pos<=mid)
			{
				ans+=query(pos,l,mid,tr<<1);
				if(ans==mid-pos+1)
					ans+=query(mid+1,mid+1,r,tr<<1|1);
			}
			else
				ans+=query(pos,mid+1,r,tr<<1|1);
			return ans;
		}
	}
}
int main()
{
	int t,c=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s%s",s1+1,s2+1);
		int len1=strlen(s1+1);
		int len2=strlen(s2+1);
		int len=min(len1,len2);
		build_tr(1,len,1);
		int q;
		scanf("%d",&q);
		printf("Case %d:\n",++c);
		while(q--)
		{
			int op;
			scanf("%d",&op);
			if(op==1)
			{
				char s[2];
				int a,b;
				scanf("%d%d%s",&a,&b,s);
				if(a==1)
					s1[b+1]=s[0];
				else
					s2[b+1]=s[0];
				update(b+1,1,len,1);
			}
			else
			{
				int a;
				scanf("%d",&a);
				printf("%d\n",query(a+1,1,len,1));
			}

		}
	}
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ 题目4339 Query(线段树,单点更新)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/47171049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!