• hdu 1698 线段树 区间更新 区间求和


    Just a Hook

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 24474    Accepted Submission(s): 12194


    Problem 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.
     
    Source
     
    线段树 区间更新 区间求和
    理解这个博客
    要多刷
    http://www.douban.com/note/273509745/
    4.14重刷 更新模板  注意 pushdown的使用 有些题目 懒散标记不能堆积,得向下更新
     
      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdio>
      4 #include<queue>
      5 #include<stack>
      6 #define ll __int64
      7 #define maxn 100000
      8 using namespace std;
      9 struct node 
     10 {
     11     int l,r;
     12     int add;
     13     int sum;
     14 }tree[4*maxn];
     15 int t;
     16 int x,y,z;
     17 int n,q;
     18 int ans=0;
     19 void buildtree(int root,int left,int right)
     20 {
     21      tree[root].l=left;
     22      tree[root].r=right;
     23      tree[root].add=0;
     24      if(left==right)
     25      {
     26       tree[root].sum=1;
     27          return ;
     28      }
     29      int mid=(left+right)>>1;
     30      buildtree(root<<1,left,mid);
     31      buildtree(root<<1|1,mid+1,right);
     32      tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
     33 }
     34 void pushdown(int root,int m)
     35 {
     36    tree[root<<1].add=tree[root].add;
     37    tree[root<<1|1].add=tree[root].add;
     38    tree[root<<1].sum=tree[root].add*(m-(m>>1));
     39    tree[root<<1|1].sum=tree[root].add*(m>>1);
     40    tree[root].add=0;
     41 }
     42 void updata(int root,int left,int right,int c)
     43 {
     44     if(tree[root].l==left&&tree[root].r==right)
     45     {
     46         tree[root].add=c;
     47         tree[root].sum=c*(right-left+1);
     48         return ; 
     49     }
     50     if(tree[root].l==tree[root].r)
     51         return ;
     52     if(tree[root].add)
     53         pushdown(root,tree[root].r-tree[root].l+1);
     54     int mid=(tree[root].l+tree[root].r)>>1;
     55     if(right<=mid)
     56     updata(root<<1,left,right,c);
     57     else
     58     {
     59         if(left>mid)
     60             updata(root<<1|1,left,right,c);
     61         else
     62         {
     63             updata(root<<1,left,mid,c);
     64             updata(root<<1|1,mid+1,right,c);
     65         }
     66     }
     67     tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
     68 }
     69 int query(int root,int left,int right)
     70 {
     71     if(tree[root].l==left&&tree[root].r==right)
     72     {
     73         return tree[root].sum;
     74     }
     75        if(tree[root].add)
     76      pushdown(root,tree[root].r-tree[root].l+1);
     77      int mid=(tree[root].l+tree[root].r)>>1;
     78      if(right<=mid)
     79       ans+=query(root<<1,left,right);
     80       else
     81       {
     82           if(left>mid)
     83           ans+=query(root<<1|1,left,right);
     84           else
     85           {
     86               ans+=query(root<<1,left,mid);
     87             ans+=query(root<<1|1,mid+1,right); 
     88         } 
     89       }
     90       return ans;
     91 }
     92 int main()
     93 {
     94     while(scanf("%d",&t)!=EOF)
     95     {
     96         for(int i=1;i<=t;i++)
     97         {
     98             scanf("%d",&n);
     99             buildtree(1,1,n);
    100             scanf("%d",&q);
    101             for(int j=1;j<=q;j++)
    102             {
    103                 scanf("%d %d %d",&x,&y,&z);
    104                 updata(1,x,y,z);    
    105             }
    106             ans=0;
    107             printf("Case %d: The total value of the hook is %d.
    ",i,query(1,1,n));
    108         }
    109     }
    110     
    111     return 0;
    112 }

    旧板子

     
    #include<bits/stdc++.h>
    using namespace std;
    #define maxn 100005
    struct ss
    {
        int l,r;
        int sum;
        int tag;
    }tr[4*maxn];
    void build (int k,int s,int t)
    {
        tr[k].l=s;
        tr[k].r=t;
        tr[k].tag=0;
        if(s==t)
        {
         tr[k].sum=1;
         return ;
        }
        int mid=(s+t)>>1;
            build(k<<1,s,mid);
            build(k<<1|1,mid+1,t);
        tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
        //cout<<tr[k].sum<<endl;
    }
    void pushdown(int k,int m)//向下更新节点
    {
        tr[k<<1].tag=tr[k].tag;
        tr[k<<1|1].tag=tr[k].tag;
        tr[k<<1].sum=tr[k].tag*(m-(m>>1));
        tr[k<<1|1].sum=tr[k].tag*(m>>1);
        tr[k].tag=0;
    }
    void update (int k,int s,int t,int exm)
    {
        if(tr[k].l==s&&tr[k].r==t)
        {
            tr[k].tag=exm;
            tr[k].sum=exm*(t-s+1);
            return ;
        }
        if(tr[k].l==tr[k].r)
            return ;
        if(tr[k].tag)
            pushdown(k,tr[k].r-tr[k].l+1);
        int mid=(tr[k].l+tr[k].r)>>1;
        if(t<=mid)
            update(k<<1,s,t,exm);
        else if(s>mid)
            update(k<<1|1,s,t,exm);
        else
        {
            update(k<<1,s,mid,exm);
            update(k<<1|1,mid+1,t,exm);
        }
        tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
    }
    int ask(int k,int s,int t)
    {
        if(tr[k].l==s&&tr[k].r==t)
        {
            return tr[k].sum;
        }
        if(tr[k].tag) pushdown(k,tr[k].r-tr[k].l+1);
        int mid=(tr[k].l+tr[k].r)>>1;
        int ans=0;
        if(t<=mid)
            ans+= ask(k<<1,s,t);
        else
            if(s>mid)
            ans+=ask(k<<1|1,s,t);
        else
        {
           ans+=ask(k<<1,s,mid);
           ans+=ask(k<<1|1,mid+1,t);
        }
        return ans;
    }
    int t;
    int n;
    int m;
    int qq,ww,ee;
    //int a[maxn];
    int main()
    {
        while(scanf("%d",&t)!=EOF)
        {
            for(int i=1;i<=t;i++)
            {
    
                scanf("%d",&n);
                build(1,1,n);
                scanf("%d",&m);
                for(int j=1;j<=m;j++)
                {
                  scanf("%d%d%d",&qq,&ww,&ee);
                  update(1,qq,ww,ee);
                }
    printf("Case %d: The total value of the hook is %d.
    ",i,ask(1,1,n));
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    函数的存储 堆和栈
    函数的容错处理 函数的返回值
    Linux启动故障排查和修复技巧
    干货 | 亿级Web系统负载均衡几种实现方式
    利用expect批量修改Linux服务器密码
    干货 | LVM快照学习
    实战 | Linux根分区扩容
    LVM 逻辑卷学习
    Shell脚本实战:日志关键字监控+自动告警
    手把手教你在Linux中快速检测端口的 3 个小技巧
  • 原文地址:https://www.cnblogs.com/hsd-/p/5055780.html
Copyright © 2020-2023  润新知