HDU 5634 线段树

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634

题意:给定一个长度为n的序列,有m次操作。操作有3种:

1 l,r :区间[l,r]的值变成phi[val[i]](l<=i<=r; phi是欧拉值)

2 l,r,x:区间[l,r]的值变成x

3 l,r:求区间[l,r]的和

思路:操作2和3就是传统的简单线段树,操作2对应区间覆盖,操作3对应区间求和,重点在于操作1,由于一个数经过不超过log次求phi后会变成1,所以可以在线段树是用一个same标记,如果整个区间的数都相同则操作1就转换成操作2的区间覆盖了。如果操作的区间[l,r]已经包含住当前递归的子树区间但是子树的same标记为假则继续递归到子树的same标记为真为止,最多递归到叶子结点。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
#define L(k)(k<<1)
#define R(k)(k<<1|1)
const LL INF = 9223372036854775807;
const int MAXN = 3e5 + 24;
const int MAXX = 1e7 + 24;
struct Node{
    int l, r, val;
    LL sum;
    bool same;
    Node(int _l = 0, int _r = 0, int _val = 0, LL _sum = 0, bool _same = false){
        l = _l; r = _r; sum = _sum; same = _same; val = _val;
    }
}Seg[MAXN * 4];
int val[MAXN], Phi[MAXX];
void pushUp(int k){
    Seg[k].sum = Seg[L(k)].sum + Seg[R(k)].sum;
    if (Seg[L(k)].val == Seg[R(k)].val&&Seg[L(k)].same&&Seg[R(k)].same&&Seg[L(k)].val != -1){
        Seg[k].same = true;
        Seg[k].val = Seg[L(k)].val;
    }
    else{
        Seg[k].same = false;
        Seg[k].val = -1;
    }
}
void pushDown(int k){
    if (Seg[k].same){
        Seg[L(k)].same = Seg[R(k)].same = true;
        Seg[L(k)].val = Seg[R(k)].val = Seg[k].val;
        Seg[L(k)].sum = 1LL * (Seg[L(k)].r - Seg[L(k)].l + 1)*Seg[L(k)].val;
        Seg[R(k)].sum = 1LL * (Seg[R(k)].r - Seg[R(k)].l + 1)*Seg[R(k)].val;
    }
}
void Build(int st, int ed, int k){
    Seg[k].l = st; Seg[k].r = ed; Seg[k].same = false;  Seg[k].val = -1;
    if (st == ed){
        Seg[k].val = val[st];
        Seg[k].sum = val[st];
        Seg[k].same = true;
        return;
    }
    int mid = (st + ed) >> 1;
    Build(st, mid, L(k)); Build(mid + 1, ed, R(k));
    pushUp(k);
}
void Change(int st, int ed, int val, int k){
    if (Seg[k].l == st&&Seg[k].r == ed){
        Seg[k].same = true;
        Seg[k].val = val;
        Seg[k].sum = 1LL * (ed - st + 1)*val;
        return;
    }
    pushDown(k);
    if (Seg[L(k)].r >= ed){
        Change(st, ed, val, L(k));
    }
    else if (Seg[R(k)].l <= st){
        Change(st, ed, val, R(k));
    }
    else{
        Change(st, Seg[L(k)].r, val, L(k));
        Change(Seg[R(k)].l, ed, val, R(k));
    }
    pushUp(k);
}
void Modify(int st, int ed, int k){
    if (Seg[k].l == st&&Seg[k].r == ed&&Seg[k].same){
        Seg[k].val = Phi[Seg[k].val];
        Seg[k].sum = 1LL * (ed - st + 1)*Seg[k].val;
        return;
    }
    pushDown(k);
    if (Seg[L(k)].r >= ed){
        Modify(st, ed, L(k));
    }
    else if (Seg[R(k)].l <= st){
        Modify(st, ed, R(k));
    }
    else{
        Modify(st, Seg[L(k)].r, L(k));
        Modify(Seg[R(k)].l, ed, R(k));
    }
    pushUp(k);
}
LL Query(int st, int ed, int k){
    if (Seg[k].l == st&&Seg[k].r == ed){
        return Seg[k].sum;
    }
    pushDown(k);
    if (Seg[L(k)].r >= ed){
        return Query(st, ed, L(k));
    }
    else if (Seg[R(k)].l <= st){
        return Query(st, ed, R(k));
    }
    else{
        return Query(st, Seg[L(k)].r, L(k)) + Query(Seg[R(k)].l, ed, R(k));
    }
    pushUp(k);
}
void GetPhi(){ //预处理欧拉值
    memset(Phi, 0, sizeof(Phi));
    Phi[1] = 1;
    for (LL i = 2; i < MAXX; i++){
        if (!Phi[i]){
            for (LL j = i; j < MAXX; j += i){
                if (!Phi[j]) Phi[j] = j;
                Phi[j] = Phi[j] / i*(i - 1);
            }
        }
    }
}
int main(){
//#ifdef kirito
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
//    int start = clock();
    int n, t, m; GetPhi();
    scanf("%d", &t);
    while (t--){
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++){
            scanf("%d", &val[i]);
        }
        Build(1, n, 1);
        for (int i = 1; i <= m; i++){
            int tpe, l, r, x;
            scanf("%d%d%d", &tpe, &l, &r);
            if (tpe == 1){
                Modify(l, r, 1);
            }
            else if (tpe == 2){
                scanf("%d", &x);
                Change(l, r, x, 1);
            }
            else{
                printf("%lld\n", Query(l, r, 1));
            }
        }
    }
//#ifdef LOCAL_TIME
//    cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
    return 0;
}
时间: 2024-10-21 15:46:44

HDU 5634 线段树的相关文章

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

HDU 4902 线段树(区间更新)

Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 353    Accepted Submission(s): 169 Problem Description There is an old country and the king fell in love with a devil. The devil alw

hdu 4893 线段树 --- 也是两个变 类似双标记

http://acm.hdu.edu.cn/showproblem.php?pid=4893 开始的时候,我按双标记,WA了一下午,搞不定,我是用的两个标记add--表示当前结点中有值发生变化,flag,斐波那契的懒惰标记,但是估计是我自己处理的有问题,一直不对 参考了别人的代码,写法还是很不错的,Add变量维护的是,完全变成Fibonacci的时候的和,---回头我再重新写一遍 #include <cstdio> #include <cstring> #include <a

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]

HDU 4302 线段树单点更新,维护区间最大最小值

http://acm.hdu.edu.cn/showproblem.php?pid=4302 Problem Description Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the p

HDU 3397 线段树 双懒惰标记

这个是去年遗留历史问题,之前思路混乱,搞了好多发都是WA,就没做了 自从上次做了大白书上那个双重懒惰标记的题目,做这个就思路很清晰了 跟上次大白上那个差不多,这个也是有一个sets标记,代表这个区间全部置为0或者1,没有置位的时候为-1 还有个rev标记,代表翻转操作,0代表当前不翻,1代表当前翻 要注意一下优先级,发现有不同的弄法,我是这个弄得,有set操作的时候,set标记设值,并把当前节点的rev标记设为0,因为不管要不要rev,当前set操作肯定直接覆盖了 rev操作不改变set操作,在

hdu 2795 线段树--点更新

http://acm.hdu.edu.cn/showproblem.php?pid=2795 多校的第一场和第三场都出现了线段树,比赛期间没做,,这两天先做几道热身下,然后31号之前把那两道多校的线段树都搞了,这是一道热身题 关键是建模: 首先一定看清楚题目构造的场景,看有什么特点--------会发现,如果尽量往左上放置的话,那么由于 the i-th announcement is a rectangle of size 1 * wi.,完全可以对h建立线段树,表示一行,结点里的l,r就表示

HDU 1698 线段树(区间染色)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16255    Accepted Submission(s): 8089 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU 3642 线段树+离散化+扫描线

题意:给你N个长方体的左下角和右上角坐标,问你空间中有多少体积是被大于两个不同的立方体覆盖的.x,y~10^6 z~500 考虑到给的z比较小,所以可以直接枚举z,然后跑二维的扫描线就好. 关于处理被不同的线段覆盖三次的问题,可以维护四个信息,cnt,once,twice,more,然后相互推出结果就好. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #