HDU - 1698 Just a Hook (线段树区间修改)

Posted fht-litost

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 1698 Just a Hook (线段树区间修改)相关的知识,希望对你有一定的参考价值。

https://cn.vjudge.net/problem/HDU-1698

题意

大小为n的数组,数组元素初始值为1,有q次操作,x,y,z表示从第x到第y所有的元素的值变为z,最后问1到n的和。

分析

区间修改,给每个区间打标记。注意这里是直接把整个区间都变为某个数。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 100000 + 10;
const int maxm = 200000 + 10;
const int mod = 998244353;
int n;
struct ND{
    int l,r;
    ll sum,lazy;
}tree[maxn<<2];
int a[maxn];
void pushup(int rt){
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void pushdown(int rt,int len){
    if(tree[rt].lazy){
        tree[rt<<1].lazy=tree[rt].lazy;
        tree[rt<<1|1].lazy=tree[rt].lazy;
        tree[rt<<1].sum=tree[rt].lazy*(len-(len>>1));
        tree[rt<<1|1].sum=tree[rt].lazy*(len>>1);
        tree[rt].lazy=0;
    }
}
void build(int rt,int l,int r){
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].lazy=0;
    if(l==r){
        tree[rt].sum=1;
        return;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    pushup(rt);
}
void update(int rt,int L,int R,int val){
    if(L<=tree[rt].l&&tree[rt].r<=R){
        tree[rt].lazy=val;
        tree[rt].sum=1ll*val*(tree[rt].r-tree[rt].l+1);
        return;
    }
    pushdown(rt,tree[rt].r-tree[rt].l+1);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid>=L) update(rt<<1,L,R,val);
    if(mid<R) update(rt<<1|1,L,R,val);
    pushup(rt);
}
ll query(int rt,int L,int R){
    if(L<=tree[rt].l&&tree[rt].r<=R) return tree[rt].sum;
    pushdown(rt,tree[rt].r-tree[rt].l+1);
    ll sum=0;
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid<R) sum+=query(rt<<1|1,L,R);
    if(mid>=L) sum+=query(rt<<1,L,R);
    return sum;
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    int t;
    int cas=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
//        for(int i=1;i<=n;i++) a[i]=1;
        build(1,1,n);
        int q;
        scanf("%d",&q);
        while(q--){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            update(1,x,y,z);
        }
        printf("Case %d: The total value of the hook is %lld.
",cas++,query(1,1,n));
    }
    return 0;
}

 

以上是关于HDU - 1698 Just a Hook (线段树区间修改)的主要内容,如果未能解决你的问题,请参考以下文章

Just a Hook HDU - 1698

HDU - 1698 Just a Hook

HDU - 1698 Just a Hook

Just a Hook HDU - 1698

HDU 1698 Just a Hook (线段树)

Just a Hook HDU - 1698 (区间修改+区间查询)