题意:胖子有一条大jb,大JB由n个小JB组成,每次操作将一个区间的小JB变成金银铜三者之一,最后取出所有区间的JB总价值
思路:和刷气球差不多意思了,简单的区间更新,无需更新到叶子节点,防止超时
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#define INF 0x3f3f3f3f
#define maxn 1000100
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
int ans[maxn],len;
struct node
{
int l,r,n;
}tree[maxn*4];
/*void pushup(int pos)
{
tree[pos].n = max(tree[pos<<1].n,tree[pos<<1|1].n);
}
void pushdown(int pos)
{
if(!tree[pos].lazy)
return;
tree[pos<<1].sum += tree[pos].lazy-tree[pos<<1].n;
tree[pos<<1|1].sum += tree[pos].lazy;
tree[pos<<1].lazy = tree[pos].lazy;
tree[pos<<1|1].lazy = tree[pos].lazy;
tree[pos].n = tree[pos].lazy;
tree[pos].lazy = 0;
}*/
void build(int l,int r,int pos)
{
tree[pos].l = l;
tree[pos].r = r;
tree[pos].n = 1;
//if(l == r)
//{
//scanf("%lld",&tree[pos].sum);
//return;
//}
if(l != r)
{
int mid = (l+r)/2;
build(l,mid,pos<<1);
build(mid+1,r,pos<<1|1);
}
}
void update(int l,int r,int k,int pos)
{
if(tree[pos].n == k)
return;
if(tree[pos].l == l && tree[pos].r == r)
{
tree[pos].n = k;
return ;
}
if(tree[pos].n != -1)
{
tree[pos<<1].n = tree[pos<<1|1].n = tree[pos].n;
tree[pos].n = -1;
}
int mid = (tree[pos].l + tree[pos].r) >> 1;
if(r <= mid)
update(l,r,k,pos<<1);
else if(l > mid)
update(l,r,k,pos<<1|1);
else
{
update(l,mid,k,pos<<1);
update(mid+1,r,k,pos<<1|1);
}
// pushup(pos);
} //自上而下更新节点
/*int query(int l,int r,int pos)
{
if(l == tree[pos].l && r == tree[pos].r)
return tree[pos].n;
pushdown(pos);
int mid = (tree[pos].l+tree[pos].r)>>1;
if(r<=mid)
return query(l,r,pos<<1);
else if(l>mid)
return query(l,r,pos<<1|1);
else
return max(query(l,mid,pos<<1),query(mid+1,r,pos<<1|1));
}*/
int Find(int pos)
{
if(tree[pos].n != -1)
return (tree[pos].r - tree[pos].l + 1) * tree[pos].n;
else
return Find(pos<<1) + Find(pos<<1|1);
}
int main()
{
int t,n,m,C = 1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int a,b,c;
build(1,n,1);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1);
}
printf("Case %d: The total value of the hook is %d.\n",C++,Find(1));
}
return 0;
}