http://acm.hdu.edu.cn/showproblem.php?pid=3038

大致题意:

有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为v,但每次给出的区间可能与之前的有冲突,问这样起冲突的区间共有多少个

首先区间[a,b]的和可由区间[0,b]的和减去区间[0,a-1]的和得到

但是我们不太可能知道[0,b],故我们只用知道和b的合并过的区间的左端点就行

其实并查集实质就是一颗树,我们可以以树的角度去看待它,理解维护过程

不理解的同学可以在纸上多划几遍,理解过程

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e5+;
using namespace std; int fa[];//fa[i] 存i的最左端点
int sum[];//记录i到根节点之间和的差值,若再跟节点右侧 则为负数 void init(int n)
{
for(int i=;i<=n;i++)
fa[i]=i;
}
int Find(int x)//带权并查集,此时find不单有查找任务,还有更新距离任务
{
if(x!=fa[x])
{
int t=fa[x];//记录之前的父节点
fa[x]=Find(fa[x]);//路径压缩
sum[x]+=sum[t];//递归维护sum
}//记录到根节点的距离,根节点是一个区间的一个端点而不是一个区间,输入的区间被合并成了两个点
return fa[x];
} int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif
// ios_base::sync_with_stdio(false);
// cin.tie(NULL); int n,m;
while(~scanf("%d %d",&n,&m))
{
init(n);
memset(sum,,sizeof(sum));
int ans=;
for(int i=;i<=m;i++)
{
int l,r,v;
scanf("%d %d %d",&l,&r,&v);//
int x=Find(l-); //注意为什么要-1
int y=Find(r);
if(x!=y)//如果不是一个集合 合并
{
fa[y]=x;
sum[y]=sum[l-]-sum[r]+v;
}
else if(sum[r]-sum[l-]!=v) ans++;//当有相同的最左端时,直接判断
}
printf("%d\n",ans);
} return ;
}

-

HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)的更多相关文章

  1. hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. HDU 3038 How Many Answers Are Wrong(带权并查集)

    太坑人了啊,读入数据a,b,s的时候,我刚开始s用的%lld,给我WA. 实在找不到错误啊,后来不知怎么地突然有个想法,改成%I64d,竟然AC了 思路:我建立一个sum数组,设i的父亲为fa,sum ...

  3. POJ-1733 Parity game(带权并查集区间合并)

    http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...

  4. HDU3038 How Many Answers Are Wrong —— 带权并查集

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...

  5. hdu3038How Many Answers Are Wrong(带权并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...

  6. HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  7. HDU3038 How Many Answers Are Wrong[带权并查集]

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. 【HDU3038】How Many Answers Are Wrong - 带权并查集

    描述 TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always ...

  9. hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13

    了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...

随机推荐

  1. python 定义一个空集合、空字典

    s = set() #定义一个空集合 s = {} #定义一个空字典

  2. Java自学-集合框架 Comparator和Comparable

    Java Comparator和Comparable 步骤 1 : Comparator 假设Hero有三个属性 name,hp,damage 一个集合中放存放10个Hero,通过Collection ...

  3. 021-PHP常用的数值类型判断函数

    <?php //判断数组 $colors = array("red", "blue", "green"); if(is_array($ ...

  4. Swift 类的使用class

    /* 类属性的介绍 Swift中类的属性有多种 1.存储属性:存储示例的常量和变量 2.计算属性:通过某种方式计算出来的属性 3.类属性:与整个类自身相关的属性 存储属性 存储属性是最简单的属性,它作 ...

  5. spark-submit脚本分析

    执行任务 ./spark-submit \ --class cn.com.dtmobile.spark.DebugTest \ --master yarn \ --deploy-mode client ...

  6. ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建

    ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建 集群规划: hostname NameNode DataNode JournalNode Re ...

  7. Tomcat JDK MySql 安装配置

    Tomcat 7绿色版指定jdk并注册服务  https://blog.csdn.net/weixin_43976019/article/details/89386171   例如:service.b ...

  8. Spring入门之二-------SpringIoC之实例化Bean以及注入Bean

    一.实例化Bean 1. 通过默认构造方法实创建Bean public class Bean1 { public Bean1() { System.out.println(this.getClass( ...

  9. 用Python复制文件的9个方法(转)

    转自:https://zhuanlan.zhihu.com/p/35725217 用Python复制文件的9个方法 Python 中有许多“开盖即食”的模块(比如 os,subprocess 和 sh ...

  10. centos系统 通过命名查找需要安装哪个安装包 command not found

    场景: 服务器未安装命令,但是我们需要使用,不知道该安装什么包,以sz命令为例 [root@localhost linshi]# sz tes.sh -bash: /usr/bin/sz: 没有那个文 ...