D - How Many Answers Are Wrong HDU - 3038【带权并查集】
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
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)
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.
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
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【带权并查集】的更多相关文章
- HDU - 3038 带权并查集
这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...
- hdu 3038带权并查集
#include<stdio.h> #include<string.h> #define N 200100 struct node { int x,count; }pre[N ...
- 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 ...
- hdu 1829 带权并查集的运用类似于食物链但是更简单些
#include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...
- Zjnu Stadium HDU - 3047 带权并查集板子题
#include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...
- 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 ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
- 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s
这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱 ...
- hdu3038How Many Answers Are Wrong(带权并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...
- HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...
随机推荐
- 单元测试-xUnit总结
xUnit总结 什么是xUnit xUnit.net是针对.NET Framework的免费,开源,以社区为中心的单元测试工具. 自动化测试的优点 可以频繁的进行测试 可以在任何时间进行测试,也可以按 ...
- 面试官:你用过mysql哪些存储引擎,请分别展开介绍一下
这是高级开发者面试时经常被问的问题.实际我们在平时的开发中,经常会遇到的,在用SQLyog等工具创建表时,就有一个引擎项要你去选.如下图: Mysql的存储引擎有这么多种,实际我们在平时用的最多的莫过 ...
- pikachu-SQL注入漏洞
一.SQL Inject 漏洞原理概述 1.1 什么是数据库注入漏洞 数据库注入漏洞,主要是开发人员在构建代码的时候,没有对用户输入的值的边界进行安全的考虑,导致攻击者可以通过合法的输入点提交 ...
- findContours()函数
函数原型 findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int m ...
- 哈尔滨工业大学(深圳)本科毕业设计(论文)LaTeX模板:hitszthesis
目录 本篇文章的主要内容如下: 目录 引言 Why hitszthesis? 下载方式 编译方式简介 模板说明文档(用户手册) 毕业论文撰写样例 后记 引言 去年发布过哈深本科毕业设计(论文)的LaT ...
- Spring Mvc Http 400 Bad Request问题排查
如果遇到了Spring MVC报错400,而且没有返回任何信息的情况下该如何排查问题? 问题描述 一直都没毛病的接口,今天测试的时候突然报错400 Bad Request,而且Response没有返回 ...
- LNMP环境配置(2)
php-fpm配置,Nginx配置 Nginx配置 默认虚拟主机 修改主配置文件 # vi /usr/local/nginx/conf/nginx.conf 在最后 } 符号上面写入 includ ...
- C语言中File的应用
#C语言中规定我们使用文件必须初始化一个文件指针 FILE* pfile = NULL; #以a+追加的方式打开文件 返回值0为打开成功 int fp = fopen_s(&pfile, &q ...
- 为什么要进行初始化(C语言)
答案:是为了清除被释放的内存中保存的以前程序中留下的垃圾数据.
- AQS源码分析总结
AQS是并发编程的一个最基本组件,是一个抽象同步器. 网上有很多详细介绍AQS的博文,在这里我就不仔细介绍了,主要写一些重要的内容. AQS中重要的几个属性: //同步队列的头节点 private t ...