(kuangbin带你飞--并查集)How Many Answers Are Wrong HDU(带权并查集)

原题目:

TT and FF are ... friends. Uh... very very good friends -________-b

FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).

Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.

Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.

The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.

However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.

What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)

Input

Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.

Output

A single line with a integer denotes how many answers are wrong.

Sample Input

10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1

Sample Output

1

中文概要:

:有两个人一个叫TT的男孩一个叫FF的女孩,这个叫TT的男孩会经常叫这个女孩一起玩一个游戏,这个有些是这样的,随便写一个数列,现在TT会选择一个区间,然后让FF计算这个区间里面所有数的和,这是一个非常非常无聊的游戏,于是FF准备捉弄一下TT,有时候她会故意计算出来一个错的答案,当然TT也比较聪明,他会发现这个答案跟以前的答案会有冲突,输出有多少个错误的数据

#include<bits/stdc++.h>
using namespace std;
const int maxn  = 200005;
int f[maxn], val[maxn];
//f[i] 存 i 的最左端点;
//val[i] 记录下标i所能到达最左端f[i]这个区间中所有数的和; 
int Find(int x)
{
    int k = f[x];
    if(f[x] != x)
    {
        f[x] = Find(f[x]);
        val[x] += val[k];
    }
    return f[x];
}
int main()
{
    int N, M;
    while(scanf("%d%d", &N, &M) != EOF)
    {
        int i, u, v, w, ans=0;
        for(i=0; i<=N; i++)
        {
            f[i] = i;
            val[i] = 0;
        }
        while(M--)
        {
            scanf("%d%d%d", &u, &v, &w);
            u = u-1;                //注意为什么要减一
            int ru = Find(u), rv = Find(v);
            if(ru == rv && val[u]+w != val[v])  // 当有相同的最左端时,直接判断 
                ans++;
            // 当最左端不同时 
            else if(ru < rv)  // 结合上面给出的条件 更新区间[ru,rv]所有数的和   
            {                 // 情况 1 
                f[rv] = ru;
                val[rv] = val[u] - val[v] + w;
            }
            else if(ru > rv)   // 更新 区间 [rv,ru]所有数的和    
            {                  // 情况 2 
                f[ru] = rv;  
                val[ru] = val[v] - val[u] - w;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

思路:

刚看题百思不得其解。。。。看完题解恍然大悟

原文章:https://blog.csdn.net/obsorb_knowledge/article/details/81168166

那么我们怎么把排除这种情况的思路转移到幷查集上呢?幷查集 能找到同伙合并成一伙人,并且选出一个老大,我们能运用幷查集把给出的多个连续的区间合并成一个区间,而且选出这个区间最左边界为老大;定义 f 和 val 数组,  f[i] 存 i 的最左端点;val[i] 记录下标i所能到达最左端f[i]这个区间中所有数的和;

这是当 B,C的最左端点相同时

比如上面这个图, 我们已经知道了AB的长度和AC的长度,如果下面再来一个CB,我们就可以知道C的最左端是A,B的最左端也是A,那么就可以判断一个AC+CB的长度是不是等于AB的长度就可以了

​​​​​​当B,C的最左端点不同时,下面把B和C看成 u和v

当v的最左端点大于u的最左端点时,情况1,由给出的条件可以得出 [ru,rv]中的和,很明显val[ru,rv] = val [ru,u] + val[u,v]  - [rv,v];(上面式子端点处,没有处理,这样是为了方便理解,下面代码中我们把所有的区间的左端点减一,变成了开区间) ;

当v的最左端点小于u的最左端点时,情况2,也可由上面给出的条件得出[rv,ru]中的和,式子很明显,看图自己想;

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值