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

本文介绍了一道经典算法题目HDU-3038的解决方案,通过使用带权并查集来解决一系列连续子序列求和的矛盾查询问题,详细解释了算法思路及实现细节。

How Many Answers Are Wrong HDU - 3038

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

题意:有编号为1-n的n个序列,给m个查询:x y s,每个查询表示区间[x,y]的和为s

问有多少个有矛盾的询问

题解(转自https://dilthey.cnblogs.com/):

经典的带权并查集问题;

我们设sum[x]是这n个数的前缀和数组,那么就可以把所有的 a+…+b = s 都变成 sum[b] - sum[a-1] = s;

那么我们就可以利用带权并查集进行进行建树,照例par[x]代表了节点x的父亲节点,而val[x]代表了sum[x] - sum[par[x]];

首先是find()函数,我们在原来最基础的并查集find函数中,是有一种类似于把x的父亲节点par[x]变成par[par[x]]这样的,将节点沿着树枝往上移动的操作,

那么相应的,我们每次做将par[x]变成par[par[x]]这样的操作时,val[x]也要修改为val[x]+val[par[x]];

其次,我们对于每次查询的结果a,b,s,令 t1 = find(a),t2 = find(b),那么对应的s,val[a],val[b],val[t2]就可以如下图的向量所示:

          

向量的方向相当重要(因为这个WA了两发= =):对于任何的从x指向y的向量都代表了sum[x] - sum[y];

那么很显然的有:

①若a,b还不是一个集合内,那么就要unite他们,即令par[t2]=t1;

 那么同时,根据向量的加减法,val[t2]也要跟随着par[t2]的变动,转变成sum[t2] - sum[t1];所以根据向量加减的规则就有val[t2] = - val[b] + s + val[a];

②若a,b已经是一个集合内的,那么就要判断是否有矛盾;

 显然此时t1=t2,那么如果s是正确的话,就应该有 - val[b] + s = val[a],否则,s就是一个与前面产生矛盾的、有错误的查询结果;

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
#define N 200010
#define nmax 6
#define inf 0x3f3f3f3f
int fa[N],sum[N]; //sum[x]表示从x到它的根(fa[x])的区间的和
int fin(int x)
{
    if(fa[x]==x)
        return x;
    int t=fa[x];
    fa[x]=fin(fa[x]);
/*因为sum[x]表示从x到它的根(fa[x])的区间的和,所以,当x的根发生改变时,
例如x的根原本是t=fa[x],即sum[x]原本表示[x,t]这段区间和,这里的x不一定比t大,现在x的根要指向fy,那么sum[x]要表示[x,fy]的区间和,则现在的sum[x]=sum[x]+sum[t];
    sum[x]+=sum[t];
    return fa[x];
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int x,y,s;
        int ans=0;
        memset(sum,0,sizeof(sum));
        for(int i=0; i<=n; i++)
            fa[i]=i;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&x,&y,&s);
            x--;
            int fx=fin(x);
            int fy=fin(y);
            if(fx!=fy)
            {
                fa[fy]=fx;
                sum[fy]=-sum[y]+s+sum[x];
                /*(1)
                fa[fx]=fy;
                sum[fx]=-sum[x]+s+sum[y];
                */
            }
            else if(sum[y]!=sum[x]+s) 
                ans++;
            /*(2)
            对应(1)处的if语句 
            else if(sum[x]!=sum[y]+s) 
                ans++;
                */
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值