Line Segment tree-Interval Single Point Update-interval and-Interval Maximum

Source: Internet
Author: User

Line Segment tree:

The element [a, B] In the figure indicates that the value stored by the node is the result (maximum value or sum) between a and B ). If the root node is c, the numbers of the following two subtree nodes are c * 2 and c * 2 + 1.

Maximum interval

Question:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Expires + expires + 6437XEyse24MnZoaM8YnI + CtXiyMO63Lbg0afJ + expires + CrK7udzE48 + expires/a1xMrHo6y + expires/expires + w/LqstuDX6bLiytSj Examples/examples/cS/us2y2df3tcTK/cS/oaM8YnI + examples/examples + examples/Su9DQ09DSu7j219a3 + ybdic1_u8ih "Q' or 'U '), and two positive integers A and B.
When C is 'Q', it indicates that this is A query operation. It asks the students whose ID ranges from A to B (including A and B) about the highest score.
When C is 'U', it indicates that this is an update operation. You must change the score of students whose ID is A to B.

Output outputs the highest score in one row for each query operation.
Sample Input

5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5

Sample Output
5659

--- Change from a stray calf

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define lson l, m, root <1 # define rson m + 1, r, root <1 | 1 const int n= 200010; int segtree [N <1 + 10]; // double void PushUp (int root) // maximum node storage interval {segtree [root] = max (segtree [root <1], segtree [root <1 | 1]);} void Build_Tree (int l, int r, int root) {if (l = r) {scanf ("% d", & segtree [root]); // cout <
    
     
> 1; Build_Tree (lson); Build_Tree (rson); PushUp (root);} void Update (int pos, int data, int l, int r, int root) {if (l = r) {segtree [root] = data; return;} int m = (l + r)> 1; if (pos <= m) update (pos, data, lson); elseUpdate (pos, data, rson); PushUp (root);} int Query (int L, int R, int l, int r, int root) {int sum =-9999999; if (L <= l & r <= R) return segtree [root]; int m = (l + r)> 1; if (L <= m) sum = max (sum, Query (L, R, lson); if (R> m) sum = max (sum, Query (L, R, rson); return sum;} int main () {int num, que; char ope; int a, B; while (scanf ("% d", & num, & que )! = EOF) {Build_Tree (1, num, 1); // for (int I = 0; I
     
      


Question: interval summation

Soldiers (2) time limit: 1000 MS | memory limit: 65535 KB difficulty: 5
Description

There are N soldiers under general Nan, numbered 1 to N respectively. The number of these soldiers killing the enemy is known.

The minor engineer is a military division under general Nan. General Nan often wants to know the total number of enemies from general m to general n. Please help the minor engineer to answer the question.

After an inquiry by General Nan, the soldier I may have killed another q-man. Then, General Nan needs to consider the number of new enemies.

Input
Only one set of test data
The first line is two integers N, M, where N represents the number of soldiers (1 The next line is N integers, and ai indicates the number of attacked soldiers on the I. (0 <= ai <= 100)
Each row of the subsequent M line is an instruction, which contains a string and two integers. The first is a string. If it is a string QUERY, it indicates that General Nan has performed the QUERY operation, the two integers m, n, which indicate the start and end soldiers of the query. If the string is added, the two integers I, A (1 <= I <= N, 1 <= A <= 100), indicating that the number of new enemies of the I-th soldier is.
Output
For each query, an integer R is output to indicate the total number of enemies from soldiers m to soldiers n. Each group occupies one row.
Sample Input
5 61 2 3 4 5QUERY 1 3ADD 1 2QUERY 1 3ADD 2 3QUERY 1 2QUERY 1 5
Sample output
68820


--


# Include
           
            
Int sum [3000000]; void build (int B, int e, int index) {if (B = e) {scanf ("% d ", & sum [index]); return;} int mid = (B + e)/2, lchild = index <1, rchild = lchild | 1; build (B, mid, lchild); build (mid + 1, e, rchild); sum [index] = sum [lchild] + sum [rchild];} int query (int qb, int qe, int B, int e, int index) {if (qb <= B & e <= qe) return sum [index]; int mid = (B + e)/2, lchild = index <1, rchild = lchild | 1, lr = 0, rr = 0; if (qb <= mid) lr = query (qb, qe, B, mid, lchild); if (qe> mid) rr = query (qb, qe, mid + 1, e, rchild); return lr + rr;} void update (int B, int e, int node, int num, int index) {if (B = e & e = node) {sum [index] + = num; return ;} int mid = (B + e)/2, lchild = index <1, rchild = lchild | 1; if (node <= mid) update (B, mid, node, num, lchild); else update (mid + 1, e, node, num, rchild); sum [index] + = num;} int main () {int n, k, b, e; char str [10]; scanf ("% d", & n, & k); build (1, n, 1 ); for (int I = 0; I
            
             

Question: questions about the blue bridge cup exercise-combining the largest interval and

Algorithm training operation lattice time limit: 1.0 s memory limit: 256.0MB Problem Description

There are n grids in a row numbered 1-n from left to right.

There are a total of m operations, there are 3 types of operations:

1. Modify the weight of a grid,

2. Calculate the sum of consecutive lattice values,

3. Calculate the maximum value of a consecutive grid.

Output the result of each 2 or 3 operation.

Input Format

The first row has two integers, n and m.

The n integers in the next row represent the initial weights of n grids.

In the next m row, each row has three integers, p, x, y, and p, indicating the operation type. When p = 1, it indicates changing the weight of grid x to y, when p = 2, it indicates the sum of the grid weights in the range [x, y], and when p = 3, it indicates the maximum weight of the grid in the range [x, y.

Output Format

There are several rows. The number of rows equals the total number of operations with p = 2 or 3.

Each row has one integer, corresponding to the result of each p = 2 or 3 operation.

Example input 4 3
1 2 3 4
2 1 3
1 4 3
3 1 4 sample output 6
3. Data scale and conventions

For 20% of data, n <= 100, m <= 200.

For 50% of data, n <= 5000, m <= 5000.

For 100% of data 1 <= n <= 100000, m <= 100000,0 <= grid weight <= 10000.




# Include
              
               
# Include
               
                
Using namespace std; const int MAX = 100110; # define lson l, m, rt <1 # define rson m + 1, r, rt <1 | 1int sum [MAX <2], segs [MAX <2], maxv [MAX <2]; // interval and-interval value-Interval Maximum inline void push_up (int rt) {sum [rt] = sum [rt <1] + sum [rt <1 | 1]; maxv [rt] = max (maxv [rt <1], maxv [rt <1 | 1]);} void build_up (int l, int r, int rt) {if (l = r) {scanf ("% d ", & segs [rt]); sum [rt] = segs [rt]; maxv [rt] = segs [rt]; return;} int m = (l + r)> 1; build_up (lson); build_up (rson); push_up (rt);} void update (int l, int r, int rt, int p, int v) {if (l = r) {segs [rt] = v; maxv [rt] = v; sum [rt] = v; return ;} int m = (l + r)> 1; if (p <= m) update (lson, p, v); else update (rson, p, v ); push_up (rt);} int query_sum (int L, int R, int l, int r, int rt) {if (L <= l & r <= R) {return sum [rt];} int ret = 0; int m = (l + r)> 1; if (L <= m) ret + = query_sum (L, r, lson); if (R> m) ret + = query_sum (L, R, rson); return ret;} int query_max (int L, int R, int l, int r, int rt) {if (L <= l & r <= R) {return maxv [rt];} int ret =-1; int m = (l + r)> 1; if (L <= m) ret = max (ret, query_max (L, R, lson )); if (R> m) ret = max (ret, query_max (L, R, rson); return ret;} int main () {int n, m; cin> n> m; build_up (1, n, 1); while (m --) {int p, x, y; scanf ("% d", & p, & x, & y); if (p = 1) {update (1, n, 1, x, y);} else if (p = 2) {printf ("% d \ n", query_sum (x, y, 1, n, 1 ));} else {printf ("% d \ n", query_max (x, y, 1, n, 1) ;}} return 0 ;}
               
              



Question:

Doped geometric knowledge

Crane
Time Limit:2000 MS Memory Limit:65536 K
Total Submissions:3012 Accepted:806 Special Judge

Description

ACM has bought a new crane (crane -- je? Áb ). the crane consists of n segments of various lengths, connected by flexible joints. the end of the I-th segment is joined to the beginning of the I + 1-th one, for 1 ≤ I <n. the beginning of the first segment is fixed at point with coordinates (0, 0) and its end at point with coordinates (0, w), where w is the length of the first segment. all of the segments lie always in one plane, and the joints allow arbitrary rotation in that plane. after series of unpleasant accidents, it was decided that software that controls the crane must contain a piece of code that constantly checks the position of the end of crane, and stops the crane if a collision shoshould happen.

Your task is to write a part of this software that determines the position of the end of the n-th segment after each command. the state of the crane is determined by the angles between consecutive segments. initially, all of the angles are straight, I. e ., 180o. the operator issues commands that change the angle in exactly one joint.

Input

The input consists of several instances, separated by single empty lines.

The first line of each instance consists of two integers 1 ≤ n ≤ 10 000 and c 0 separated by a single space -- the number of segments of the crane and the number of commands. the second line consists of n integers l1 ,..., ln (1 li 100) separated by single spaces. the length of the I-th segment of the crane is li. the following c lines specify the commands of the operator. each line describing the command consists of two integers s and a (1 ≤ s <n, 0 ≤ a ≤359) separated by a single space -- the order to change the angle between the s-th and the s + 1-th segment to a degrees (the angle is measured counterclockwise from the s-th to the s + 1-th segment ).

Output

The output for each instance consists of c lines. the I-th of the lines consists of two rational numbers x and y separated by a single space -- the coordinates of the end of the n-th segment after the I-th command, rounded to two digits after the decimal point.

The outputs for each two consecutive instances must be separated by a single empty line.

Sample Input

2 110 51 903 25 5 51 2702 90

Sample Output

5.00 10.00-10.00 5.00-5.00 10.00


# Include
              
               
# Include
               
                
# Include
                
                 
Using namespace std; # define lson I <1 # define rson I <1 | 1 # define lc l, mid, I <1 # define rc mid + 1, r, I <1 | 1 const int L = 100000 + 10; const double pi = acos (-1.0); struct node {double x, y; int deg; int flag ;} a [L <2]; double set (int x) {return x * pi/180;} void work (int I, int deg) // obtain the new coordinate formula {double r = set (deg); double x = a [I]. x; double y = a [I]. y; a [I]. x = x * cos (r)-y * sin (r); a [I]. y = x * sin (r) + y * cos (r); a [I]. deg = (A [I]. deg + deg) % 360;} void pushup (int I) {a [I]. x = a [lson]. x + a [rson]. x; a [I]. y = a [lson]. y + a [rson]. y;} void pushdown (int I) {if (a [I]. flag) {work (lson, a [I]. flag); work (rson, a [I]. flag); a [lson]. flag + = a [I]. flag; a [rson]. flag + = a [I]. flag; a [I]. flag = 0 ;}} void init (int l, int r, int I) {a [I]. x = a [I]. y = 0; a [I]. flag = a [I]. deg = 0; if (l = r) {scanf ("% lf", & a [I]. y); return;} int mid = (l + r)> 1; init (l C); init (rc); pushup (I);} void insert (int l, int r, int I, int L, int R, int z) {if (L <= l & r <= R) {work (I, z); a [I]. flag + = z; return;} pushdown (I); int mid = (l + r)> 1; if (L <= mid) insert (lc, L, R, z); if (R> mid) insert (rc, L, R, z); pushup (I);} int query (int l, int r, int I, int x) {if (l = r) return a [I]. deg; pushdown (I); int mid = (l + r)> 1; if (x <= mid) return query (lc, x); else return query (rc, x);} int main () {int N, m, x, y, flag = 1, I, j; while (~ Scanf ("% d", & n, & m) {init (0, n-1, 1); if (! Flag) printf ("\ n"); flag = 0; while (m --) {scanf ("% d", & x, & y); int deg; deg = query (0, n-1, 1, x-1) + 180 + y-query (0, n-1, 1, x); // since the question is clockwise, this computation is clockwise and requires an additional 180 degrees. It regards the following rod as still on the Y axis. Therefore, we need to subtract the angle insert (0, n-1, 1, x, n-1, deg); printf ("%. 2f %. 2f \ n ", a [1]. x, a [1]. y) ;}} 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.