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 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.

BoringBoringa 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)

输入

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.

输出

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

样例

输入

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

输出

1

思路

刚开始想不到这道题用并查集应该怎么写,后来看了别人的题解才明白原来并查集还可以这么用。

这道题让我们找出冲突的个数,证明冲突是很简单的,因为只有在所给区间我们恰好都知道的情况下(比如我们知道1-10前缀和是50,1-5前缀和是20,当给出的Ai,Bi为6,10时,我们才能够验证这组数据是否正确,如果Ai不为6,我们则无法进行验证,也无法证明它是错误的,只能将其当做正确答案来处理)才能证明是否冲突,否则全部认为正确并进行更新

pre[i]表示i的前缀和

f[i]表示已知的i所在区间的开始位置(语言描述能力太烂,可以看一下图)

u、v表示x、y
fu、fv表示f[u]、f[v]

图一:

图一

图二:

图二

图一图二所表示的情况便是我们无法验证是否冲突的情况,这种情况我们只能进行更新。

代码

 1 /*
 2  * =================================================================
 3  *
 4  *       Filename:  D.cpp
 5  *
 6  *           Link:  http://acm.hdu.edu.cn/showproblem.php?pid=3038
 7  *
 8  *        Version:  1.0
 9  *        Created:  2018/09/18 19时47分44秒
10  *       Revision:  none
11  *       Compiler:  g++
12  *
13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
14  *   Organization:  QLU_浪在ACM
15  *
16  * =================================================================
17  */
18 #include <bits/stdc++.h>
19 using namespace std;
20 #define clr(a, x) memset(a, x, sizeof(a))
21 #define rep(i,a,n) for(int i=a;i<=n;i++)
22 #define pre(i,a,n) for(int i=n;i>=a;i--)
23 #define ll long long
24 #define max3(a,b,c) fmax(a,fmax(b,c))
25 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
26 const double eps = 1e-6;
27 const int INF = 0x3f3f3f3f;
28 const int mod = 1e9 + 7;
29 const int N = 200100;
30 int f[N],pre[N];
31 int n,m,pr;
32 int x,y,sum;
33 
34 int find(int x)
35 {
36     int t = f[x];
37     if(f[x] != x)
38     {
39         f[x] = find(f[x]);
40         pre[x] += pre[t];
41     }
42     return f[x];
43 }
44 
45 int main()
46 {
47     ios
48 #ifdef ONLINE_JUDGE 
49 #else 
50         freopen("in.txt","r",stdin);
51     // freopen("out.txt","w",stdout); 
52 #endif
53     while(scanf("%d %d",&n,&m)!=EOF)
54     {
55         pr = 0;
56         rep(i,0,n)
57             f[i] = i;
58         clr(pre,0);
59         rep(i,1,m)
60         {
61             scanf("%d %d %d",&x,&y,&sum);
62             x -= 1;   
63             // x-y的和为sum  sum = (x+(x+1)+···+(y-1)+y)
64             // 为此,x应该-1
65             int fx = find(x);
66             int fy = find(y);
67             if(fx == fy && pre[x]+sum != pre[y])   
68             // 只有这种情况才能证明这个情况是错误的
69             {
70                 
71                 pr++;
72             }
73             else 
74             {
75                 if(fx < fy)
76                 {
77                     f[fy] = fx;
78                     pre[fy] = pre[x]-pre[y]+sum;
79                     // 为什么这样写可以看图一
80                 }
81                 else 
82                 {
83                     if(fx > fy)
84                     {
85                         f[fx] = fy;
86                         pre[fx] = pre[y]-pre[x]-sum;
87                         // 图二
88                     }
89                 }
90             }
91         }
92         printf("%d
",pr);
93     }
94     fclose(stdin);
95     // fclose(stdout);
96     return 0;
97 }
原文地址:https://www.cnblogs.com/duny31030/p/14305028.html