题目链接

分析: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. HashSet<T>类 用法

    HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素 改变集的值的方法: HashSet<T>的 ...

  2. snr ber Eb/N0之间的区别与联系

    信噪比(S/N)是指传输信号的平均功率与加性噪声的平均功率之比,载噪比(C/N)指已经调制的信号的平均功率与加性噪声的平均功率之比,它们都以对数的方式来计算,单位为dB. 对同一个传输系统而言,载噪比 ...

  3. PS技能大全

    1.设置固定的图片大小 参考:http://jingyan.baidu.com/album/642c9d3418bec4644a46f72a.html?picindex=1

  4. 【Python】[函数式编程]高阶函数,返回函数,装饰器,偏函数

    函数式编程高阶函数 就是把函数作为参数的函数,这种抽象的编程方式就是函数式编程.--- - -跳过,不是很理解,汗 - ---

  5. android 使用多个接口

    今天,好久没有这么用过都忘记可以这样用了.来记录下: 一个类想要使用多个接口可以implements 接口1 , 接口2,...

  6. android listview 的监听事件

    今天遇到了一个比较让我头疼的问题,不过追根揭底只是我对listview理解的不够透彻罢了, 闲言少叙,说说我遇到的问题吧: 上篇随笔我写了关于listview的使用,如果你也已经写好了列表那么恭喜这一 ...

  7. 聚类算法kmeans

    1. 聚类问题 所谓聚类问题,就是给定一个元素集合D,其中每个元素具有n个可观察属性,使用某种算法将D划分成k个子集,要求每个子集内部的元素之间相异度尽可能低,而不同子集的元素相异度尽可能高. 2. ...

  8. iOS开发遇到的错误 -- Label显示多行文字导致宽度和高度的问题

    Label的宽度问题 注意:UILabel下面需要设置preferredMaxLayoutWidth ,设置了autolayout和numberofline的UIlabel才显示多行 label宽度的 ...

  9. ansible解密

    ansible是个什么东西呢?官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具.这个工具的目标有这么几项:让我们自动化部署APP:自动化管理 ...

  10. windows2003开启vpn服务

    远程连接登陆到Windows服务器,在配置VPN之前,需要设置两个地方: 1.请确认windows自带的防火墙处于关闭状态,可以在我的电脑点右键>>管理>>服务和应用程序> ...