HDU 1698-Just a Hook
来源:互联网 发布:长兴人民法院淘宝拍卖 编辑:程序博客网 时间:2024/06/07 23:49
题意:
题意:一段线段由n条单位长度线段组成,每次操作把一个区间内的线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。
题外话:当初刚刚学线段树,老是想着弄个线段树总模板来一劳永逸,结果喜闻乐见的崩了,线段树的强大是我无法想象的,不能一味单纯的模板,重要的是自己的思想,今天再做他,终于想出来了
#include<stdio.h>#include<string.h>#define lson root<<1,l,mid#define rson root<<1|1,mid+1,r const int MAXN=100000+7;int t[MAXN<<2],a[MAXN],add[MAXN<<2];void build(int root,int l,int r){ add[root]=0; if(l==r) t[root]=a[l]; else{ int mid=(l+r)/2; build(lson); build(rson); t[root]=t[root<<1]+t[root<<1|1]; }}void pushdown(int root,int m){ if(add[root]) { add[root<<1]=add[root]; add[root<<1|1]=add[root]; t[root<<1]=add[root]*(m-(m>>1));//这里直接用要变成什么金属去覆盖之前的金属就可以了,而不是加上去 t[root<<1|1]=add[root]*(m>>1); add[root]=0; }}void update(int root,int l,int r,int L,int R,int x){ if(r<L||l>R) return ; if(L<=l&&r<=R) { t[root]=(r-l+1)*x; add[root]=x; return ; } pushdown(root,r-l+1); int mid=(l+r)/2; if(L<=mid) update(lson,L,R,x); if(mid<R) update(rson,L,R,x); t[root]=t[root<<1]+t[root<<1|1];}int query(int root,int l,int r,int L,int R){ if(r<L||l>R) return 0; if(L<=l&&r<=R) return t[root]; int mid=(l+r)/2; pushdown(root,r-l+1); return query(lson,L,R)+query(rson,L,R); }int main(){ int T,n,m,i,k=1; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=n;i++) a[i]=1; scanf("%d",&m); build(1,1,n); while(m--) { int x,y,z; scanf("%d%d%d",&x,&y,&z); update(1,1,n,x,y,z); } printf("Case %d: The total value of the hook is %d.\n",k++,query(1,1,n,1,n)); }}
阅读全文
0 0
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- Hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 just a hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- HDU-1698-Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- HDU 1698 - Just a Hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- 我的Spring学习记录(四)
- HDU 5510 Bazinga(kmp)
- linux 查看服务器序列号(S/N)
- 折腾了半天,发现在lightswitch下有个解决FTP等功能的好方法。
- LeedCode Add Two Numbers
- HDU 1698-Just a Hook
- POJ 2154 Color(polya定理+欧拉函数)
- leetcode 334. Increasing Triplet Subsequence 自增序列3个元素
- GYM 100801J Journey to the “The World's Start”【二分+Dp+单调队列】
- 如何判断2条线段是否相交(编程实现)?
- Nginx ngx_http_referer_module模块根据referer屏蔽或禁止访问
- PAT 甲级 1069. The Black Hole of Numbers (20)
- java-工厂模式
- 字段级