hdu3047 Zjnu Stadium && HDU 3038 How Many Answers Are Wrong (带权并查集)

How Many Answers Are Wrong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3855    Accepted Submission(s): 1471


Problem Description
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
 

 

Source
 

 

Recommend
gaojie
 
题意:有1~N个点,有M次询问,每次询问描述的是区间[l,r]的和是val,求所有描述中有多少个是错误的。
分析:并查集
 
w[i]表示编号为i的点到当前根节点的距离
如果要计算区间的和话,会处理成sum=w[r]-w[l-1],所以这里要处理成左开右闭区间
 
合并A,B时,假设A,B属于不同的树,那么就要合并这两棵树, 把A树合并到B树上,这时要给A树的跟结点root_a赋值,关键是给root_a附上一个什么值?由于A点和B点的权值w[A]和w[B]都是相对跟结点的距离,所以分析A,B之间的相对距离,可以得到w[root_a] = w[A]+x-w[B]。 注意到这时,对于原来的A的树,只跟新了root_a跟结点的权值, 那么其它结点的跟新在查找的那一步里面实行了。
因为w[]保存的是到根节点的距离,由于根节点的权值改变了,所以递归中每个点到根节点的值也会改变
 
用向量表示法来得到公式w[root_a] = w[A]+val-w[B]
 
                 w[A] + val - w[B]
root_a<---------------------------root_b
                                 
     ↑ w[A]                              w[B]
 
     A    <---------------------------   B
                             val
 
HDU 3047 和这题几乎就是同一道题= =
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=200000+5;
int n,m;
int ans;
int w[MAXN];
int p[MAXN];

void init()
{
    for(int i=1;i<=n;i++)
    {
        p[i]=i;
        w[i]=0;
    }
}

int findfa(int x)
{
    if(p[x]==x) return x;
    int tmp=p[x];
    p[x]=findfa(p[x]);
    w[x]+=w[tmp];
    return p[x];
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        init();
        int u,v,val;
        ans=0;

        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&u,&v,&val);
            u--;

            int x=findfa(u);
            int y=findfa(v);
            if(x!=y)
            {
                p[y]=x;
                w[y]=w[u]-w[v]+val;
            }
            else
            {
                if(w[v]-w[u]!=val)
                    ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2015-08-01 10:18  Cliff Chen  阅读(330)  评论(0编辑  收藏  举报