题目链接

分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间【a,b】的值为2或3或者1,统计最后整个区间的和

本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找了,直接输出tree[1].value就可以了 =_=

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Max = ;
int hook[Max + ];
struct node
{
int l,r;
int value;
int tag;
};
node tree[ * Max];
void buildTree(int left, int right, int k)
{
tree[k].l = left;
tree[k].r = right;
tree[k].tag = -;
if(left == right)
{
tree[k].value = ;
return;
}
int mid = (left + right ) / ;
buildTree(left, mid, k * );
buildTree(mid + , right, k * + );
tree[k].value = tree[k * ].value + tree[k * + ].value;
}
void upDate(int left, int right, int k, int newp)
{
if(tree[k].l ==left && tree[k].r == right)
{
tree[k].value = (right - left + ) * newp;
tree[k].tag = newp;
return;
}
if(tree[k].tag != -)
{
tree[k * ].tag = tree[k * + ].tag = tree[k].tag;
tree[k * ].value = (tree[k * ].r - tree[k * ].l + ) * tree[k].tag;
tree[k * + ].value = (tree[k * + ].r - tree[k * + ].l + ) * tree[k].tag;
tree[k].tag = -;
}
int mid = (tree[k].l + tree[k].r) / ;
if(right <= mid)
{
upDate(left, right, k * , newp);
}
else if(mid < left)
{
upDate(left, right, k * + , newp);
}
else
{
upDate(left, mid, k * , newp);
upDate(mid + , right, k * + , newp);
}
tree[k].value = tree[k * ].value + tree[k * + ].value;
}
int Search(int k)
{
if(tree[k].tag != -)
{
tree[k * ].tag = tree[k * + ].tag = tree[k].tag;
tree[k * ].value = (tree[k * ].r - tree[k * ].l + ) * tree[k].tag;
tree[k * + ].value = (tree[k * + ].r - tree[k * + ].l + ) * tree[k].tag;
tree[k].tag = -;
}
if(tree[k].l == tree[k].r)
return tree[k].value;
return Search(k * ) + Search(k * + );
}
int main()
{
int test,n;
scanf("%d", &test);
for(int t = ; t <= test; t++)
{
int q,a,b,newp;
scanf("%d", &n);
buildTree(, n, );
scanf("%d", &q);
for(int i = ; i <= q; i++)
{
scanf("%d%d%d", &a, &b, &newp);
upDate(a, b, , newp);
}
printf("Case %d: The total value of the hook is %d.\n", t, tree[].value);
}
return ;
}

HDU1698Just a Hook(线段树 + 区间修改 + 求和)的更多相关文章

  1. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  2. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  3. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  4. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

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

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

  6. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

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

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

  8. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  9. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

随机推荐

  1. EXCEL时间日期转换为常规字符显示

    当我们做报表导入的时候,我们不得不思考这样一个问题,遇到的数据是时间格式的,而在EXCEL中,时间格式的单元格实际上是以1900年以后来计算的,例如,1900年是闰年(显然可以被4整除),那么1900 ...

  2. JNI系列——PassData

    1.在工程目录下创建xxx.jni包并在该包下创建JNI类 2.在JNI类中声明所有的本地方法,并加载库 3.在MainActivity类中实例化JNI对象,并调用其方法 4.通过Javah生成的方法 ...

  3. iOS常用---NSString,NSMutabuleString

    普通的创建字符串: NSString *string =[[NSString alloc]init]; //创建一个空的字符串,即 @“” 一 .字符串读取本地文件: 参数一: 文件地址 参数二: 文 ...

  4. PHP中CURL方法curl_setopt()函数的参数

    PHP CURL curl_setopt 参数 bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL ...

  5. Install marvel and head plugin for ealsticsearch

    安装ES插件 marvel marvel是ES的供开发者免费使用的管理工具,他内置了一款叫做Sense的控制台,Sense是运行在浏览器中的,基于Sense可以很方便的和ES进行通讯.官方文档中的很多 ...

  6. 【CodeVS 2083】Cryptcowgraphy 解密牛语

    http://codevs.cn/problem/2083/ 奶牛搜索题.我加了如下剪枝: 1.用字符串hash判重.注意判重时也要对字符串长度判重,否则会出现两个字符串长度不同但hash值相同的情况 ...

  7. JAVA System.getProperty() 与 System.getenv() 差异及示例

    System.getenv() 方法是获取指定的环境变量的值. System.getenv() 接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null. System.getP ...

  8. 一起学HTML基础-JavaScritp简介与语法

    简介: 1.什么是JavaScript? 它是个脚本语言,作用是使 HTML 页面具有更强的动态和交互性,它需要有宿主文件,它的宿主文件就是html文件.  JavaScript 是 Web 的编程语 ...

  9. 用css3让溢出内容显示省略号

    css3现在越来越普及了.给我们前端人员也带来了极大的便利.以前要实现让溢出的内容显示省略号还得通过js实现,现在完全可以用css代替之. 主要的代码如下: <style type=" ...

  10. 【Codeforces 707C】Pythagorean Triples(找规律)

    一边长为a的直角三角形,a^2=c^2-b^2.可以发现1.4.9.16.25依次差3.5.7.9...,所以任何一条长度为奇数的边a,a^2还是奇数,那么c=a^2/2,b=c+1.我们还可以发现, ...