• hdu1698线段树


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MAX=100000;
    int svalue;
    struct node
    {
     int left,right,cover;
    };
    node tree[MAX*3];
    bool used[31];
    void built(int L,int R,int id)//建树
    {
     tree[id].cover=1;//初始化
     tree[id].left=L;
     tree[id].right=R;
     if(L<R)
     {
      int mid=(L+R)/2;
      built(L,mid,2*id); //左子树
      built(mid+1,R,2*id+1); //右子树
     }
    }
    void insert(int id,int L,int R,int col)
    {
     if(tree[id].left>=L&&tree[id].right<=R)
     {
      tree[id].cover=col;
      return;
     }
     if(tree[id].left<tree[id].right)
     {
      int mid=(tree[id].left+tree[id].right)>>1;
      if(tree[id].cover>0)
      {
       tree[id*2].cover=tree[id].cover;
       tree[id*2+1].cover=tree[id].cover;
      }
      tree[id].cover=0;
      if(R<=mid)
       insert(id*2,L,R,col);
      else if(L>mid)
       insert(id*2+1,L,R,col);
      else
      {
       insert(id*2,L,mid,col);
       insert(id*2+1,mid+1,R,col);
      }
     }
    }
    void cnt(int L,int R,int id)
    {
     if(tree[id].cover>0)
     {
      svalue+=tree[id].cover*(tree[id].right-tree[id].left+1);
      return;
     }
     if(tree[id].left<tree[id].right)
     {
      int mid=(tree[id].left+tree[id].right)>>1;
      if(R<=mid)
       cnt(L,R,id*2);
      else if(L>mid)
       cnt(L,R,id*2+1);
      else
      {
       cnt(L,mid,id*2);
       cnt(mid+1,R,id*2+1);
      }
     }
    }
    int main()
    {
     int cases,N,Q;
     int i,l,r,z;
     int num=1;
     cin>>cases;
     while(cases--)
     {
      scanf("%d%d",&N,&Q);
      built(1,N,1);
      for (i=1;i<=Q;i++)
      {
       scanf("%d %d %d",&l,&r,&z);
       insert(1,l,r,z);
      }
      svalue=0;  
      cnt(1,N,1);
      printf("Case %d: The total value of the hook is %d.\n",num++,svalue);
     }
     return 0;
    }

  • 相关阅读:
    ecos 编译时无法找到 tclConfig.sh 和 tkConfig.sh
    gcc 的宏替换 __stringify
    CentOS 静态IP配置
    光照
    CUnit 安装
    git push not configured with USE_CURL_MULTI
    在VC中用FreeImage显示图片的简单方法
    vanilla kernel
    Eclipse CDT 对 Doxygen 型注释的支持
    己所不欲,人欲取之
  • 原文地址:https://www.cnblogs.com/qijinbiao/p/2182798.html
Copyright © 2020-2023  润新知