线段树以及lazy tag的应用,不过对于实现pushdown的部分的熟练度有待提高
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <deque>
using namespace std;
const int maxn= 1e5+5;
struct Node
{
int l, r;
int sum, lz;
}segTree[maxn<<2];
int n;
void Build(int i, int l, int r)
{
if (l> r){
return;
}
segTree[i].l= l;
segTree[i].r= r;
segTree[i].lz= 0;
if (l== r){
segTree[i].sum= 1;
return;
}
int mid= (l+r)>>1;
Build(i<<1, l, mid);
Build((i<<1)|1, mid+1, r);
segTree[i].sum= segTree[i<<1].sum+segTree[i<<1|1].sum;
}
void Update(int x, int l, int r, int v)
{
if (l> r){
return;
}
if (segTree[x].l== l && segTree[x].r== r){
segTree[x].sum= v*(r-l+1);
segTree[x].lz= v;
return;
}
int mid= (segTree[x].l+segTree[x].r)>>1;
if (segTree[x].lz){
Update(x<<1, segTree[x].l, mid, segTree[x].lz);
Update((x<<1)|1, mid+1, segTree[x].r, segTree[x].lz);
segTree[x].lz= 0;
}
if (mid>= r){
Update(x<<1, l, r, v);
}
else if (mid< l){
Update((x<<1)|1, l, r, v);
}
else{
Update(x<<1, l, mid, v);
Update((x<<1)|1, mid+1, r, v);
}
segTree[x].sum= segTree[x<<1].sum+segTree[(x<<1)|1].sum;
}
int main(int argc, char const *argv[])
{
int kase, q;
scanf("%d", &kase);
for (int k_cnt= 1; k_cnt<= kase; ++k_cnt){
scanf("%d %d", &n, &q);
Build(1, 1, n);
while (q--){
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
Update(1, x, y, z);
}
printf("Case %d: The total value of the hook is %d.
", k_cnt, segTree[1].sum);
}
return 0;
}