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]的区间和为 ...
随机推荐
- Linux学习小记(1)---nm*ip
注意在CentOS7中ifconfig等命令已经被ip取代,ip的功能很强大,而NetworkManager系列命令(nmcli nmtui等)可以用于配置网络连接
- java设计模式 - 单例模式(干货)
深度讲解23种设计模式,力争每种设计模式都刨析到底.废话不多说,开始第一种设计模式 - 单例. 作者已知的单例模式有8种写法,而每一种写法,都有自身的优缺点. 1,使用频率最高的写法,废话不多说,直接 ...
- ES6 - 报错整理(1): Unexpected end of JSON input while parsing near '...es":"7.0.0-alpha.11",'
npm install --save-dev 安装 babel-preset-env时一直报错 Unexpected end of JSON input while parsing near '... ...
- Centos7没有IP地址,查看网络状态显示No suitable device found for this connection (devint match))
今天打开虚拟机,使用 ifconfig 命令时,没有显示出 IP 地址 (更好的阅读体验可访问 这里 ) 使用 systemctl status network 命令查看网络状态 显示没有合适的网络装 ...
- Android Vitamio初探
GitHub: https://github.com/yixia/VitamioBundle 1.下载完毕导入用Android Studio打开 2.新建Mode,引入依赖 dependencies ...
- system.run
客户端开启了remotecommand后可以在server调用该命令在agent上执行一些命令 命令中有逗号 zabbix_get -s xxx.xxx.xxx.xxx -k "system ...
- Python中verbaim标签使用详解
verbatim标签:默认在"DTL"模板中是会去解析那些特殊字符串的,比如{% 和 %}以及{{等.如果你在某个代码片段中不想使用"DTL"的解析引擎,那么就 ...
- HTML连载66-过度模块的连写、弹性效果
一.过渡模块的连写 1.过渡连写格式: 过渡属性 过渡时长 运动速度 延迟时间: 2.过渡连写注意点: (1)和分开写一样,如果想要多个属性添加过渡效果,也是使用逗号来隔开即可. (2)连写的时 ...
- 三星正在改善1Gb MRAM寿命问题
据报道三星已经成功研发出有望替代嵌入式闪存存储器(eFlash)的嵌入式磁阻随机访问内存(eMRAM),容量为1Gb,测试芯片的优良率已达90%. 随着5G物联网时代的来临,存储器领域发展快速,而在这 ...
- 搜索练习题LETTERS
题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1212 或者http://poj.org/problem?id=1154 题目描述: 给 ...