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

最新文章

  1. Rendering Paths
  2. [linux]记录如何设置一个新的vps
  3. 写给IOS开发工程师的网页前端入门笔记
  4. bt协议详解 DHT篇(上)
  5. TabLayout
  6. Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)
  7. Boost简介
  8. linux内核申请内存函数
  9. Spring Boot 部署与服务配置
  10. vmware时间不同步的问题
  11. 51Nod 1509加长棒
  12. UML系列图2
  13. android系统中如何通过程序打开某个AccessibilityService
  14. bzoj5107: [CodePlus2017]找爸爸
  15. 转:Python: 什么是*args和**kwargs
  16. TypeScript基础类型,类实例和函数类型声明
  17. Django之模板
  18. jQuery -- 光阴似箭(五):AJAX 方法
  19. 【html5】解决HTML5新标签不兼容的问题
  20. 微信小程序 如何获取用户code

热门文章

  1. php 通过stomp协议连接ActiveMQ
  2. [z]eclipase优化
  3. mysql lost connection to server during query
  4. linux 输出重定向
  5. c++计时
  6. 常用的TCP Option
  7. Linux下查看系统启动 、运行以及安装时间
  8. 洛谷1993 小K的农场
  9. Convert 实现 pdf 和图片格式互转
  10. tomcat 启动卡住不动的原因