How Many Answers Are Wrong

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

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   |   We have carefully selected several similar problems for you:  3033 3035 3036 3037 3031 
 

Statistic | Submit | Discuss | Note

题意

思路

CODE

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <assert.h>
#include <vector> #define dbg(x) cout << #x << "=" << x << endl using namespace std;
typedef long long LL;
const int maxn = 4e5 + ; int fa[maxn];
int n,m,ans,cnt;
int sum[maxn];
//vector <int> v; void init()
{
for(int i = ; i <= maxn; i++) {
fa[i] = i;
}
} int fid(int x)
{
if(x != fa[x]) {
int r = fa[x];
fa[x] = fid(fa[x]);
sum[x] += sum[r];
}
/*int i,j;///路径压缩
i = x;
while(fa[i] != r) {
j = fa[i];
fa[i] = r;
i = j;
}
return r;*/
return fa[x];
} void join(int r1, int r2, int x)///合并
{
int fidroot1 = fid(r1), fidroot2 = fid(r2);
if(fidroot1 == fidroot2) {
if(sum[r1] - sum[r2] != x) {
++ans;
}
}
else {
fa[fidroot1] = fidroot2;
sum[fidroot1] = sum[r2] - sum[r1] + x;
}
} template<class T>inline void read(T &res)
{
char c;T flag=;
while((c=getchar())<''||c>'')if(c=='-')flag=-;res=c-'';
while((c=getchar())>=''&&c<='')res=res*+c-'';res*=flag;
} namespace _buff {
const size_t BUFF = << ;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
if (ib == ie) {
ib = ibuf;
ie = ibuf + fread(ibuf, , BUFF, stdin);
}
return ib == ie ? - : *ib++;
}
} int qread() {
using namespace _buff;
int ret = ;
bool pos = true;
char c = getc();
for (; (c < '' || c > '') && c != '-'; c = getc()) {
assert(~c);
}
if (c == '-') {
pos = false;
c = getc();
}
for (; c >= '' && c <= ''; c = getc()) {
ret = (ret << ) + (ret << ) + (c ^ );
}
return pos ? ret : -ret;
} int main()
{
while(scanf("%d %d",&n,&m) != EOF) {
init();
memset(sum, , sizeof(sum));
ans = ;
for(int i = ; i <= m; ++i) {
int a,b,x;
scanf("%d %d %d",&a, &b, &x);
a -= ;
join(a,b,x);
}
cout << ans << endl;
}
return ;
}

D - How Many Answers Are Wrong HDU - 3038【带权并查集】的更多相关文章

  1. HDU - 3038 带权并查集

    这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...

  2. hdu 3038带权并查集

    #include<stdio.h> #include<string.h> #define N  200100 struct node { int x,count; }pre[N ...

  3. HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...

  4. hdu 1829 带权并查集的运用类似于食物链但是更简单些

    #include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...

  5. Zjnu Stadium HDU - 3047 带权并查集板子题

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...

  6. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  7. HDU3038 How Many Answers Are Wrong —— 带权并查集

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...

  8. 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s

    这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱 ...

  9. hdu3038How Many Answers Are Wrong(带权并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...

  10. HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...

随机推荐

  1. 深入源码分析SpringMVC执行过程

    本文主要讲解 SpringMVC 执行过程,并针对相关源码进行解析. 首先,让我们从 Spring MVC 的四大组件:前端控制器(DispatcherServlet).处理器映射器(HandlerM ...

  2. 使用PropTypes进行类型检查

    原文地址 1.组件特殊属性——propTypes 对Component设置propTypes属性,可以为Component的props属性进行类型检查. import PropTypes from ' ...

  3. LOJ #2877. 「JOISC 2014 Day2」交朋友 并查集+BFS

    这种图论问题都挺考验小思维的. 首先,我们把从 $x$ 连出去两条边的都合并了. 然后再去合并从 $x$ 连出去一条原有边与一条新边的情况. 第一种情况直接枚举就行,第二种情况来一个多源 bfs 即可 ...

  4. cf1280B

    题意:给出一个n*m的矩阵,矩阵中的元素要么P要么是A 每次可以选择一个的子矩形,然后将矩阵中每一行都变为第一行,或者将矩阵中每一列都变为第一列 要求用最少的次数将矩阵中所有元素都变成A 题解:分类讨 ...

  5. You are my great sunshine

    "何为孤寂?" "清风,艳日,无笑意." "可否具体?" "左拥,右抱,无情欲." "可否再具体?" ...

  6. LeetCode 面试题18. 删除链表的节点

    题目链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/ 给定单向链表的头指针和一个要删除的节点的值,定义一 ...

  7. 两行配置完全解放gradle编译慢问题

    Android Studio编译经常出现gradle编译缓慢甚至超时问题,抛开电脑硬件配置不说,主要问题还是国内网络环境的因素影响,可以通过修改项目根目录下的build.gradle文件如下: bui ...

  8. 更改 Solution (.Sln) file

    Microsoft Visual Studio 2010 的项目为件改为Microsoft Visual Studio 2015默认打开 2010 的Solution (.Sln) file Micr ...

  9. 洛谷题解 P1744 【采购特价商品】

    原题传送门 题目描述 中山路店山店海,成了购物狂爱与愁大神的"不归之路".中山路上有n(n<=100)家店,每家店的坐标均在-10000~10000之间.其中的m家店之间有通 ...

  10. 【读书笔记】自然语言处理综述 -- 第四章 -- N元语法

    第四章 N元语法 本章开篇的两句话很有意思,代表了当时两个学派的思想和矛盾. 一句是"有史以来最伟大的语言学家"乔姆斯基说的:"句子的概率,在任何已知的对于这个术语的解释 ...