12: Challenge 5 (directly modify the line segment tree), challenge Line Segment

Source: Internet
Author: User

12: Challenge 5 (directly modify the line segment tree), challenge Line Segment

Total time limit:
10000 ms
 
Time limit for a single test point:
1000 ms
 
Memory limit:
262144kB
Description

For an N-length series with M operations, each operation is one of the following two types:

(1) Change a continuous segment to a number at the same time

(2) Calculate the sum of a continuous segment of a Series

Input
The first line has two integers, N and M.
The integer of N in the second row represents this series.
In the next M line, each line starts with one character. If the character is 'M', it indicates a modification operation. The next three integers x, y, and z are represented in [x, y] the number of this interval is changed to z. If the character is 'Q', it indicates a query operation. The next two integers x and y represent [x, y] the sum of the range.
Output
Output a single line for each query operation, indicating the answer.
Sample Input
5 31 2 3 4 5Q 1 5M 2 3 2Q 3 5
Sample output
1511
Prompt
1 <= N <= 10 ^ 5, 1 <= M <= 10 ^ 5, the input is valid, and All integers and answers can be stored with 32-bit integer characters.
Directly modify the line segment tree,
We should first consider maintaining a modification tag,
Note that this mark can be overwritten every time!
Then change the value range directly.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # define ls k <1 5 # define rs k <1 | 1 6 using namespace std; 7 const int MAXN = 100001; 8 const int maxn = 0x7ffff; 9 void read (int & n) 10 {11 char c = '+'; int x = 0; bool flag = 0; 12 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1;} 13 while (c> = '0' & c <= '9') 14 x = (x <1) + (x <3) + c-48, c = getchar (); 15 flag = 1? N =-x: n = x; 16} 17 int n, m; 18 int ans = 0; 19 struct node 20 {21 int l, r, w, f; 22 node () 23 {24 l = r = w = 0; 25 f =-maxn; 26} 27} tree [MAXN <2]; 28 void update (int k) 29 {30 tree [k]. w = tree [ls]. w + tree [rs]. w; 31} 32 void build (int ll, int rr, int k) 33 {34 tree [k]. l = ll; tree [k]. r = rr; 35 if (ll = rr) 36 {37 read (tree [k]. w); 38 return; 39} 40 int mid = (ll + rr)> 1; 41 build (ll, mid, ls); 42 build (mid + 1, rr, rs ); 43 update (k); 44} 45 void push (int k) 46 {47 tree [ls]. w = (tree [ls]. r-tree [ls]. l + 1) * tree [k]. f; 48 tree [rs]. w = (tree [rs]. r-tree [rs]. l + 1) * tree [k]. f; 49 tree [ls]. f = tree [k]. f; 50 tree [rs]. f = tree [k]. f; 51 tree [k]. f =-maxn; 52 53} 54 void change (int k, int wl, int wr, int v) 55 {56 if (wr <tree [k]. l | wl> tree [k]. r) 57 return; 58 if (wl <= tree [k]. l & tree [k]. r <= wr) 59 {60 tree [k]. w = (tree [k]. r-tree [k]. l + 1) * v; 61 Tree [k]. f = v; 62 return; 63} 64 int mid = (tree [k]. l + tree [k]. r)> 1; 65 if (tree [k]. f! =-Maxn) 66 push (k); 67 change (ls, wl, wr, v); 68 change (rs, wl, wr, v); 69 update (k ); 70} 71 void ask (int k, int wl, int wr) 72 {73 if (wr <tree [k]. l | wl> tree [k]. r) 74 return; 75 if (wl <= tree [k]. l & tree [k]. r <= wr) 76 {77 ans + = tree [k]. w; 78 return; 79} 80 int mid = (tree [k]. l + tree [k]. r)> 1; 81 if (tree [k]. f! =-Maxn) 82 push (k); 83 ask (ls, wl, wr); 84 ask (rs, wl, wr); 85 update (k ); 86} 87 int main () 88 {89 read (n); read (m); 90 build (1, n, 1); 91 for (int I = 1; I <= m; I ++) 92 {93 char c; int x, y; 94 cin> c; 95 read (x); read (y ); 96 if (c = 'M') 97 {98 int v; 99 read (v); 100 change (1, x, y, v ); 101} 102 else103 {104 ans = 0; 105 ask (1, x, y); 106 printf ("% d \ n", ans ); 107} 108} 109 return 0; 110} 111 12: Challenge 5 recent submission

 

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.