带权并查集模板题详解

1. 题目描述

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) 
InputLine 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. 
OutputA 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

2.问题分析

需要在线判断数据是否有误,因为是以区间的形式给出和,所以我们想到了利用前缀和来求解。

所以我们需要维护的是区间起点到区间端点的和,我们利用并查集的思想,定义从某点到根节点的距离为区间和。

注意,为了维护区间加法,我们必须要维护一个左开右闭区间,(a,b]+(b,c]=(a,c]

如果想合并a, b,最终目的是求a,b,到其共同基准的距离.首先求出a, b的祖宗,即比较标准x, y,默认x认y为爹,(a的祖先认b的祖先为爹)所以a的祖先变成了y, x的祖先变成了y,所以a到a的祖先的距离(a到y)=(a到x) + (x 到y),, b到y的距离即为sum[b]没变;  
1:sum[a] = v + k; 
2:sum[b] = k + (y-x); 
2-1: y-x = sum[b] - sum[a] + v; 

从而另sum[x] = sum[b] - sum[a] + v; 而后更新sum[a].更新的部分放在了查找中.

具体分析,可以自己画个草图出来!

下面给出代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
using namespace std;

typedef long long ll;
const int maxn = 200005;

int root[maxn];
ll cnt[maxn];

int find(int x)
{
    if(x != root[x])
    {
        int fa = root[x];
        root[x] = find(root[x]);
        cnt[x] += cnt[fa];
    }
    return root[x];
}

void init(int n)
{
    memset(cnt,0,sizeof(cnt));
    for(int i = 0; i <= n; i++)
    {
        root[i] = i;
    }
}

int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        int ans = 0;
        init(n);
        int u, v, d;
        int roota,rootb;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &u, &v, &d);
            u-=1;
            roota = find(u);
            rootb = find(v);
            if(roota == rootb)
            {
                if(cnt[u] + d != cnt[v])
                    ans++;
            }
            else
            {
                root[rootb] = roota;
                cnt[rootb] = cnt[u] - cnt[v]  + d;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/80754494
今日推荐