zoukankan      html  css  js  c++  java
  • HDU 1698 Just a Hook

    题目地址

    线段树

    这题看到第一句和插图宝宝我就惊呆了23333

      1 #include<cstdio>
      2 #include<algorithm>
      3 using namespace std;
      4 const int Nmax=1e5+10;
      5 struct Node
      6 {
      7     int l;
      8     int r;
      9     int data;
     10     //int lazy;
     11 };
     12 Node tree[Nmax*4];
     13 int t,n,q,x,y,z,ans;
     14 
     15 void init()
     16 {
     17     for(int i=0;i<Nmax;i++)
     18         tree[i].data=tree[i].l=tree[i].r=0;
     19 }
     20 
     21 void build(int root,int l,int r)
     22 {
     23     tree[root].l=l;
     24     tree[root].r=r;
     25     if(l==r)
     26     {
     27         tree[root].data=1;
     28         return;
     29     }
     30     int ls=root<<1,rs=root<<1|1,mid=(l+r)>>1;
     31     tree[root].data=1;
     32     build(ls,l,mid);
     33     build(rs,mid+1,r);
     34 }
     35 
     36 void update(int root,int l,int r,int data)
     37 {
     38     if(tree[root].data==data)
     39         return;
     40     if(tree[root].l==l && tree[root].r==r)
     41     {
     42         tree[root].data=data;
     43         return;
     44     }
     45     int ls=root<<1,rs=root<<1|1;
     46     int mid=(tree[root].l+tree[root].r)>>1;
     47     if(tree[root].data!=-1)
     48     {
     49         tree[ls].data=tree[rs].data=tree[root].data;
     50         tree[root].data=-1;
     51     }
     52     if(mid>=r)
     53         update(ls,l,r,data);
     54     else if(mid<l)
     55         update(rs,l,r,data);
     56     else
     57     {
     58         update(ls,l,mid,data);
     59         update(rs,mid+1,r,data);
     60     }
     61 }
     62 
     63 int query(int root,int l,int r)
     64 {
     65     if(tree[root].l==l && tree[root].r==r)
     66     {
     67         if(tree[root].data!=-1)
     68             return tree[root].data*(r-l+1);
     69         else
     70         {
     71             int ls=root<<1,rs=root<<1|1;
     72             int mid=(tree[root].l+tree[root].r)>>1;
     73             return query(ls,l,mid)+query(rs,mid+1,r);
     74         }    
     75     }
     76     int ls=root<<1,rs=root<<1|1;
     77     int mid=(tree[root].l+tree[root].r)>>1;
     78     if(mid>=r)
     79         return query(ls,l,r);
     80     else if(mid<l)
     81         return query(rs,l,r);
     82     else
     83         return query(ls,l,mid)+query(rs,mid+1,r);
     84 }
     85 
     86 int main()
     87 {
     88     //freopen("hdu.in","r",stdin);
     89     scanf("%d",&t);
     90     for(int rr=1;rr<=t;rr++)
     91     {
     92         scanf("%d%d",&n,&q);
     93         init();
     94         build(1,1,n);
     95         while(q--)
     96         {
     97             scanf("%d%d%d",&x,&y,&z);
     98             update(1,x,y,z);
     99         }
    100         ans=query(1,1,n);
    101         printf("Case %d: The total value of the hook is %d.
    ",rr,ans);
    102     }
    103     return 0;
    104 }
  • 相关阅读:
    计算机组成原理:“性能”是什么?
    试题 基础练习 Huffuman树
    最小堆的插入删除函数
    特殊函数
    进程控制
    进程的描述
    进程管理
    生成和配置Linux操作系统
    系统调用
    Shell脚本编程
  • 原文地址:https://www.cnblogs.com/BBBob/p/5947901.html
Copyright © 2011-2022 走看看