博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kuangbin专题七 HDU1698 Just a Hook (区间设值 线段树)
阅读量:6605 次
发布时间:2019-06-24

本文共 4301 字,大约阅读时间需要 14 分钟。

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.

InputThe 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.
OutputFor 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

11021 5 25 9 3

Sample Output

Case 1: The total value of the hook is 24. 将一段区间设置为同一个数,然后区间求和。自己敲得时候,还是发现了一点细节问题。这个和修改有区别,pushdown的左右孩子的sum是= 而不是 += 可以理解 还有最重要的一点!!! 以后的lazy还是不要直接写了,应该先判断一下,就因为这个,找了好长时间bug 还有一个问题是 区间求和时的 Pushdown和Pushup 不是必要的???不是太懂。
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std; 15 #define FO freopen("in.txt","r",stdin); 16 #define rep(i,a,n) for (int i=a;i
=a;i--) 18 #define pb push_back 19 #define mp make_pair 20 #define all(x) (x).begin(),(x).end() 21 #define fi first 22 #define se second 23 #define SZ(x) ((int)(x).size()) 24 #define debug(x) cout << "&&" << x << "&&" << endl; 25 #define lowbit(x) (x&-x) 26 #define mem(a,b) memset(a, b, sizeof(a)); 27 typedef vector
VI; 28 typedef long long ll; 29 typedef pair
PII; 30 const ll mod=1000000007; 31 const int inf = 0x3f3f3f3f; 32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){ if(b&1)res=res*a%mod;a=a*a%mod;}return res;} 33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 34 //head 35 36 const int maxn=100010; 37 int sum[maxn<<2],lazy[maxn<<2],_,n,q,l,r,val; 38 39 void Pushup(int rt) { 40 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 41 } 42 43 void Pushdown(int rt,int x) { 44 if(lazy[rt]) { //!!! 45 lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; 46 sum[rt<<1]=(x-(x>>1))*lazy[rt];//!!! = 47 sum[rt<<1|1]=(x>>1)*lazy[rt]; 48 lazy[rt]=0; 49 } 50 } 51 52 void build(int rt,int L,int R) { 53 lazy[rt]=0; 54 if(L==R) { 55 sum[rt]=1; 56 return; 57 } 58 int mid=(L+R)>>1; 59 build(rt<<1,L,mid); 60 build(rt<<1|1,mid+1,R); 61 Pushup(rt); 62 } 63 64 void Updata(int rt,int L,int R,int l,int r) { 65 if(L>=l&&R<=r) { //核心 66 lazy[rt]=val; 67 sum[rt]=(R-L+1)*val; 68 return; 69 } 70 Pushdown(rt,R-L+1); 71 int mid=(L+R)>>1; 72 if(l<=mid) Updata(rt<<1,L,mid,l,r); 73 if(r>mid) Updata(rt<<1|1,mid+1,R,l,r); 74 Pushup(rt); 75 } 76 77 int Query(int rt,int L,int R,int l,int r) { 78 if(L>=l&&R<=r) return sum[rt]; 79 Pushdown(rt,R-L+1); 80 int ans=0; 81 int mid=(L+R)>>1; 82 if(l<=mid) ans+=Query(rt<<1,L,mid,l,r); 83 if(r>mid) ans+=Query(rt<<1|1,mid+1,R,l,r); 84 Pushup(rt); 85 return ans; 86 87 } 88 89 int main() { 90 int cur=1; 91 for(scanf("%d",&_);_;_--) { 92 scanf("%d",&n); 93 build(1,1,n); 94 scanf("%d",&q); 95 while(q--) { 96 scanf("%d%d%d",&l,&r,&val); 97 Updata(1,1,n,l,r); 98 } 99 printf("Case %d: The total value of the hook is %d.\n",cur++,Query(1,1,n,1,n));100 }101 }

 

 

转载于:https://www.cnblogs.com/ACMerszl/p/9862235.html

你可能感兴趣的文章
port 22: Connection refused
查看>>
java中关键字volatile的作用(转载)
查看>>
基础查询语句
查看>>
Linux 硬链接、软链接
查看>>
ORACLE PL/SQL编程之六: 把过程与函数说透
查看>>
[.Net线程处理系列]专题五:线程同步——事件构造
查看>>
Welcom To My Blog
查看>>
windows 下使clion支持c++11操作记录
查看>>
组件里传值到父级
查看>>
201521123009 《Java程序设计》第13周学习总结
查看>>
js堆栈与队列简单记忆
查看>>
【转】说说云计算中的地域和可用区概念
查看>>
sqlite3 判断数据库和表的状态
查看>>
【转】Shell 编程专题
查看>>
51术语详解
查看>>
angularJs2随记
查看>>
验证文件切分实验
查看>>
三条圆形加载进度条
查看>>
scala学习手记18 - Any和Nothing
查看>>
使用 FFMPEG 命令为视频嵌入字幕
查看>>