Hdu 4399 good line segment tree

Source: Internet
Author: User

Query
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 1857 Accepted Submission (s): 595


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 shoshould set I-th character in a-th string to c;
2) 2 I-you shocould 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


Question:

Question: There are two strings and Q queries. Each query has two formats:

1 p I c: Replace the I character of the p string with the c character,

2i starts from the second I, and the maximum length of the two substrings that are consecutively the same.


Ideas
If the position I is the same, it is 0. If the position I is different, it is 1. In this way, it can be converted to the position where I is followed by the first 1.
At first, I did not know how to implement the query function.

I passed it by referring to Daniel.

[Cpp]
# Include <stdio. h>
# Include <string. h>
Struct haha
{
Int left;
Int right;
Int r_len;
Int l_len;
} Node [1000011*4];
Char s1 [1000011], s2 [1000011];
Int a [1000011];
Void pushup (int nd)
{
Int rl = node [nd * 2 + 1]. right-node [nd * 2 + 1]. left + 1;
Int ll = node [nd * 2]. right-node [nd * 2]. left + 1;
Node [nd]. l_len = node [nd * 2]. l_len;
Node [nd]. r_len = node [nd * 2 + 1]. r_len;
If (node [nd]. l_len = ll) node [nd]. l_len + = node [nd * 2 + 1]. l_len;
If (node [nd]. r_len = rl) node [nd]. r_len + = node [nd * 2]. r_len;
}
Void build (int left, int right, int nd)
{
Int mid;
Mid = (left + right)/2;
Node [nd]. left = left;
Node [nd]. right = right;
If (left = right)
{
If (s1 [left] = s2 [left]) {a [left] = 1; node [nd]. r_len = node [nd]. l_len = 1 ;}
Else {a [left] = 0; node [nd]. r_len = node [nd]. l_len = 0 ;}
Return;
}
Build (left, mid, nd * 2 );
Build (mid + 1, right, nd * 2 + 1 );
Pushup (nd );
}
Void update (int pos, int val, int nd)
{
If (node [nd]. left = node [nd]. right) {a [pos] = val; node [nd]. l_len = node [nd]. r_len = val; return ;}
Else
{
Int mid = (node [nd]. left + node [nd]. right)/2;
If (pos <= mid) update (pos, val, nd * 2 );
Else update (pos, val, nd * 2 + 1 );

}
Pushup (nd );
}
Int query (int pos, int nd) // I didn't think about this function at first.
{
If (pos = node [nd]. left) return node [nd]. l_len;
If (node [nd]. left = node [nd]. right) return node [nd]. l_len;
If (pos> node [2 * nd]. right) return query (pos, nd * 2 + 1); // enter the right branch
Else
{
Int len = node [nd * 2]. right-pos + 1;
If (node [nd * 2]. r_len <len) return query (pos, nd * 2 );
Else return len + query (node [nd * 2 + 1]. left, nd * 2 + 1 );
}
}
Int main ()
{
Int cas, k, d2, d1, ccas = 0;
Scanf ("% d", & cas );
While (cas --)
{
Printf ("Case % d: \ n", ++ ccas );
Scanf ("% s", s1 + 1, s2 + 1 );
D1 = strlen (s1 + 1 );
D2 = strlen (s2 + 1 );
If (d1> d2) d1 = d2;
Build (1, d1, 1 );
Scanf ("% d", & k );
While (k --)
{
Int flag;
Scanf ("% d", & flag );
If (flag = 1)
{
Int kk, pos, val;
Char ch;
Scanf ("% d % c", & kk, & pos, & ch );
Pos ++;
If (pos> d1) continue; // note that this sentence is overcast.
If (kk = 1)
{
If (ch = s2 [pos]) val = 1;
Else
Val = 0;
S1 [pos] = ch;
}
Else
{
If (ch = s1 [pos]) val = 1;
Else val = 0;
S2 [pos] = ch;
}
Update (pos, val, 1 );
}
Else
{
Int left;
Scanf ("% d", & left );
Left ++;
Printf ("% d \ n", query (left, 1 ));
}
}
}
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.