Hdu 4893 Wow! Such Sequence! (Line Segment tree), hdu4893

Source: Internet
Author: User

Hdu 4893 Wow! Such Sequence! (Line Segment tree), hdu4893

Link: hdu 4983 Wow! Such Sequence!

There are three operations.
1 k d. Change k to increase d.
2 l r: query the range from l to r and
3 l r, the numbers between l and r are changed to the nearest Fibonacci number. If they are equal, the values are taken.

Solution: Line Segment tree. If a bool is added for each node, it indicates whether the positions below the node are all Fibonacci numbers.

#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#define lson(x) ((x<<1))#define rson(x) ((x<<1)|1)using namespace std;typedef __int64 ll;const int maxn = 100005;const int maxf = 100;const ll INF = 2000000000000000LL;int n, m;ll fib[maxf], fn, num[maxn];struct maxnode {    int l, r;    ll sum;    bool isfib;    void set (int l, int r, ll sum, bool isfib) {        this->l = l;        this->r = r;        this->sum = sum;        this->isfib = isfib;    }} node[4 * maxn];void init () {    fib[0] = fib[1] = 1;    for (fn = 2;; fn++) {        fib[fn] = fib[fn - 2] + fib[fn - 1];        if (fib[fn] > INF)            break;    } }void pushup(int x) {    int l = lson(x), r = rson(x);    node[x].isfib = (node[l].isfib && node[r].isfib);    node[x].sum = node[l].sum + node[r].sum;;}void build(int l, int r, int x) {    node[x].set(l, r, 0, false);    if (l == r)        return;    int mid = (l + r) / 2;    build(l, mid, lson(x));    build(mid + 1, r, rson(x));}ll find (ll x) {    int id;    ll ans = INF;    for (int i = 0; i < fn; i++) {        ll k = (fib[i] > x ? fib[i] - x : x - fib[i]);        if (k < ans) {            ans = k;            id = i;        }    }    return fib[id];}void add (int k, ll v, int x) {    if (node[x].l == k && node[x].r == k) {        node[x].sum += v;        node[x].isfib = (find(node[x].sum) == node[x].sum ? true : false);        return;    }    int mid = (node[x].l + node[x].r) / 2;    if (k <= mid)        add(k, v, lson(x));    else if (k > mid)        add(k, v, rson(x));    pushup(x);}void insert(int l, int r, int x) {    if (node[x].isfib)        return;    if (node[x].l == node[x].r) {        node[x].sum = find(node[x].sum);        node[x].isfib = true;        return;    }    int mid = (node[x].l + node[x].r) / 2;    if (l <= mid)        insert(l, r, lson(x));    if (r > mid)        insert(l, r, rson(x));    pushup(x);}ll query(int l, int r, int x) {    if (node[x].l >= l && node[x].r <= r)        return node[x].sum;    int mid = (node[x].l + node[x].r) / 2;    ll ans = 0;    if (l <= mid)        ans += query(l, r, lson(x));    if (r > mid)        ans += query(l, r, rson(x));    return ans;}int main() {    init();    while (scanf("%d%d", &n, &m) == 2) {        build(1, n, 1);        int Q, a, b;        ll v;        while (m--) {            scanf("%d", &Q);            if (Q == 1) {                scanf("%d%I64d", &a, &v);                add(a, v, 1);            } else if (Q == 2) {                scanf("%d%d", &a, &b);                printf("%I64d\n", query(a, b, 1));            } else {                scanf("%d%d", &a, &b);                insert(a, b, 1);            }        }    }    return 0;}


Change of hdu1166 baseline pascal Line Segment tree

Line Segment tree
, Line segment tree Definition
Informatics competitions often encounter problems related to interval operations. When calculating the extreme values and sum of several segments, special data structures of line segments are used.
Line Segment tree binary tree T (AB ). parameter a and parameter B indicate the length of the interval [a, B] and its B-a are called the Interval Length. The line segment tree T (a, B) is also recursively defined:
If L> 1: [a, (a + B) div 2] T left son;
[(A + B) div 2, B] T right son
If L = l: T leaf node
Process object line segments or ranges. Set the range to [1 .. n] divide some [I, I + 1] Each unit area corresponds to a line segment tree leaf node. Each node uses variables to record the number of line segments that overwrite the node. Use a line segment tree to know that the endpoint of the specified interval can take values, set A [1 .. n] For any undefined closed range in the same endpoint set in a small large permutation area P = [xy]. save l ≤ I ≤ j ≤ n so that x = a [I] and y = a [j], Li I, j is called x, y number note: even if the real coordinate line segment tree has only integer meanings, the ranges [x, y] x and y are all integers. that is, the coordinate number of the vertex in the original interval.
Generally, the line segment Tree node type is defined as follows:
Type linetree = ^ node
Node = record
I, j: integer; {node indicates the vertex ID of the partition question I. j}
Count: integer; {Number of overwriting node zones}
Lchild, rchild: linetree; {two subnodes of a binary tree}
End;
Figure 6-22 typical line segment tree description interval endpoint has 10 values

Ii. Basic operations and Applications of line tree
The line segment tree has some important features. line Segment tree Balance Tree. the depth exceeds log2L. The second line segment tree divides any line segment in the area question into lines that exceed 2log2L. The line segment tree energy O (log2L) the following describes how to perform operations on a line segment tree when inserting, deleting, or searching a line segment within the specified time.
[Example 6-14] Ticket Sales System
[Problem description]
A certain train passes through city c. there are a total of s seats on the 1c train on the city no. the Railway Bureau stipulates that tickets sold can only be seated. that is, all passengers on the bus have a ticket sales system run by a computer. each ticket request contains three parameters. O and DN are used to represent the origin site D. N ticket count ticketing system accepts or accepts the ticket application. the ticket application is accepted only when there are N points N seats on the train in the OD section. Please write a program to implement automatic ticketing.
Input:
Input File: RAILWAY. IN, the first line contains three spaces to separate integers c, s, and R. its l ≤ c ≤ 600001 ≤ s ≤ 600001 ≤ R ≤ 60000c city number of s trains on the seat number R all ticket tickets application total number next R lines each ticket application. use three spaces to separate the integer OD and N to represent the start station of O. d. Visual site. N number of ticket stations, its l ≤ D ≤ cl ≤ O ≤ c. all ticket sales applications are provided from morning and evening based on the application time
Output:
Output file: RAlLWAY. OUT. There are a total of R rows. Each row outputs YES or NO, indicating that the current ticket sales application is accepted or accepted.
[Example input] RAIlWAY. IN
4 6 4
L 4 2
I 3 2
2 4 3
1 2 3
[Example output] RAILWAY. OUT
YES
YES
NO
NO
[Problem Analysis]
In this case, when the number of online statistics and search questions-interval insertion and search questions is relatively small, it is easy to simply simulate the scale of this question by 60,000. simple quasi-complexity O (n2) 60,000x60,000 = 3,600,000,000 The amount of computation cannot bear. Therefore, we must use special data structures and algorithms to reduce the time complexity.
Interval insertion and searching make it easy for us to think of line segments (intervals) of data structures. The time complexity of inserting and searching a line segment tree is O (log2n) only this topic is special. To search for segments, you must design a line segment tree with the function of searching for segments.
Let's design a line segment tree.
First. as a line segment tree. the number of times each record interval on the tree is overwritten is required. in this question, we need to record the two values of the period in each interval. except for the two values. we can also see that we need to record other information. We may as well, the shader line segment tree only records two values.
Maintenance Operation key record value for line segment tree maintenance refers to the insertion and search process. Maintenance complexity within the log2n level ...... the remaining full text>

The problem "acm1199" in HDU is always WA. It is estimated that the array range is not enough. How many questions should be opened in the array? All the input are less than 2 ^ 31-1

The data range is 2 ^ 31-1. But obviously we cannot open such a large array ...... Let's move back to Step 1 and say that even if we can open such a large array, we can use the brute force algorithm to estimate the time complexity of this O (10 thousand * 2 ^ 31) for an hour ......

If you are a beginner in acm, or if you do not want to engage in acm, you just want to practice some questions to improve the Code level, or if you are not proficient in writing binary trees:
Skip this question.

If you have some code capabilities and want to seriously engage in acm, this question is one of the necessary data structures: line tree. Please search for the knowledge about the Baidu line segment tree. In addition, because the data range is too large, it involves data discretization knowledge. We recommend that you first find a question about the common line segment tree without discretization. After that, you can start this question.

If you have any questions, please contact us.

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.