参考博客

How Many Answers Are Wrong

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
 

思路:

这题完全没想到如何用并查集来解

总结:

sum[i]的意义是  点i   与 点i+1   中间的点到根点的距离,巧妙使用递归寻路,更新sum与父节点
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int boss[],sum[];
int fin(int x)
{
if(boss[x]==x)return x;
int fx=boss[x];
boss[x]=fin(boss[x]);
sum[x]+=sum[fx];
return boss[x];
}
int main()
{
int n,m,a,b,c,fa,fb,ans=;
while(cin>>n>>m)
{ans=;
for(int i=;i<=n;i++)boss[i]=i,sum[i]=;//初始化boss与sum
for(int i=;i<m;i++)
{
scanf("%d %d %d",&a,&b,&c);
fa=fin(a-);fb=fin(b);//寻找根,并且更新sum值
if(fa==fb)
{
if(sum[a-]-sum[b]!=c)ans++;//a-1与b之间有(a,a+1,a+2...b)
}
else
{
boss[fa]=fb;
sum[fa]=-sum[a-]+sum[b]+c;
}
}
cout<<ans<<endl;
}
return ;
}

hdu 3038 给区间和,算出多少是错的的更多相关文章

  1. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733

    1.POJ 1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5744   Accepted: ...

  2. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  3. hdu 3038 How Many Answers Are Wrong(并查集的思想利用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:就是给出n个数和依次m个问题,每个问题都是一个区间的和,然后问你这些问题中有几个有问题,有 ...

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

    传送门 Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, ...

  5. 一次 Oracle 算出运算溢出问题 排查解决 (并非除数为零!)

    前段时间 出现过这个问题,: 表中有一列为number类型 rec_recordlength (两个时间的间隔长度/秒) 部分数据 统计这个字段就会出现 "算出运算溢出" 错误,很 ...

  6. javascript基础程序(算出一个数的平方值、算出一个数的阶乘、输出!- !- !- !- !- -! -! -! -! -! 、函数三个数中的最大数)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 关于一道面试题【字符串 '1 + (5 - 2) * 3',怎么算出结果为10,'eval'除外】

    最近徘徊在找工作和继续留任的纠结之中,在朋友的怂恿下去参加了一次面试,最后一道题目是: 写一个函数,输入一个字符串的运算式,返回计算之后的结果.例如这样的: '1 + (5 - 2) * 3',计算出 ...

  8. [转]JAVA 根据经纬度算出附近的正方形的四个角的经纬度

    csv文件转化为geojson文件中,涉及到路测图的打点生成,打点是由一个个正方形组成,而正方形是由四个点组成的,这四个点根据经纬度和范围生成,具体的实现代码是从网上找来的: /** * * @par ...

  9. JavaScript如何根据当天算出前三天和后三天

    经杨秀徐批准 中央军委颁发意见建设新型司令机关news 杨秀徐会见到北京述职的香港特首梁振英news 海军372潜艇官兵先进事迹报告会举行 杨秀徐作指示news 中央农村工作会议在京召开 李克强作重要 ...

随机推荐

  1. CentOS7:搭建配置SVN服务器

    1. 安装 CentOS通过yum安装subversion. $ sudo yum install subversion subversion安装在/bin目录: $ which svnserve / ...

  2. Python基础知识:模块

    目录 JSON模块&pickle模块 requests模块 time模块 datetime模块 logging模块 os模块 sys模块 hashlib模块 re模块.正则表达式 config ...

  3. 2.1Python数据处理篇之---内建有关数学的函数

    目录 目录 前言 (一)数学相关得内建函数 (二)具体演示 1.求绝对值 2.创建一个复数 3.求商和余数 4.求x得y次幂 5.生成一个序列 6.四舍五入 7.对一个集合求和 8.求最大值 9.求最 ...

  4. Deepin中设置文件或文件夹权限

    Deepin中设置文件或文件夹权限 -R 递归进行某项操作,不论是删除文件夹或者修改文件夹下所有文件权限   权限更改,777相当于完全控制权限: 更改一个文件夹或文件的权限:chmod 777 文件 ...

  5. Java设计模式之十一 ---- 策略模式和模板方法模式

    前言 在上一篇中我们学习了行为型模式的访问者模式(Visitor Pattern)和中介者模式(Mediator Pattern).本篇则来学习下行为型模式的两个模式,策略模式(Strategy Pa ...

  6. HEU预热赛

    A题: 一道dp的题目 dp[i][j] = k 代表 i行放j个棋子有k中可能 dp[i][j] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2] +...dp[i-1] ...

  7. sql server2008 R2打开报错:无法识别的配置节 system.serviceModel解决办法分享

    本人是先安装的sql server2008 R2成功可以运行后,再安装VS2010成功后,再打开sql server2008,就出现以下错误,无法连接服务器.   无法识别的配置节 system.se ...

  8. HDU 2865 Birthday Toy

    题目链接 题意:n个小珠子组成的正n边形,中间有一个大珠子.有木棍相连的两个珠子不能有相同的颜色,旋转后相同视为相同的方案,求着色方案数. \(\\\) 先选定一种颜色放在中间,剩下的\(k-1\)种 ...

  9. 开启 J2EE(一)—‘全明星队伍’

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/akkzhjj/article/details/27973427 J2EE-一套规范 J2EE(Jav ...

  10. Ubuntu 14.04服务器配置 (1) 安装和配置

    http://jingyan.baidu.com/article/9c69d48fb9fd7b13c8024e6b.html ssh是一种安全协议,主要用于给远程登录会话数据进行加密,保证数据传输的安 ...