【原创】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.\n",ug-noc,tree[1].data);
87     }
88 }
时间: 2024-10-22 21:40:27

【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)的相关文章

hdu1698 Just a Hook (线段树区间更新 懒惰标记)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 22730    Accepted Submission(s): 11366 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

Hdu1698Just a Hook线段树区间更新

区间更新基础..不说了,也是照着notonlysuccess的博客撸的. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list>

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU 1698 Just a Hook (线段树 区间更新基础)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21856    Accepted Submission(s): 10963 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

HDU 1689 Just a Hook 线段树区间更新求和

点击打开链接 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18894    Accepted Submission(s): 9483 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible

(简单) HDU 1698 Just a Hook , 线段树+区间更新。

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

Just a Hook 线段树 区间更新

Just a Hook 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.

hdu 1698 Just a Hook 线段树区间更新

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 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

hdu - 1689 Just a Hook (线段树区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<=z<=3),问更新完后的总价值. 线段树的区间更新,需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新或者询问的时候. 这题只需要输出总区间的信息,即直接输出1结点的信息. 1 #include <iostream> 2 #include <