HDU 1698 Just a Hook 线状树经典模型之区间变动bool标记,上下同时更新

    Just a Hook
Crawling in process... Crawling failed Time Limit:2000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

Description

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.




Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
 

Input

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
 

Output

For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
 

Sample Input

     
     
1 10 2 1 5 2 5 9 3
 

Sample Output

     
     
Case 1: The total value of the hook is 24.

记得题意是如果一个区间值全部相等,那么权就是这个区间的长度乘以这个值,最开始全都是1

用一个sta记录这个区间是不是所值都相等然后做

>>AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
const int MAXN = 100010;
const int INF = 0x3f3f3f3f;
struct node
{
    int l,r;
    bool sta;
    int val;//没有sum,后面是直接return val*区间长度
    bool flag;
}tree[MAXN * 4];
int L;

void build(int id,int l,int r)
{
    tree[id].l = l;
    tree[id].r = r;
    tree[id].val = 1;
    tree[id].flag = false;//flag表示要不要对它进行操作,刚开始不用所以为false;
    tree[id].sta = true;//刚开始每个区间都相同,所以是true;
    if (l == r) return;//与之前l==r才进行操作不同,这里是要给所有的树标,所以当无法再分时return;
    int mid = (l + r) / 2;
    build(id * 2,l,mid);
    build(id * 2 + 1,mid + 1,r);
}

void push_down(int id)//只需要满足一个条件
{
    if (tree[id].flag)//需要对该树进行操作(更新)
    {
        tree[id * 2].val = tree[id * 2 + 1].val = tree[id].val;
        tree[id * 2].flag = true;//需要对子树继续操作(更新)
        tree[id * 2 + 1].flag = true;
        tree[id].flag = false;//父树已操作结束
        tree[id].sta = true;
        tree[id * 2].sta = true;
        tree[id * 2 + 1].sta = true;//既然要对id父树进行操作,那就说明该区间的值都要换成一个特定值,所以都相同,为true;
    }
}


void push_up(int id)//需要满足两个条件↓
{
    if (tree[id * 2].sta == true && tree[id * 2 + 1].sta == true)//当子树区间值都相同时,父树单值对应要更新为相同值;
    {
        if (tree[id * 2].val == tree[id * 2 + 1].val)//再次判断,防止刚开始的默认情况误更新
        {
            tree[id].val = tree[id * 2 + 1].val;
            tree[id].sta = true;//父区间也为相同true;
        }
        else tree[id].sta = false;//不满足条件false;
    }
    else tree[id].sta = false;
}

void update(int id,int l,int r,int val)//特别要注意的是需要先push_down再push_up
{
    if (tree[id].l >= l && tree[id].r <= r)//要操作的区间大于等于该树的区间
    {
        tree[id].flag = true;//要对该树进行操作
        tree[id].val = val;
        tree[id].sta = true;
        return;
    }
    push_down(id);//从父到子更新;
    int mid = (tree[id].l + tree[id].r) / 2;
    if (l > mid) update(id * 2 + 1,l,r,val);
    else if (r <= mid) update(id * 2,l,r,val);
    else
    {
        update(id * 2,l,mid,val);
        update(id * 2 + 1,mid + 1,r,val);
    }
    push_up(id);//从子到父再更新,确保无遗漏,或者说刚开始的push_down(id)只是对相邻的子树进行更新,然后一直dfs二分到最后,
                //所以最后一定要从末尾到前面再更新一次(有些父树没更新)。
}

int query(int id,int l,int r)
{
    if (tree[id].l >= l && tree[id].r <= r)  
    {
        if (tree[id].sta == true)
        {
           // printf("%d %d %d\n",tree[id].l,tree[id].r,tree[id].val);
            return tree[id].val * (tree[id].r - tree[id].l + 1);
        }
    }
    push_down(id); //看需不需要对id父树进行操作,也就是说可能有一些子树还没更新到,确保再二分时已经更新完毕。
    int mid = (tree[id].l + tree[id].r) / 2;
    if (l > mid) return query(id * 2 + 1,l,r);
    else if (r <= mid) return query(id * 2,l,r);
    else
    {
        return query(id * 2,l,mid) + query(id * 2 + 1,mid + 1,r);
    }
}

int main()
{
    int T,kase = 1;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&L);
        build(1,1,L);
        int Q;
        scanf("%d",&Q);
        while (Q--)
        {
            int l,r,val;
            scanf("%d%d%d",&l,&r,&val);
            update(1,l,r,val);
            //printf("%d %d %d\n",l,r,val);
        }
        printf("Case %d: The total value of the hook is %d.\n",kase++,query(1,1,L));
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值