当前位置:网站首页>D - How Many Answers Are Wrong
D - How Many Answers Are Wrong
2022-07-06 06:13:00 【RCyyds】
Portal :How Many Answers Are Wrong
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.
BoringBoringa 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
The question : Tell you m Weights of intervals , Find out the number of weights of the wrong interval . Be careful , Before finding out the weight between obvious errors , They all think that everything from the previous one to the previous error interval is right .
Ideas : It is a classic topic of interval and search set , First contact with this type of topic , mentally . After reading some people's blogs, I understand .
First sum Array is the core of this problem , You can understand it as the weight from this node to its ancestor node , If it is understood as an interval , It's the left closed and right closed interval , The left end point of the interval is the node , The right endpoint is its ancestor node .
Here is a schematic diagram :
among B yes A The ancestor node of .
Next, back to the topic itself , When the ancestor nodes of two nodes are the same , It means they are in a range , We assume that b Below ,a On the top , Then it's judgment sum[b]-sum[a] Is it equal to s 了 , It's not equal to ans++;
Here is a schematic diagram :
because sum It is a left closed and right closed interval , therefore a One less , such sum[b]-sum[a] Namely [a,b] The weight of this interval .
When the ancestor nodes of two nodes are different , The schematic diagram is as follows :
We first make pa Become pb The ancestor node of , because sum[a],sum[b] All in find It is found in the function ,s It is known that ,sum[pb] You can find out ,sum[pb]=s+sum[a]-sum[b];
See code for details .
Code :
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=2e5+5;
int f[N],sum[N];//f[i] Express i Father ,sum[i] Express i This point to f[i] The sum between this point
int find(int x)
{
if(x!=f[x])
{
int pre=f[x];
f[x]=find(f[x]);// Path compression , Make all nodes point to the root node ,
sum[x]+=sum[pre];// The reason for adding here is that after all the parts are updated, it's good to update here at one time
}
return f[x];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(sum,0,sizeof(sum));
int ans=0;
for(int i=1;i<=n;i++)
f[i]=i;
while(m--)
{
int a,b,s;
scanf("%d%d%d",&a,&b,&s);
a--;
int pa=find(a);
int pb=find(b);
if(pa==pb)
{
if(sum[b]-sum[a]!=s)
ans++;
}
else{
f[pb]=pa;
sum[pb]=s+sum[a]-sum[b];
}
}
printf("%d\n",ans);
return 0;
}
边栏推荐
- [postman] test script writing and assertion details
- [postman] collections configuration running process
- Idea new UI usage
- 一文揭开,测试外包公司的真 相
- 10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
- Company video accelerated playback
- Web界面元素的测试
- 黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
- GTSAM中ISAM2和IncrementalFixedLagSmoother说明
- Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
猜你喜欢
【微信小程序】搭建开发工具环境
ContentType的作用
使用Nacos管理配置
Nodejs realizes the third-party login of Weibo
[Thesis code] SML part code reading
误差的基本知识
Investment strategy discussion and market scale prediction report of China's solid state high power amplifier industry from 2022 to 2028
功能安全之故障(fault),错误(error),失效(failure)
【Postman】Monitors 监测API可定时周期运行
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
随机推荐
[postman] collections - run the imported data file of the configuration
Configuring OSPF GR features for Huawei devices
VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
selenium源码通读·9 |DesiredCapabilities类分析
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
Interface test: what are the components of the URL in fiddler
功能安全之故障(fault),错误(error),失效(failure)
H3C S5820V2_ Upgrade method after stacking IRF2 of 5830v2 switch
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
二维码的前世今生 与 六大测试点梳理
通过修改style设置打印页样式
Application of Lie group in gtsam
(中)苹果有开源,但又怎样呢?
The latest 2022 review of "graph classification research"
A complete collection of necessary learning websites for office programmers
Seven imperceptible truths in software testing
还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
对数据安全的思考(转载)
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)