hdu 1698 Just a Hook (线段树)

题意:

t组数据,给你n个数,初始为1,q次操作 区间更新

线段树裸题,但是我wa了好几次。。。因为最后输出的那个hook 我写成了book。。看了半天都没发现

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int maxn=1e5+5;
const int maxm=5e5+5;
const int inf=0x3f3f3f3f;

LL n, a[100005*4], d[maxn*4], b[maxn*4];
void build(LL l, LL r, LL p) {
  b[p]=0;
  if (l == r) {
    d[p] = 1;
    return;
  }
  LL m = (l + r) >> 1;
  build(l, m, p << 1), build(m + 1, r, (p << 1) | 1);
  d[p] = d[p << 1] + d[(p << 1) | 1];
}
void update(LL l, LL r, LL c, LL s, LL t, LL p) {
  if (l <= s && t <= r) {
    d[p] = (t - s + 1) * c, b[p] = c;
    return;
  }
  LL m = (s + t) >> 1;
  if (b[p])
    d[p << 1] = b[p] * (m - s + 1), d[(p << 1) | 1] = b[p] * (t - m),
        b[p << 1] = b[p], b[(p << 1) | 1] = b[p];
  b[p] = 0;
  if (l <= m) update(l, r, c, s, m, p << 1);
  if (r > m) update(l, r, c, m + 1, t, (p << 1) | 1);
  d[p] = d[p << 1] + d[(p << 1) | 1];
}
LL getsum(LL l, LL r, LL s, LL t, LL p) {
  if (l <= s && t <= r) return d[p];
  LL m = (s + t) >> 1;
  if (b[p])
    d[p << 1] = b[p] * (m - s + 1), d[(p << 1) | 1] = b[p] * (t - m),
        b[p << 1] = b[p], b[(p << 1) | 1] = b[p];
  b[p] = 0;
  LL sum = 0;
  if (l <= m) sum += getsum(l, r, s, m, p << 1);
  if (r > m) sum += getsum(l, r, m + 1, t, (p << 1) | 1);
  return sum;
}
int main(int argc, char const *argv[])
{
  int t;scanf("%d",&t);
  for(int cas=1;cas<=t;cas++)
  {
    int q;
    scanf("%lld%d",&n,&q);
    build(1,n,1);
    while(q--)
    {
      LL x,y,z;scanf("%lld%lld%lld",&x,&y,&z);
      update(x,y,z,1,n,1);
    }
    printf("Case %d: The total value of the hook is %lld.\n",cas,getsum(1,n,1,n,1));
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/wzazzy/article/details/88146515