Gym 100096D Guessing game
Gym 100096D Guessing game
题面
Problem Description
Byteman is playing a following game with Bitman. Bitman writes down some 1 000 000 000-element
sequence of zeros and ones. Byteman’s task is to guess the sequence that Bitman created. He can achieve
this goal by asking Bitman questions of the form: ’What is the parity of the sum of a subsequence of your
sequence, that begins at the b-th element and ends at the e-th element of the sequence?’. After playing
the game for some time, Byteman started suspecting that Bitman was cheating. He would like to know
at which moment did Bitman first answer his question in an inconsistent way, so he asked you for help.
Write a program which:
reads a description of Byteman’s questions and Bitman’s answers,
computes the greatest number N, for which Bitman’s answers for the first N questions are consistent.
Input
The first line of the input file contains one integer n (0 ≤ n ≤ 100 000), denoting the number of Byteman’s
questions. Each of the following n lines describes one Byteman’s question with corresponding Bitman’s
answer in the form of three positive integers b, e and s (1 ≤ b ≤ e ≤ 1 000 000 000, s ∈ {0, 1}), separated
by single spaces. b and e are the indices of the first and the last element of the subsequence in Byteman’s
question. s = 0 means that Bitman answered that the sum was even and s = 1 — that it was odd.
Output
The first and only line of the output file should contain one integer — the greatest value of N such that
there exists a sequence of zeros and ones that is consistent with Bitman’s answers to first N Byteman’s
questions.
题意
给出一些关系,从l到r的1的个数为奇数还是偶数。问从哪组开始是矛盾的。
考虑一个点不是奇数就是偶数,用一个前缀和的形式表示到i为止的前面所有数i的个数是奇数还是偶数。将一个点拆成两个点。
如果一个点的奇数代表和偶数代表相连说明改组关系与之前的肯定矛盾。
若v==1,则将l-1的奇数代表和r的偶数代表相连,l-1的偶数代表和r的奇数代表相连,表示[l,r]中有改变奇偶性。
若v==1,则将l-1的奇数代表和r的奇数代表相连,l-1的偶数代表和r的偶数代表相连,表示[l,r]中未改变奇偶性。
考虑到b,e很大,用map离散化。
代码
#include <bits/stdc++.h>
using namespace std;
int n;
int fa[1000010];
int l[1000010];
int r[1000010];
int v[1000010];
int cnt;
map<int,int> f;
int ans;
int find(int k)
{
return fa[k]==k?k:fa[k]=find(fa[k]);
}
int main()
{
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
cin>>n;
for (int i=1;i<=n;i++)
{
cin>>l[i]>>r[i]>>v[i];
l[i]--;
if (!f[l[i]]) f[l[i]]=++cnt;
if (!f[r[i]]) f[r[i]]=++cnt;
}
for (int i=1;i<=cnt+cnt;i++) fa[i]=i;
for (int i=1;i<=n;i++)
{
int x=f[l[i]],y=f[r[i]];
if (v[i])
{
if (find(x)==find(y)) break;
ans++;
if (find(y+cnt)!=find(x)) fa[fa[x]]=fa[y+cnt];
if (find(x+cnt)!=find(y)) fa[fa[y]]=fa[x+cnt];
}
else
{
if (find(x)==find(y+cnt)) break;
ans++;
if (find(y)!=find(x)) fa[fa[x]]=fa[y];
if (find(x+cnt)!=find(y+cnt)) fa[fa[y+cnt]]=fa[x+cnt];
}
}
cout<<ans<<endl;
return 0;
}
题目链接
http://codeforces.com/gym/100096/attachments
Gym 100096D Guessing game的更多相关文章
- Codeforces Gym 100015G Guessing Game 差分约束
Guessing Game 题目连接: http://codeforces.com/gym/100015/attachments Description Jaehyun has two lists o ...
- 【容斥原理】【推导】【树状数组】Gym - 101485G - Guessing Camels
题意:给你三个1~n的排列a,b,c,问你在 (i,j)(1<=i<=n,1<=j<=n,i≠j),有多少个有序实数对(i,j)满足在三个排列中,i都在j的前面. 暴力求的话是 ...
- GCD Guessing Game Gym - 100085G 猜数字 gcd
http://codeforces.com/gym/100085/attachments 因为那个数字是一个质数,这样的猜的次数是最多的,所以至少是质数次. 但是如果需要猜2.3,那么可以直接猜6,也 ...
- Gym - 100085G - GCD Guessing Game
原题链接 题意一个数字x在1-n之间,现在猜数字,每次猜一个数字a,告知gcd(x, a)的答案,问最坏情况下需要猜几次 分析 考虑素数.当猜的数为一组素数的乘积时,就可以把这些素数都猜出来.那么答案 ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- 2632: [neerc2011]Gcd guessing game
2632: [neerc2011]Gcd guessing game Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 144 Solved: 84[S ...
随机推荐
- android的图片的初步学习理解
Android支持JPEG和PNG格式.GIF和BMP格式图片的支持. 图片最终要显示在屏幕上,都会对应一个屏幕上的点,即对应一个颜色值.不同格式的图片,只是不同压缩编码和解压算法. 也就是说,我们看 ...
- SQL update语句 更新和查询同一张表 冲突
#update 和 select在同一张表的时候会显示冲突 报错信息: [Err] 1093 - You can't specify target table 'tb_a' for update i ...
- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this
最近使用新版本的mysql,执行语句的时候报错.网上找了一下,解决方法如下: vim /etc/mysql/conf.d/mysql.cnf [mysqld] sql_mode=STRICT_TRAN ...
- rem初始化
使用css实现rem适配,头部引入即可 adaptive.js !function(e, t) { function i() { o = , e.devicePixelRatioValue = o, ...
- better-scroll使用总结
参考:https://zhuanlan.zhihu.com/p/27407024 better-scroll使用小结 核心就是这4个 <script> import BScroll fro ...
- 整理 oracle异常错误处理
5.1 异常处理概念 5.1.1 预定义的异常处理 5.1.2 非预定义的异常处理 5.1.3 用户自定义的异常处理 5.1.4 用户定义的异常处理 5.2 异常错误传播 5.2.1 在执行部分引发 ...
- PAT 1017 A除以B(20)(代码)
1017 A除以B(20 分) 本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: 输入在一 ...
- js 闭包 弊端
闭包有许多有趣的用途,Javascript的两个特征使它这么有趣:1. function是一个对象,它跟数组,Object一样,地位平等.2. Javascript变量作用域范围.<Javasc ...
- svn conflict问题解决办法
转自:http://www.cnblogs.com/aaronLinux/p/5521844.html 目录: 1. 同一处修改文件冲突 1.1. 解决方式一 1.2. 解决方式二 1.3. 解决总结 ...
- 20172306《Java程序设计与数据结构》第十周学习总结
20172306<Java程序设计>第十周学习总结 教材学习内容总结 本章主要的讲的是集合有关的知识: 1.集合与数据结构 - 集合是一种对象,集合表示一个专用于保存元素的对象,并该对象还 ...