zoukankan      html  css  js  c++  java
  • 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

    学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新。

    区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想:

      就是在数据结构中,树形结构可以线性存储(线性表)也可以树状存储(链表)

    树形
    typedef struct node { int data; struct node* Lchild; struct node* Rchild; }Btree,*BTree;
    BTree = (BTree)malloc(Btree);
    好像是这样吧...大半个暑假过去忘得一干二净...这个并不重要....

    然后顺序就是存到顺序表了,第i个节点的左孩子节点就是i*2,右孩子节点就是i*2+1,这个是一个性质。

    这个题就是用了树结构的顺序表。

    好像也没啥说的,简单的模板题,看代码吧:

     1 #include<cstdio>
     2 #define N 100003
     3 using namespace std;
     4 struct nod
     5 {
     6     int data,l,r,lazy;
     7 }tree[4*N];
     8 
     9 void push_up(int i)
    10 {
    11     tree[i].data = tree[i*2].data+tree[i*2+1].data;
    12 }
    13 
    14 void build_tree(int i,int l,int r)
    15 {
    16     tree[i].l = l;
    17     tree[i].r = r;
    18     tree[i].lazy = -1;
    19     if(l==r){
    20         tree[i].data = 1;
    21         return;
    22     }
    23     int mid = (r + l)/2;
    24     build_tree(i*2,l,mid);
    25     build_tree(i*2+1,mid+1,r);
    26     push_up(i);
    27 }
    28 
    29 void push_down(int i)
    30 {
    31     tree[i*2].data = tree[i].lazy*(tree[i*2].r-tree[i*2].l+1);
    32     tree[i*2+1].lazy = tree[i*2].lazy = tree[i].lazy;
    33     tree[i*2+1].data = tree[i].lazy*(tree[i*2+1].r-tree[i*2+1].l+1);
    34     tree[i].lazy = -1;
    35 }
    36 
    37 void updata(int i,int v,int l,int r)
    38 {
    39     if(l<=tree[i].l&&tree[i].r<=r)
    40     {
    41         tree[i].data = (tree[i].r-tree[i].l+1)*v;
    42         tree[i].lazy = v;
    43         return;
    44     }
    45     if(tree[i].lazy!=-1) push_down(i);
    46     int mid = (tree[i].l+tree[i].r)/2;
    47     if(l<=mid)
    48         updata(i*2,v,l,r);
    49     if(r > mid)
    50         updata(i*2+1,v,l,r);
    51     push_up(i);
    52 }
    53 
    54 int query(int i,int l,int r)
    55 {
    56     if(l<=tree[i].l&&tree[i].r<=r)
    57     {
    58         return tree[i].data;
    59     }
    60     if(tree[i].lazy!=-1)
    61         push_down(i);
    62     int mid=(tree[i].l+tree[i].r)/2;
    63     if(r<=mid)
    64         return query(i*2,l,r);
    65     if(l>=mid+1)
    66         return query(i*2+1,l,r);
    67     return query(i*2,l,r)+query(i*2+1,l,r);
    68 }
    69 
    70 int main()
    71 {
    72     int noc,ug;
    73     scanf("%d",&noc);
    74     ug = noc;
    75     while(noc--)
    76     {
    77         int n,q,x,y,z;
    78         scanf("%d",&n);
    79         build_tree(1,1,n);
    80         scanf("%d",&q);
    81         for(int i=1;i<=q;i++)
    82         {
    83             scanf("%d%d%d",&x,&y,&z);
    84             updata(1,z,x,y);
    85         }
    86         printf("Case %d: The total value of the hook is %d.
    ",ug-noc,tree[1].data);
    87     }
    88 }
  • 相关阅读:
    bootstrap 兼容 IE8
    在IE8的基础上安装IE11
    前台
    dll 库文件下载地址
    年轻
    linux 异常
    Navicat断网时连不上数据库
    jQuery
    破解版 Teamver 安装
    mysql
  • 原文地址:https://www.cnblogs.com/liwenchi/p/5762608.html
Copyright © 2011-2022 走看看