线段树模板题,注意要用到lazy操作便是了。
代码如下:
#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lrt rt << 1
#define rrt rt << 1|1
#define root 1,n,1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const LL INF = (((LL)1)<<62)+1;
using namespace std;
const double eps = 1e-5;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const double M=1e-8;
const int N=1e6;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int n,m,st[N],lazy[N];
void pushdown(int rt,int len)
{
int mid=(len+1)>>1;
if (lazy[rt])
{
st[rt<<1]=mid*lazy[rt];
st[rt<<1|1]=(len-mid)*lazy[rt];
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
lazy[rt]=0;
}
}
void updata(int a,int b,int c,int l,int r,int rt)
{
if (a>r || b<l) return ;
if (a<=l && r<=b) {
st[rt]=c*(r-l+1);
lazy[rt]=c;
return ;
}
pushdown(rt,r-l+1);
int mid=MID(l,r);
if (a<=mid) updata(a,b,c,lson);
if (b>mid) updata(a,b,c,rson);
st[rt]=st[lrt]+st[rrt];
}
void build(int l,int r,int rt)
{
st[rt]=r-l+1;
if (l==r) { return; }
int mid=MID(l,r);
build(lson);
build(rson);
}
int main()
{
int i,j,T,cnt=1;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
build(root);
MS(lazy,0);
for (i=0;i<m;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
updata(a,b,c,root); // for (j=1;j<n*3;j++) cout<<st[j]<<" "; cout<<endl;
}
printf("Case %d: The total value of the hook is %d.\n",cnt++,st[1]);
}
}