有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少。

区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被分开的时候再往下传递。

 #include <cstdio>

 const int maxn =  + ;
int sum[maxn << ], set[maxn << ]; int n, qL, qR, m, v; void build(int o, int L, int R)
{
set[o] = ;
if(L == R) { sum[o] = ; return; }
int M = (L + R) / ;
build(o*, L, M);
build(o*+, M+, R);
sum[o] = sum[o*] + sum[o*+];
} void pushdown(int o, int L, int R)
{
if(set[o])
{
int M = (L + R) / ;
int lc = o*, rc = o*+;
set[lc] = set[rc] = set[o];
set[o] = ;
sum[lc] = set[lc] * (M - L + );
sum[rc] = set[rc] * (R - M);
}
} void update(int o, int L, int R)
{
if(qL <= L && qR >= R)
{
set[o] = v;
sum[o] = v * (R-L+);
return;
}
pushdown(o, L, R);
int M = (L + R) / ;
if(qL <= M) update(o*, L, M);
if(qR > M) update(o*+, M+, R);
sum[o] = sum[o*] + sum[o*+];
} int main()
{
//freopen("in.txt", "r", stdin); int T; scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
scanf("%d%d", &n, &m);
build(, , n);
while(m--)
{
scanf("%d%d%d", &qL, &qR, &v);
update(, , n);
}
printf("Case %d: The total value of the hook is %d.\n", kase, sum[]);
} return ;
}

代码君

HDU 1698 (线段树 区间更新) Just a Hook的更多相关文章

  1. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  2. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU(1698),线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...

  4. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  5. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

  6. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  7. HDU 3016 线段树区间更新+spfa

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. hdu 1698 线段树 区间修改

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

  9. Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

随机推荐

  1. HDOJ 1428 漫步校园

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. 了解Javascript 变量

    javascript语言变量的作用域可以分为局部变量和全局变量 函数内部定义的变量为局部变量,作用范围在整个函数体内,函数外定义的变量为全局变量,如果在函数内部定义变量时没有使用关键字var,那么该变 ...

  3. no module named firefly.master.master

    因为没有安装firefly python setup.py install

  4. Shell 备忘录

    此文收集工作中用到的Shell备忘,随用随机: 1.比较 -eq       等于,如:if [ "$a" -eq "$b" ] -ne       不等于,如 ...

  5. POJ 2013

    #include <iostream> #include <string> #define MAXN 20 using namespace std; string _m[MAX ...

  6. elasticsearch 八、重要的配置更改

    http://jingyan.baidu.com/article/7908e85c9fc626af491ad263.html

  7. 我是如何学习 Linux 的

    为何要学习 Linux? 这个问题可能困扰着很多 Linux 初学者和爱好者,其实我也说不上来为何要学习 Linux,可能最实在的理由就是—-Linux 相关工作岗位很多.在“见到” Linux 的第 ...

  8. HDU 5596/BestCoder Round #66 (div.2) GTW likes math 签到

    GTW likes math  Memory Limit: 131072/131072 K (Java/Others) 问题描述 某一天,GTW听了数学特级教师金龙鱼的课之后,开始做数学<从自主 ...

  9. spring_150803_service

    实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...

  10. 【uva1502/hdu4117-GRE Words】DP+线段树优化+AC自动机

    这题我的代码在hdu上AC,在uva上WA. 题意:按顺序输入n个串以及它的权值di,要求在其中选取一些串,前一个必须是后一个的子串.问d值的和最大是多少. (1≤n≤2×10^4 ,串的总长度< ...