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. eclipse中使用jstl

    错误提示为"can not find the tag library for http://java.sun.com/jsp/jstl/core" 这是我在练习把axis2和普通j ...

  2. 记--linux 下svn安装配置,同步web目录

    安装#yum install subversion 1.创建版本库 # svnadmin create /svn/project 2.配置svn服务的配置文件svnserver.conf文件 anon ...

  3. MongoDB 数据建模

    版权所有,未经许可,禁止转载 章节 MongoDB 入门 MongoDB 优势 MongoDB 安装 MongoDB 数据建模 MongoDB 创建数据库 MongoDB 删除数据库 MongoDB ...

  4. ERROR in Cannot find module 'node-sass'

    windows下,通过淘宝的npm镜像安装 npm install node-sass --registry=https://registry.npm.taobao.org (之前安装好过,一段时间没 ...

  5. docker安装出现"Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"

    今天按照这个教程使用WSL安装docker时遇到了个问题: 使用命令:$ docker search mysql 出现:Cannot connect to the Docker daemon at u ...

  6. bat 卸载程序的脚本

    @echo off :: BatchGotAdmin :------------------------------------- REM --> Check for permissions & ...

  7. C++ createprocess 打开word

    #define FileName _TEXT("E:\\DuplicateHandle伪句柄与实句柄的应用.docx") void CMFCApplication1Dlg::OnB ...

  8. 如何在Ubuntu 18.04上安装和卸载TeamViewer

    卸载命令:sudo apt --purge remove teamviewer 安装:https://www.linuxidc.com/Linux/2018-05/152282.htm 如何在Ubun ...

  9. 10 —— node —— 获取文件在前台遍历

    思想 : 前台主动发起获取 => ajax 1,前台文件 index.html <!DOCTYPE html> <html lang="en"> &l ...

  10. Day3-T1

    原题目 Describe:暴力出奇迹 [ 注意步长0.5!] code: #include<bits/stdc++.h> using namespace std; inline int r ...