题意

Language:Default
Parity game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13833 Accepted: 5303

Description

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.



You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

Input

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either `even' or `odd' (the answer, i.e. the parity of the number of ones in the chosen subsequence, where `even' means an even number of ones and `odd' means an odd number).

Output

There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

Sample Input

10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

Sample Output

3

Source

分析

如果用sum表示序列S的前缀和,那么在每个回答中:

  1. S[l~r]有偶数个1,等价于sum[l-1]和sum[r]奇偶性相同。
  2. S[l~r]有奇数个1,等价于sum[l-1]和sum[r]奇偶性不同。

那么离散化后使用边带权并查集即可。带权并查集使用异或值来表示奇偶性的关系。

时间复杂度\(O(m \log m)\)

另外把每个节点拆点,使用扩展域并查集也可以做。具体是把每个点拆成奇数和偶数点,使用2-SAT那样的连通关系即可。

代码

#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std; co int N=5e3+1;
struct {int l,r,ans;}query[N];
int a[2*N],fa[2*N],d[2*N],n,m;
void read_discrete(){
read<int>(),read(m);
for(int i=1;i<=m;++i){
static char str[5];
read(query[i].l),read(query[i].r);
scanf("%s",str),query[i].ans=str[0]=='o'?1:0;
a[++n]=query[i].l-1,a[++n]=query[i].r;
}
sort(a+1,a+n+1),n=unique(a+1,a+n+1)-a-1;
}
int get(int x){
if(x==fa[x]) return x;
int root=get(fa[x]);
d[x]^=d[fa[x]];
return fa[x]=root;
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
read_discrete();
for(int i=1;i<=n;++i) fa[i]=i;
for(int i=1,x,y,p,q;i<=m;++i){
x=lower_bound(a+1,a+n+1,query[i].l-1)-a;
y=lower_bound(a+1,a+n+1,query[i].r)-a;
p=get(x),q=get(y);
if(p==q){
if((d[x]^d[y])!=query[i].ans)
return printf("%d\n",i-1),0;
}
else fa[p]=q,d[p]=d[x]^d[y]^query[i].ans;
}
return printf("%d\n",m),0;
}

POJ1733 Parity game的更多相关文章

  1. POJ1733 Parity game 【扩展域并查集】*

    POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...

  2. POJ1733 Parity game 【带权并查集】*

    POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...

  3. poj1733 Parity Game(扩展域并查集)

    描述 Now and then you play the following game with your friend. Your friend writes down a sequence con ...

  4. POJ1733 Parity game —— 种类并查集

    题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  5. Poj1733 Parity Game(带权并查集)

    题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...

  6. poj1733 Parity game[带权并查集or扩展域]

    地址 连通性判定问题.(具体参考lyd并查集专题该题的转化方法,反正我菜我没想出来).转化后就是一个经典的并查集问题了. 带权:要求两点奇偶性不同,即连边权为1,否则为0,压缩路径时不断异或,可以通过 ...

  7. [POJ1733]Parity game(并查集 + 离散化)

    传送门 题意:有一个长度已知的01串,给出[l,r]这个区间中的1是奇数个还是偶数个,给出一系列语句问前几个是正确的 思路:如果我们知道[1,2][3,4][5,6]区间的信息,我们可以求出[1,6] ...

  8. POJ-1733 Parity game(带权并查集区间合并)

    http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. Mac下使用源码编译安装TensorFlow CPU版本

    1.安装必要的软件 1.1.安装JDK 8 (1)JDK 8 can be downloaded from Oracle's JDK Page: http://www.oracle.com/techn ...

  2. Tree CodeForces - 1111E (树,计数,换根)

    大意: 给定树, 多组询问, 每个询问给出一个点集$S$, 给定$m, r$, 求根为$r$时, $S$的划分数, 满足 每个划分大小不超过$m$ 每个划分内不存在一个点是另一个点的祖先 设点$x$的 ...

  3. Buy Low Sell High CodeForces - 867E (思维,贪心)

    大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不 ...

  4. jenkins X 和k8s CI/CD

    架构 二.核心组件 1.包管理工具     1.1.helm工具包    https://github.com/helm/helm      1.2.Chartmuseum开源helm chart仓库 ...

  5. hdu-3276-dp+二分+单调队列

    Star Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. POJ-3744 Scout YYF I (矩阵优化概率DP)

    题目大意:有n颗地雷分布在一条直线上,有个人的起始位置在1,他每次前进1步的概率为p,前进两步的概率为1-p,问他不碰到地雷的概率. 题目分析:定义状态dp(i)表示到了dp(i)的概率,则状态转移方 ...

  7. PHP 的 HTTP 认证机制

    PHP 的 HTTP 认证机制仅在 PHP 以 Apache 模块方式运行时才有效,因此该功能不适用于 CGI 版本.在 Apache 模块的 PHP 脚本中,可以用 header()函数来向客户端浏 ...

  8. splunk 通过rest http导入数据

    使用 HTTP Event Collector go to Settings > Data inputs > HTTP Event Collector. Then click the Gl ...

  9. 快速切题 acdream手速赛(6)A-C

    Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...

  10. 51nod1482

    题解: 发现是一个环,而环的题目有一些就是要转化成为链 首先找到一个最高点,中间断开 然后当作一条链来做 代码: #include<cstdio> #include<algorith ...