1.题目描述:
x a b就是把a ~ b区间的值都改成x
3.解题思路:
也是一个线段树的裸题,可以用lazy降低复杂度,但是好像这题不明显。
4.AC代码:
#include
#define INF 0x3f3f3f3f
#define maxn 100010
#define lson root << 1
#define rson root << 1 | 1
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct tree
{
int l, r, w, lazy;
} t[maxn << 2];
void pushup(int root)
{
t[root].w = t[lson].w + t[rson].w;
}
void pushdown(int root)
{
if (t[root].lazy)
{
t[lson].lazy = t[rson].lazy = t[root].lazy;
t[lson].w = (t[lson].r - t[lson].l + 1) * t[root].lazy;
t[rson].w = (t[rson].r - t[rson].l + 1) * t[root].lazy;
t[root].lazy = 0;
}
}
void build(int l, int r, int root)
{
t[root].l = l;
t[root].r = r;
t[root].w = 0;
t[root].lazy = 0;
if (l == r)
{
t[root].w = 1;
return;
}
int mid = l + r >> 1;
build(l, mid, lson);
build(mid + 1, r, rson);
pushup(root);
}
void update(int z, int l, int r, int root)
{
if (l <= t[root].l && t[root].r <= r)
{
t[root].lazy = z;
t[root].w = z * (t[root].r - t[root].l + 1);
return;
}
pushdown(root);
int mid = t[root].l + t[root].r >> 1;
if (l <= mid)
update(z, l, r, lson);
if (r > mid)
update(z, l, r, rson);
pushup(root);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int T, n, q, kase = 0;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &q);
build(1, n, 1);
while (q--)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
update(z, x, y, 1);
}
printf("Case %d: The total value of the hook is %d.\n", ++kase, t[1].w);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}