HDU 4339 Query(线段树)
来源:互联网 发布:算法与程序设计 编辑:程序博客网 时间:2024/05/16 15:53
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
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int maxn = 1000100;int tcase, q;int cc[maxn<<2][2];int llen[maxn<<2], rlen[maxn<<2], mlen[maxn<<2]; // 左端点开始的最大的相同长度,右端开始的最大的相同长度,整个区间里的最大相同长度char s1[maxn], s2[maxn];void pushUp(int l, int r, int rt){ int ls = rt << 1; int rs = rt << 1 | 1; int m = (l + r) >> 1; llen[rt] = llen[ls]; rlen[rt] = rlen[rs]; if (llen[ls] == m - l + 1) { llen[rt] += llen[rs]; } if (rlen[rs] == r - m) { rlen[rt] += rlen[ls]; } mlen[rt] = max(rlen[ls] + llen[rs], max(llen[rt], rlen[rt]));}void build(int l, int r, int rt){ if (l == r) { cc[rt][0] = s1[l]; cc[rt][1] = s2[l]; if (cc[rt][0] == cc[rt][1]) { rlen[rt] = llen[rt] = mlen[rt] = 1; } else { rlen[rt] = llen[rt] = mlen[rt] = 0; } return ; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushUp(l, r, rt); }void update(int l, int r, int rt, int p, int id, int c){ if (l == r) { cc[rt][id] = c; if (cc[rt][id] == cc[rt][!id]) { llen[rt] = rlen[rt] = mlen[rt] = 1; } else { llen[rt] = rlen[rt] = mlen[rt] = 0; } return ; } int m = (l + r) >> 1; if (p <= m) { update(l, m, rt << 1, p, id, c); } else { update(m + 1, r, rt << 1 | 1, p, id, c); } pushUp(l, r, rt);}int query(int l, int r, int rt, int from){ if (l == r) { return mlen[rt]; } int m = (l + r) >> 1; if (from <= m) { if (rlen[rt<<1] >= m - from + 1) { return m - from + 1 + query(m + 1, r, rt << 1 | 1, m + 1); } else { return query(l, m, rt << 1, from); } } else { return query(m + 1, r, rt << 1 | 1, from); }}int main(){ scanf("%d", &tcase); for (int cas = 1; cas <= tcase; ++cas) { printf("Case %d:\n", cas); scanf("%s", s1); scanf("%s", s2); int L = min(strlen(s1), strlen(s2)); build(0, L - 1, 1); scanf("%d", &q); int op; for (int i = 0; i < q; ++i) { scanf("%d", &op); if (op == 1) { int id, p; char C; scanf("%d %d %c", &id, &p, &C); if (p >= L) { continue; } update(0, L - 1, 1, p, id - 1, C); } else { int from; scanf("%d", &from); if (from >= L) { printf("0\n"); continue; } printf("%d\n", query(0, L - 1, 1, from)); } } } return 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-5957 Query on a graph(线段树+树剖)
- hdu 4339 Query 线段树 多校联合赛(四) 第九题
- HDOJ 题目4339 Query(线段树,单点更新)
- HDU 4836 The Query on the Tree (欧拉序列,线段树,动态树)
- [HDU 5869] Different GCD Subarray Query (线段树GCD+离线算法) (好题)
- HDU 4836 —— The Query on the Tree(线段树+LCA)
- hdu 4836 The Query on the Tree(线段树or树状数组)
- java2 实用教程 第三版 第三章 习题 8
- VS2010快捷键
- 《花开半夏》--8 拥挤(1)
- struts2标签库
- 《花开半夏》--8 拥挤(2)
- HDU 4339 Query(线段树)
- UVA 10047 独轮车
- 中科院集成电化学方法的表面等离子体共振及高通量分析仪器通过验收
- Unity3d Android开发 Android Development
- 软件测试网络资源小结贴V1.0
- 将mysql中的Blob的图片在jsp中显示
- hdu 4335 A^x = A^(x % Phi(C) + Phi(C)) (mod C)(x>=phi(c))
- 利用rpcgen构建分布式程序的简单分析图
- 对概率的一些思考