HDU 1689
题意:给定序列,默认值为1。m次操作,每次操作把区间 [x,y] 的值改为 z,
求操作完序列的和。
标签:区间修改
分析: 模板题,但是注意多组输入数据,每次要重置 lazy tag
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <algorithm>
#define INF 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int mod=1e9+7;
const int maxn=2e5+10;
struct node {
ll l,r,k,lz;
} tree[maxn<<2];
ll n,m;
void push_up(ll p){
tree[p].k=tree[p<<1].k+tree[(p<<1)|1].k;
}
void push_down(ll p){
if (tree[p].lz){
tree[p<<1].k=tree[p].lz*(tree[p<<1].r-tree[p<<1].l+1);
tree[(p<<1)|1].k=tree[p].lz*(tree[(p<<1)|1].r-tree[(p<<1)|1].l+1);
tree[p<<1].lz=tree[p].lz;
tree[(p<<1)|1].lz=tree[p].lz;
tree[p].lz=0;
}
}
void build(ll p, ll l, ll r) {
tree[p].l=l;
tree[p].r=r;
tree[p].lz=0; //重置 lazy tag
if (l==r) {
tree[p].k=1;
return;
}
ll m=(l+r)>>1;
build(p<<1,l,m),build((p<<1)|1,m+1,r);
push_up(p);
}
void update(ll p, ll x, ll y, ll z){
if (x<=tree[p].l && y>=tree[p].r){
tree[p].k=z*(tree[p].r-tree[p].l+1);
tree[p].lz=z;
return;
}
push_down(p);
ll m=(tree[p].l+tree[p].r)>>1;
if (x<=m) update(p<<1,x,y,z);
if (y>m) update((p<<1)|1,x,y,z);
push_up(p);
}
ll query(ll p, ll x, ll y) {
if (x<=tree[p].l && y>=tree[p].r) return tree[p].k;
push_down(p);
ll m=(tree[p].l+tree[p].r)>>1;
ll ans=0;
if (x<=m) ans+=query(p<<1,x,y);
if (y>m) ans+=query((p<<1)|1,x,y);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin>>T;
int ca=1;
while (T--){
cin>>n>>m;
build(1,1,n);
ll X,Y,Z;
while (m--){
cin>>X>>Y>>Z;
update(1,X,Y,Z);
}
cout<<"Case "<<ca++<<": The total value of the hook is "<<query(1,1,n)<<"."<<endl;
}
return 0;
}