码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-02 02:00:14      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:ret   标记   cto   ase   def   sign   ifd   algo   数组   

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.\n",cas++,query(1,1,n));
    }
    return 0;
}

 

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

标签:ret   标记   cto   ase   def   sign   ifd   algo   数组   

原文地址:https://www.cnblogs.com/fht-litost/p/9572183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!