题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
题目大意:给你三种硬币,分别面值为1,2,3。初始时,面值都为1,后面没更改一次,就把这个区间的银币换位面值为 X的硬币,最后求所有的硬币面值是多少。
直接贴代码啦
code:
#include<stdio.h> #include<iostream> #define L(u) (u<<1) #define R(u) (u<<1|1) const int M=100010; struct Node { int l,r; int add,sum; }node[M*4]; //int a[M]; void pushup(int u) { node[u].sum=node[L(u)].sum+node[R(u)].sum; return ; } void pushdown(int u) { node[L(u)].add=node[u].add; node[L(u)].sum=(node[L(u)].r-node[L(u)].l+1)*node[u].add; node[R(u)].add=node[u].add; node[R(u)].sum=(node[R(u)].r-node[R(u)].l+1)*node[u].add; node[u].add=0; } void build(int i,int left,int right) { node[i].l=left; node[i].r=right; node[i].add=0; if(node[i].l==node[i].r) { node[i].sum=1; return; } int mid=(node[i].l+node[i].r)/2; build(L(i),left,mid); build(R(i),mid+1,right); pushup(i); } void update(int u,int left,int right,int val) { if(left<=node[u].l&&node[u].r<=right) { node[u].add=val; node[u].sum=(node[u].r-node[u].l+1)*val;<span style="white-space:pre"> </span>//注意之处,这个区间的值是这么计算的<span style="white-space:pre"> </span> return ; } //node[u].sum=(right-left+1)*val; if(node[u].add) pushdown(u); int mid=(node[u].l+node[u].r)/2; if(right<=mid) update(L(u),left,right,val); else if(left>mid) update(R(u),left,right,val); else { update(L(u),left,mid,val); update(R(u),mid+1,right,val); } node[u].sum=node[L(u)].sum+node[R(u)].sum; } int query(int u,int left,int right) { if(left<=node[u].l&&node[u].r<=right) { return node[u].sum; } if(node[u].add) pushdown(u); int mid=(node[u].l+node[u].r)/2; if(right<=mid) return query(L(u),left,right); else if(left>mid) return query(R(u),left,right); else return (query(L(u),left,mid)+query(R(u),mid+1,right)); } int main() { int T,n,m,i,x,y,z; int k=0; scanf("%d",&T); while(T--) { k++; scanf("%d%d",&n,&m); build(1,1,n); for(i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&z); update(1,x,y,z); } printf("Case %d: The total value of the hook is %d.\n",k,query(1,1,n)); } return 0; }
hdu 1698 Just a Hook,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u010304217/article/details/37668733