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

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

2012 Multi-University Training Contest 4

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));
			}

		}
	}
}

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

时间: 2024-10-20 20:40:25

HDOJ 题目4339 Query(线段树,单点更新)的相关文章

hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15719    Accepted Submission(s): 6629 Problem Description At the entrance to the university, there is a huge rectangular billboard of

HDU2852_KiKi&#39;s K-Number(线段树/单点更新)

解题报告 题目传送门 题意: 意思很好理解. 思路: 每次操作是100000次,数据大小100000,又是多组输入.普通模拟肯定不行. 线段树结点记录区间里存在数字的个数,加点删点操作就让该点个数+1,判断x存在就查询[1,x]区间的个数和[1,x-1]的个数. 求x之后第k大的数就先确定小于x的个数t,第t+k小的数就是要求的. #include <iostream> #include <cstdio> #include <cstring> using namespa

HDU 1754 I Hate It (线段树 单点更新)

题目链接 中文题意,与上题类似. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <cstdlib> 6 #include <algorithm> 7 const int maxn = 200000+10; 8 using namespace std; 9 int a[maxn], n, m; 10

HDU 1166 敌兵布阵(线段树单点更新,板子题)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 87684    Accepted Submission(s): 36912 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

hdu 1754 I Hate It 线段树单点更新和区间求和

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 参照HH大牛写的额! Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有时候需要更新某位同学的成绩. Input 本题目包含多

HDU 1166 敌兵布阵 (线段树 单点更新)

题目链接 线段树掌握的很差,打算从头从最简单的开始刷一波, 嗯..就从这个题开始吧! 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <cstdlib> 6 #include <algorithm> 7 const int maxn = 50000+10; 8 using namespace std

hdu 1166 敌兵布阵 (线段树单点更新)

敌兵布阵                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)                                                                             Total Submission(s): 59474   

NYOJ 116 士兵杀敌(二)【线段树 单点更新】

题意:题意很清楚: 策略:如题. 这道题就是简单的线段树应用,据说还可以用树状数组来做,等我学了之后在说吧. 代码: #include<stdio.h> #include<string.h> #define LC l, m, rt<<1 #define RC m+1, r, rt<<1|1 #define LL long long #define MAXN 1000000 LL sum[MAXN<<2]; void PushUp(int rt)

【HDU】1754 I hate it ——线段树 单点更新 区间最值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 37448    Accepted Submission(s): 14816 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

HDU 1754 I Hate It 线段树单点更新求最大值

题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #define N 200005 using namespace std; int data[N]; struct Tree { int l,r,ans; }tree[N*4]; void build(in