HDU 4046 panda line segment tree

Source: Internet
Author: User

In the five online games, a large number of Line Segment trees were created, and each time they were sitting there, they were dummies ..

It seems that we need to chew on the Line Segment tree ..

 

# Include <iostream>
# Include <string>
# Define maxn50010
# Define lson L, M, RT <1
# Define rson m + 1, R, RT <1 | 1
Using namespace STD;
Int sum [maxn * 4];
Char STR [maxn];
Int A [maxn];
Void pushup (int rt)
{
Sum [RT] = sum [RT <1] + sum [RT <1 | 1];
}
Void build (int l, int R, int RT)
{
If (L = r ){
Sum [RT] = A [l];
Return;
}
Int M = (L + r)> 1;
Build (lson );
Build (rson );
Pushup (RT );
}

Void Update (int p, int add, int L, int R, int RT)
{
If (L = r)
{
Sum [RT] = add;
Return;
}
Int M = (L + r)> 1;
If (P <= m) Update (p, add, lson );
Else Update (p, add, rson );
Pushup (RT );
}
Int query (int l, int R, int L, int R, int RT)
{
If (L <= L & R <= r ){
Return sum [RT];
}
Int M = (L + r)> 1;
Int ret = 0;
If (L <= m) RET + = query (L, R, lson );
If (r> m) RET + = query (L, R, rson );
Return ret;
}
Int main ()
{
Int cas, T = 0, n, m, R, L, C;
Char st;
Scanf ("% d", & CAS );
While (CAS --)
{
Scanf ("% d", & N, & M );
Scanf ("% s", STR );
Memset (A, 0, sizeof ());
Printf ("case % d: \ n", ++ t );
For (INT I = 2; I <n; I ++) // indicates whether a substring whose length ends with this subscript is 3.
{
If (STR [I]! = 'W ')
A [I] = 0;
Else if (STR [I-1] = 'B' & STR [I-2] = 'W ')
A [I] = 1;
}
Build (0, N, 1 );
While (M --)
{
Scanf ("% d", & C );
If (C = 0)
{
Scanf ("% d", & L, & R );
L + = 2;
If (L> r) printf ("0 \ n ");
Else
Printf ("% d \ n", query (L, R, 0, N, 1 ));
}
Else
{
Scanf ("% d % C", & L, & St );
If (STR [l] = sT) // if you want to use the same character as the original one, you do not need to modify it.
Continue;
STR [l] = sT;
If (L> = 2 & STR [L-2] = 'W' & STR [L-1] = 'B' & STR [l] = 'W ')
{
If (A [l] = 0) // if it is a non-conforming string before modification, update
Update (L, 1, 0, N, 1 );
A [l] = 1;
}
Else if (L> = 2 & A [l] = 1) // if it is a required string before modification, update
{
Update (L, 0, 0, N, 1 );
A [l] = 0;
}
If (L> = 1 & STR [l] = 'B' & STR [L-1] = 'W' & STR [L + 1] = 'W ')
{
If (A [L + 1] = 0)
Update (L + 1, 1, 0, N, 1 );
A [L + 1] = 1;
}
Else if (L <n-1 & L> = 1 & A [L + 1] = 1) A [L + 1] = 0, update (L +, 0, N, 1 );
If (STR [l] = 'W' & STR [L + 1] = 'B' & STR [L + 2] = 'W ')
{
If (A [L + 2] = 0)
Update (L + 2, 1, 0, N, 1 );
A [L + 2] = 1;
}
Else
If (L <N-2 & A [L + 2] = 1)
A [L + 2] = 0, update (L + 2, 0, 0, N, 1 );
}
}
}
Return 0;
}

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.