【HDU】4092 Nice boat ——线段树 懒惰标记

Nice boat

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 146    Accepted Submission(s): 75


Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

Let us continue our story, z*p(actually you) defeat the 'MengMengDa' party's leader, and the 'MengMengDa' party dissolved. z*p becomes the most famous guy among the princess's knight party. 

One day, the people in the party find that z*p has died. As what he has done in the past, people just say 'Oh, what a nice boat' and don't care about why he died.

Since then, many people died but no one knows why and everyone is fine about that. Meanwhile, the devil sends her knight to challenge you with Algorithm contest.

There is a hard data structure problem in the contest:

There are n numbers a_1,a_2,...,a_n on a line, everytime you can change every number in a segment [l,r] into a number x(type 1), or change every number a_i in a segment [l,r] which is bigger than x to gcd(a_i,x) (type 2).

You should output the final sequence.
 

Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a integers n.
The next line contains n integers a_1,a_2,...,a_n separated by a single space.
The next line contains an integer Q, denoting the number of the operations.
The next Q line contains 4 integers t,l,r,x. t denotes the operation type.

T<=2,n,Q<=100000
a_i,x >=0
a_i,x is in the range of int32(C++)
 

Output
For each test case, output a line with n integers separated by a single space representing the final sequence.
Please output a single more space after end of the sequence
 

Sample Input
  
  
1 10 16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709 10 1 3 6 74243042 2 4 8 16531729 1 3 4 1474833169 2 1 8 1131570933 2 7 9 1505795335 2 3 7 101929267 1 4 10 1624379149 2 2 8 2110010672 2 6 7 156091745 1 2 5 937186357
 

Sample Output
  
  
16807 937186357 937186357 937186357 937186357 1 1 1624379149 1624379149 1624379149
 

Author
WJMZBMR
 

题意:给定一个数字序列,对其进行两种操作:1、将序列中连续的一段变成一样的数x;2、给序列中连续的一段和一个数x,对于这个连续一段中的每个数,如果小于x则取x与这个数的最大公约数,否则不操作。


题解:线段树+懒人标记


#include <cstdio>
#include <cstring>

const int LEN = 400040;

struct line
{
    int left;
    int right;
    int value;
}line[LEN*2];
int point[LEN*2]; //记录线段树底层节点的位置

inline int gcd(int a,int b)  //求最大公约数
{
    return a % b ? gcd(b, a % b) : b;
}

void buildt(int left, int right, int step) //建树同时初始化懒人标记
{
    line[step].value = -1;
    line[step].left = left;
    line[step].right = right;
    if (left == right){
        point[left] = step;
        return;
    }
    int mid = (left + right) / 2;
    buildt(left, mid, step*2);
    buildt(mid+1, right, step*2+1);
}

void query_1(int left, int right, int x, int step)  //操作一
{
    if (left == line[step].left && right == line[step].right){
            line[step].value = x;
            return;
    }
    if (line[step].value != -1){
        line[step*2].value = line[step*2+1].value = line[step].value;
        line[step].value = -1;
    }
    int mid = (line[step].left + line[step].right) / 2;
    if (right <= mid)
        query_1(left, right, x, step*2);
    else if (left > mid)
        query_1(left, right, x, step*2+1);
    else{
        query_1(left, mid, x, step*2);
        query_1(mid+1, right, x, step*2+1);
    }
}

void query_2(int left, int right, int x, int step) //操作函数二
{
    if (line[step].left == left && line[step].right == right){ 
        if (line[step].value != -1){    //如果找到对应区间,且有标记,则进行条件判断,否则继续
            if (line[step].value > x)
                line[step].value = gcd(line[step].value, x);
            return;
        }
    }
    if (line[step].value != -1){  //如果改点被标记,则下移一层
        line[step*2].value = line[step*2+1].value = line[step].value;
        line[step].value = -1;
    }
    int mid = (line[step].left + line[step].right) / 2;
    if (right <= mid)
        query_2(left, right, x, step*2);
    else if (left > mid)
        query_2(left, right, x, step*2+1);
    else{
        query_2(left, mid, x, step*2);
        query_2(mid+1, right, x, step*2+1);
    }
}

void findans(int left, int right, int step)  //dfs输出结果
{
    if (line[step].left == left && line[step].right == right){
        if (line[step].value != -1){
            for(int i = 0; i < line[step].right - line[step].left + 1; i++)
                printf("%d ", line[step].value);
            return;
        }
    }
    if (line[step].value != -1){
        line[step*2].value = line[step*2+1].value = line[step].value;
        line[step].value = -1;
    }
    int mid = (line[step].left + line[step].right) / 2;
    if (right <= mid)
        findans(left, right, step*2);
    else if (left > mid)
        findans(left, right, step*2+1);
    else{
        findans(left, mid, step*2);
        findans(mid+1, right, step*2+1);
    }
}

int main()
{
    int T;
    //freopen("in.txt", "r", stdin);
    scanf("%d", &T);
    while(T--){
        int n;
        memset(point, 0, sizeof(point));
        scanf("%d", &n);
        buildt(1, n, 1);
        for(int i = 1; i <= n; i++){
            int t;
            scanf("%d", &t);
            line[point[i]].value = t;
        }
        int m;
        scanf("%d", &m);
        for(int i = 0; i < m; i++){
            int t, l, r, x;
            scanf("%d %d %d %d", &t, &l, &r, &x);
            if (t == 1){
                query_1(l, r, x, 1);
            }
            else if (t == 2){
                query_2(l, r, x, 1);
            }
        }
        findans(1, n, 1);
        printf("\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值