HDU 4893 wow! Such sequence! (Line Segment tree function: Single Point update, Interval Update adjacent smaller Fibonacci numbers)

Source: Internet
Author: User

Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4893


----------------------------------------------------------------------------------------------------------------------------------------------------------
Welcome to tianci Hut: http://user.qzone.qq.com/593830943/main
 
----------------------------------------------------------------------------------------------------------------------------------------------------------


Problem descriptionrecently, Doge got a funny birthday present from his new friend, protein tiger from st. Beeze college. No, not cactuses. It's a mysterious blackbox.

After some research, Doge found that the box is maintaining a sequence an of N numbers internally, initially all numbers are zero, and there are three "operations ":

1. Add D to the k-th number of the sequence.
2. query the sum of AI where L ≤ I ≤ r.
3. Change AI to the nearest Fibonacci number, where L ≤ I ≤ r.
4. Play Sound "Chee-Rio! ", A bit useless.

Let f0 = 1, F1 = 1, Maid number FN is defined as fn = FN-1 + FN-2 for n ≥ 2.
Nearest Maid number of number x means the smallest FN where | fn-x | is also smallest.

Doge doesn' t believe the machine cocould respond each request in less than 10 ms. Help Doge figure out the reason. inputinput contains several test cases, please process till EOF.
For each test case, there will be one line containing two integers n, m.
Next m lines, each line indicates a query:

1 K d-"add"
2 l r-"query sum"
3 l r-"change to nearest maid"

1 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, | d | <231, all queries will be valid. outputfor each type 2 ("query sum") operation, output one line containing an integer represent the answer of this query. sample Input
1 12 1 15 41 1 71 3 173 2 42 1 5
Sample output
022
Authorfudan University source2014 multi-university training contest 3 recommendwe have carefully selected several similar problems for you: 4896 4895 4894 4892 4891


An array with a length of N is initialized to 0. For the next M operation, there are three types of operations: 1 a B: add the element at position a in the array to B2 a B to calculate the sum of [a, B] elements in the array. 3 a B, b] The element in it becomes the closest Fibonacci number to the element (if the two Fibonacci numbers are the same as the absolute values of the difference between the element and the element, take the smallest Fibonacci number)
Idea: If three operations have been performed on a segment, the segment can be ignored when three operations are performed again. If three operations have been performed on a segment and one operation has been performed on several elements, you only need to perform one operation on these elements to change the Fibonacci number.
The Code is as follows:
# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # include <set> using namespace STD; # define ll _ int64 # define lson l, mid, RT <1 # define rson Mid + 1, R, RT <1 | 1 # define maxn 1111ll num [maxn]; ll sum [maxn <2]; // sum ll add [maxn <2]; int se [maxn <2]; ll f [111], X; void pushup (int rt) {sum [RT] = sum [RT <1] + sum [RT <1 | 1]; add [RT] = add [RT <1] + Add [RT <1 | 1];} void build (int l, int R, int RT) {sum [RT] = 0; se [RT] =-1; if (L = r) {Add [RT] = 1; return ;} int mid = (L + r)> 1; build (lson); Build (rson); pushup (RT);} void Pushdown (int rt, int L, int R) {If (SE [RT]! =-1) {sum [RT <1] + = add [RT <1]; sum [RT <1 | 1] + = add [RT <1 | 1]; add [RT <1] = add [RT <1 | 1] = 0; se [RT <1] = Se [RT <1 | 1] = 1; se [RT] =-1 ;}} void Update (int l, int R, ll C, int L, int R, int RT) {If (L <= L & R <= r) {sum [RT] + = C; int x = (INT) (lower_bound (F, F + 77, sum [RT])-f); // find the corresponding Fibonacci number if (x = 0) {Add [RT] = f [0]-sum [RT];} else {If (F [x] = sum [RT]) {return ;} else if (sum [RT]-f [x-1] <= f [x]-sum [RT]) // closer {Add [RT] = f [x-1]-sum [RT];} else add [RT] = F [x]-sum [RT];} return;} Pushdown (RT, L, R); int mid = (L + r)> 1; if (L <= mid) Update (L, R, C, lson); If (mid <r) Update (L, R, C, rson ); pushup (RT);} void ins (int l, int R, int L, int R, int RT) {If (L <= L & R <= r) {sum [RT] + = add [RT]; add [RT] = 0; se [RT] = 1; return;} Pushdown (RT, L, R ); int mid = (L + r)> 1; if (L <= mid) ins (L, R, lson); If (mid <r) ins (L, R, R, rson); pushup (RT);} ll query (int l, int R, int L, int R, int RT) {If (L <= L & R <= r) {Return sum [RT];} Pushdown (RT, L, R); LL res = 0; int mid = (L + r)> 1; if (L <= mid) RES + = query (L, R, lson); If (mid <r) RES + = query (L, R, rson ); return res;} int main () {int n, m; F [0] = 1; F [1] = 1; for (INT I = 2; I <= 77; + + I) f [I] = f [I-1] + F [I-2]; while (~ Scanf ("% d", & N, & M) {build (1, n, 1); int op, a, B; For (INT I = 1; I <= m; I ++) {scanf ("% d", & OP, & A, & B); If (OP = 1) {Update (a, a, B, 1, n, 1); // the single point increase interval is a-> A;} else if (OP = 2) {printf ("% i64d \ n", query (A, B, 1, n, 1);} else if (OP = 3) {ins (A, B, 1, 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.