Hdu oj 1166 enemy deployment array [Line Segment Tree insertion point question line]

Source: Internet
Author: User

Question: Needless to say ......
Idea: An entry line segment tree plug-in line, explained in the code
AC code:
[Cpp]
/*
Line Segment tree-insertion question line:
1: The line segment tree stores the sum of corresponding intervals.
2: When a value is updated, the parent node of the value is updated (in turn up until the root node ).
3: During the query, find the number of online segments that are divided into the corresponding cells and find sum.
*/
# Include <stdio. h>
# Include <string. h>
# Define Max 4*100000
# Define LL (a) a <1; // 2 *
# Define RR (a) a> 1 | 1; // 2 * a + 1
# Define Mid (x, y) (x + y)> 1 // (x + y)/2
Int A [Max], cnt, ans;
Struct hello
{
Int l;
Int r;
Int sum;
} Tree [Max];
Void Build_tree (int l, int r, int t) // l, r indicates the interval, and t indicates the interval Node
{
Int x;
Tree [t]. l = l;
Tree [t]. r = r;
Tree [t]. sum = 0;
If (l = r)
{
// Tree [t]. sum = A [l];
While (t! = 0)
{
Tree [t]. sum + = A [l];
T/= 2;
}
Return;
}
X = Mid (l, r );
Build_tree (l, x, 2 * t );
Build_tree (x + 1, r, 2 * t + 1 );
}
Void Updata_tree (int l, int r, int t) // l r indicates the interval to be updated, and t indicates the current node
{
If (tree [t]. l = l & tree [t]. r = r)
{
While (t! = 0)
{
Tree [t]. sum + = cnt;
T/= 2;
}
Return;
}
Int x = Mid (tree [t]. l, tree [t]. r );
If (x> = r)
Updata_tree (l, r, 2 * t );
Else if (x + 1 <= l)
Updata_tree (l, r, 2 * t + 1 );
Else
{
Updata_tree (l, x, 2 * t );
Updata_tree (x + 1, r, 2 * t + 1 );
}
}
Void Query_tree (int l, int r, int t) // l r indicates the interval t to be queried, indicating the current node
{
// Printf ("inin ------------- \ n ");
If (tree [t]. l = l & tree [t]. r = r)
{
// Printf ("outout ------ \ n ");
Ans + = tree [t]. sum;
Return;
}
Int x = Mid (tree [t]. l, tree [t]. r );
If (x> = r)
Query_tree (l, r, 2 * t );
Else if (x + 1 <= l)
Query_tree (l, r, 2 * t + 1 );
Else
{
Query_tree (l, x, 2 * t );
Query_tree (x + 1, r, 2 * t + 1 );
}
}
Int main ()
{
Int I, j, n, m, ncase;
Scanf ("% d", & ncase );
For (n = 1; n <= ncase; n ++)
{
Printf ("Case % d: \ n", n );
Scanf ("% d", & m );
For (I = 1; I <= m; I ++)
Scanf ("% d", & A [I]);
Build_tree (1, m, 1 );
Char str [100];
While (scanf ("% s", str) & str [0]! = 'E ')
{
Int x, y;
If (str [0] ='s ')
{
Scanf ("% d", & x, & cnt );
Cnt =-cnt;
Updata_tree (x, x, 1 );
}
Else if (str [0] = 'A ')
{
Scanf ("% d", & x, & cnt );
Updata_tree (x, x, 1 );
}
Else if (str [0] = 'q ')
{
Scanf ("% d", & x, & y );
Ans = 0;
Query_tree (x, y, 1 );
Printf ("% d \ n", ans );
}
}
}
}

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.