您的位置:首页 > 其它

HDU-3038 How Many Answers Are Wrong

2017-04-07 09:58 399 查看
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.

【题意】有一个序列,输入是序列中区间[l,r]内的数字的和。输入m组l, r, 序列和,求这些和中有多少是错的。假设之前的都是对的,如果有错,忽略掉错误的,不作为后面的判断条件。

【解】有错的情况:从之前的条件中可以判断出[l, r]的区间和为s,但是输入的[l, r]的区间和不是s。

带权并查集。类似kruskal算法中判断有没有圈。以区间端点作为集合中的元素,区间和作为权。如果输入的l和r在同一个集合中,说明已经可以求出[l,r]内数的和,这时需要判断新输入的和跟之前的是否矛盾。带入权值算一下就可以了。

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

struct node {
int parent;
int rank;
int weight;
};

int Find(vector<node>& v, int x) {
vector<int> path;
vector<int> weights(1, 0);
int s = 0;
while (x != v[x].parent) {
path.push_back(x);
weights.push_back(v[x].weight);
s += v[x].weight;
x = v[x].parent;
}
for (int i = 0; i != path.size(); i++) {
v[path[i]].weight = s - weights[i];
s -= weights[i];
v[path[i]].parent = x;
}
return x;
}

void Union(vector<node>& v, int x, int y, int w) {
int xp = Find(v, x);
int yp = Find(v, y);
if (v[xp].rank > v[yp].rank) {
v[yp].parent = xp;
v[yp].weight = -w + v[x].weight - v[y].weight;
}
else {
v[xp].parent = yp;
v[xp].weight = w + v[y].weight - v[x].weight;
if (v[xp].rank == v[yp].rank)
v[yp].rank++;
}
}

int main() {
int n, m;
while (cin >> n >> m) {
vector&
9010
lt;node> v(n + 1);
for (int i = 0; i < n + 1; i++) {
v[i].parent = i;
v[i].rank = 1;
v[i].weight = 0;
}
int result = 0;
for (int i = 0; i < m; i++) {
int l, r; cin >> l >> r;
int w; cin >> w;
int lp = Find(v, l - 1);
int rp = Find(v, r);
if (lp != rp) {
Union(v, l - 1, r, w);
}
else {
if (v[l - 1].weight != v[r].weight + w) {
result++;
}
}
}
cout << result << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: