HDU 4902 Nice boat (line segment tree), hdu4902

Source: Internet
Author: User

HDU 4902 Nice boat (line segment tree), hdu4902
HDU Nice boat

Question Link

Question: Given a sequence, two operations are used to convert a segment into x, and each digit in a segment. If it is greater than x, it is converted into a gcd of him and x. After the change, the final sequence.

Idea: Line Segment tree. Each node has one more cover, which indicates whether the numbers in the following ranges of the position are the same. Then, each delay operation can be performed and the single point query can be performed at the end of the output.

Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100005;#define lson(x) ((x<<1)+1)#define rson(x) ((x<<1)+2)#define INF 0x3f3f3f3fint t, n, num[N];struct Node {    int l, r, x, setv;    bool cover;} node[4 * N];int gcd(int a, int b) {    if (!b) return a;    return gcd(b, a % b);}void pushup(int x) {    node[x].cover = ((node[lson(x)].x == node[rson(x)].x) && node[lson(x)].cover && node[rson(x)].cover);    node[x].x = node[lson(x)].x;}void build(int l, int r, int x = 0) {    node[x].l = l; node[x].r = r;    node[x].setv = -1;    node[x].cover = false;    if (l == r) {node[x].cover = true;node[x].x = num[l];return;    }    int mid = (l + r) / 2;    build(l, mid, lson(x));    build(mid + 1, r, rson(x));    pushup(x);}void pushdown(int x) {    if (node[x].setv != -1) {node[lson(x)].setv = node[rson(x)].setv = node[x].setv;node[lson(x)].x = node[rson(x)].x = node[x].setv;node[x].setv = -1;    }}void add1(int l, int r, int v, int x = 0) {    if (node[x].l >= l && node[x].r <= r) {node[x].setv = v;node[x].x = v;return;    }    int mid = (node[x].l + node[x].r) / 2;    pushdown(x);    if (l <= mid) add1(l, r, v, lson(x));    if (r > mid) add1(l, r, v, rson(x));    pushup(x);}void add2(int l, int r, int v, int x = 0) {    if (node[x].cover && node[x].x <= v) return;    if (node[x].l >= l && node[x].r <= r && node[x].cover) {node[x].x = gcd(node[x].x, v);node[x].setv = node[x].x;return;    }    pushdown(x);    int mid = (node[x].l + node[x].r) / 2;    if (l <= mid) add2(l, r, v, lson(x));    if (r > mid) add2(l, r, v, rson(x));    pushup(x);}int query(int k, int x = 0) {    if (node[x].l == node[x].r)return node[x].x;    int mid = (node[x].l + node[x].r) / 2;    pushdown(x);    if (k <= mid) return query(k, lson(x));    if (k > mid) return query(k, rson(x));}int main() {    scanf("%d", &t);    while (t--) {scanf("%d", &n);for (int i = 1; i <= n; i++)    scanf("%d", &num[i]);build(1, n);int q;scanf("%d", &q);int c, a, b, v;while (q--) {    scanf("%d%d%d%d", &c, &a, &b, &v);    if (c == 1)add1(a, b, v);    else if (c == 2)add2(a, b, v);}for (int i = 1; i <= n; i++)    printf("%d ", query(i));printf("\n");    }    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.