HDU 4339 Query(线段树)
来源:互联网 发布:linux认证 编辑:程序博客网 时间:2024/04/29 23:42
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].
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.
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.
Then for each query "2 i" output in single line one integer j.
Sample Input
1aaabbaaabbaa72 02 12 22 31 1 2 b2 02 3
Sample Output
Case 1:210141
Source
2012 Multi-University Training Contest 4
并不难的线段树题目,然而比赛的时候没有做出来。。。准确的说是没有做。。。
首先是时间不够,看了一眼觉得区间合并会很麻烦,再加上线段树写得不是很溜,就没写
赛后仔细一看发现其实这题的区间合并蛮简单的。。。。。
保存区间[l,r]以l为起点的最长段为s
合并的时候只需要判断左子的s是否覆盖了左子的整个区间
若覆盖,则加上右子的s即可
另外,注意两个的串的长度没有说相等。。。
具体操作见代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<set>#include<stack>#include<cmath>#include<map>#include<stdlib.h>#include<cctype>#define mem(a,x) memset(a,x,sizeof(a))#define esp 1e-8using namespace std;typedef long long ll;const int N = 1000000;struct Node{ int l,r; int s;//区间[l,r]内以l为起点的最长相等段}q[N*4];string a,b;void build(int i,int l,int r){ q[i].l = l;q[i].r = r; if (l == r) { if (a[l] == b[l]) q[i].s = 1; else q[i].s = 0; return; } int mid = (l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid+1,r); q[i].s = q[i<<1].s;//先等于左子的最大长度 if (q[i<<1].s == mid - l + 1)//如果左子的整个区间都相等 { q[i].s += q[i<<1|1].s;//那么还可以加上右子 }}void update(int i,int x)//更改了第x个字符{ if (q[i].l == q[i].r) { if (a[q[i].l] == b[q[i].l]) q[i].s = 1; else q[i].s = 0; return ; } int mid = (q[i].l + q[i].r)>>1; if (x <= mid) update(i<<1,x); else update(i<<1|1,x); q[i].s = q[i<<1].s;//先等于左子的最大长度 if (q[i<<1].s == mid - q[i].l + 1)//如果左子的整个区间都相等 { q[i].s += q[i<<1|1].s;//那么还可以加上右子 }}int query(int i,int x){ if (q[i].l == q[i].r) { return q[i].s; } int mid = (q[i].l + q[i].r)>>1; if (x <= mid) { int tmp = query(i<<1,x); if (tmp == mid - x + 1) return tmp + q[i<<1|1].s; else return tmp; } else return query(i<<1|1,x);}int main(){ int T;scanf("%d",&T);int kas = 0; while (T--) { cin>>a>>b; int la = a.size(),lb = b.size(); int n = min(la,lb)-1; build(1,0,n); int q;scanf("%d",&q); printf("Case %d:\n",++kas);int op,x,i;char c; while (q--) { scanf("%d",&op); if (op == 1) { scanf("%d %d %c",&x,&i,&c); if (i > n) continue;//越界的不用更新了 if (x == 1) a[i] = c; else b[i] = c; update(1,i); } else { scanf("%d",&i); if (i > n) puts("0"); else printf("%d\n",query(1,i)); } } } return 0;}
线段树还是不够熟练啊。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
0 0
- hdu 4339 query#线段树
- 线段树 HDU 4339 Query
- 线段树-hdu-4339-Query
- hdu 4339 Query(线段树)
- hdu 4339 Query ---线段树
- HDU 4339 Query(线段树)
- HDU 4339 Query(线段树)
- hdu - 4339 - Query - 树状数组||线段树
- HDU 4339 Query (线段树)
- hdu 4339 Query(线段树+二分,4级)
- HDU 4339 Query【线段树】单点更新,动态查询
- hdu 4339 Query 线段树 多校联合赛(四) 第九题
- hdu 2795(线段树:query:区间求最大值的位子)
- 【HDU】3804 Query on a tree dfs+线段树
- 【HDU】4836 The Query on the Tree dfs+线段树
- HDU-5957 Query on a graph(线段树+树剖)
- Hdu 3804 Query on a tree 树链剖分+线段树
- HDU4339 Query (线段树)
- 206. Reverse Linked List--LeetCode Record
- 有线网(校园网)下WIN10 ie、edge浏览器不能上网,其他浏览器可以的解决方法。
- Atom设置代理
- 编程框架和MASM混合编程
- 2016.7.13 刷题打卡
- HDU 4339 Query(线段树)
- ssh中“Host key verification failed.“的解决方案
- C++默认参数
- 第一篇博客啦啦啦
- 欢迎使用CSDN-markdown编辑器
- Mono加密DLL也没用,借助IDA dump内存 直接提取解密后的DLL,反编译破解Unity游戏
- 自定义滚动条
- Linux基础篇——死锁
- TextInputLayout