题目链接

分析: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. 【转】XSD (xml Schema Definition)

    来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1.  定义一个Xml文档中都有什么元 ...

  2. Beta版本冲刺———第四天

    会议照片: 项目燃尽图: 1.项目进展: 今天解决的进度:新增加了一个撤销按钮,实现对上一步操作的撤销. 仍在进行对排行榜分数变更的实现. 2.每个人每天做的事情 郭怡锋:汇总工作进度,对此总结,进行 ...

  3. 1018MYSQL数据迁移到SQLSERVER

    -- 第一步利用MYSQL将数据结果的脚本迁移出来-- 第二步利用POWERDESGINER的反向功能,将脚本生成为物理模型 FILE-REVERSE DATEBASE -- 第三步将物理模型生成SQ ...

  4. PHPStorm2016如何安装主题

    今天放出如何安装phpstorm 主题教程,教程如下: 下载主题 地址:phpstoorm-themes.com 安装主题 PhpStorm and WebStorm themes can be in ...

  5. 对C语言中指针的一些新认识

    学C语言这么久了,才发现指针不是想象中那么简单,当初根本就没理解指针怎么用! 变量--是由操作系统自动分配存储空间的    指针--手动分配存储空间或指向已有变量的地址 指针中的内容需要手动释放,而变 ...

  6. [转]使用Sencha Ext JS 6打造通用应用程序

    原文地址:http://www.uedsc.com/using-sencha-ext-js-6-to-build-universal-apps.html 在Sencha和整个Ext JS团队的支持下, ...

  7. 【BZOJ 1030】【JSOI 2007】文本生成器 AC自动机+递推

    一直不理解到底怎么做啊,想了好久$TwT$ 最后终于明白了为什么找到第一个满足条件的$fail$就计算,因为避免重复,这个回答,,, 然后$root$下面要接上26个节点,这里26个字母中不在字典内的 ...

  8. 【BZOJ 1007】【HNOI 2008】水平可见直线 解析几何

    之前机房没网就做的这道题,用的解析几何判断交点横坐标 #include<cmath> #include<cstdio> #include<cstring> #inc ...

  9. bootstrap 2.3版与3.0版的使用区别

    bootstrap 2.3版与3.0版的使用区别 bootstrap已经推出了3.0的新版,看起来2.3.x版本也不会再更新了.那么bootstrap 2.3版与3.0版的区别在哪里呢?下面我们就来介 ...

  10. Oracle的自增长主键

    自增长主键 --首先建一个表TEST create table TEST(  NID int PRIMARY KEY,  test1 varchar2(20),  test2 varchar2(20) ...