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[b] - sum[a - 1], a - 1会到0,所以反过来b要+1

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <map> const int MAXN = + ;
const int MAX = + ;
using namespace std;
struct DATA{
int num, order;
bool operator < (DATA b)const{
return num < b.num;
}
}data[MAXN * ];//data记录的是出现过的数字
struct DATA2{
int a, b, type;//表示sum[b] - sum[a - 1]的是否是同一类型
}data2[MAXN];//直接的输入数据
int parent[MAXN * ];
int val[MAXN * ]; int n, q, cnt; void init(){
scanf("%d%d", &n, &q);
for (int i = ; i <= q; i++){
char str[];
scanf("%d%d", &data2[i].a, &data2[i].b);
data[i * - ].num = data2[i].a; data[i * - ].order = i * - ;
data[i * ].num = data2[i].b; data[i * ].order = i * ;
scanf("%s", str);
//odd是奇数,even是偶数
if (str[] == 'o') data2[i].type = ;
else if (str[] == 'e') data2[i].type = ;
}
//准备离散化
sort(data + , data + + * q);
cnt = ;//记录数字的
data[].num = -;
for (int i = ; i <= * q; i++){
if (data[i].num != data[i - ].num) ++cnt;
if (data[i].order % == ) data2[((data[i].order - ) / ) + ].a = cnt;
else data2[((data[i].order - ) / ) + ].b = cnt;
}
/*for (int i = 1;i <= q; i++){
printf("%d %d %d\n", data2[i].a, data2[i].b, data2[i].type);
}*/
}
int find(int x){
int tmp = , f = x;
//注意要加上路径压缩
while (x != parent[x]){
tmp += val[x];
x = parent[x];
}
parent[f] = x;
val[f] = tmp % ;
return x;
} void work(){
//val[i] = 1则与父亲的奇偶性不同,0则相同
for (int i = ; i <= cnt; i++){
parent[i] = i;
val[i] = ;
}
for (int i = ; i <= q; i++){
int x = data2[i].a, y = data2[i].b ,t = data2[i].type;
y++;
if (i == )
printf("");
int xx = find(x), yy = find(y), flag = ;
if (xx == yy){//同根
if (t == ){//需要相同
if (val[x] != val[y]) flag = ;
//相同则躲过一劫
}else{//需要不同
if (val[x] == val[y]) flag = ;
}
}else{//不管怎么样都要合并
if (t == ){
parent[xx] = y;
val[xx] = (-val[x] + ) % ;
}else{
parent[xx] = y;
val[xx] = (-val[x] + ) % ;
}
}
if (flag == ) {printf("%d\n", i - );return;}
}
printf("%d\n", q);
} int main(){
int T;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
work();
return ;
}

 

【POJ1733】【带标记并查集】Parity game的更多相关文章

  1. poj1733(种类并查集+离散化)

    题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...

  2. 【POJ1417】【带标记并查集+DP】True Liars

    Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was ...

  3. poj1733(并查集+离散化)

    题目大意:有一个长度为n的0,1字符串, 给m条信息,每条信息表示第x到第y个字符中间1的个数为偶数个或奇数个, 若这些信息中第k+1是第一次与前面的话矛盾, 输出k; 思路:x, y之间1的个数为偶 ...

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

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

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

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

  6. POJ1733:Parity Game(离散化+带权并查集)

    Parity Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12853   Accepted: 4957 题目链接 ...

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

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

  8. 【poj1733】Parity game--边带权并查集

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15776   Accepted: 5964 Description Now ...

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

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

随机推荐

  1. 使用VisualStudio进行单元测试之三

    私有方法需不需要测试,本文不做讨论.假设您也认为有时候,私有方法也需要进行测试,那就一起来看看如何进行私有方法的测试. 准备测试代码 测试用的代码还是前面测试时使用过的代码,不同之处就是在类中增加了一 ...

  2. java多线程编程(2)交替输出数字和字母

    mark一下,不停的看看notify和wait的没有理解 class Printer { int index=0; //输出奇数 public synchronized void printA(int ...

  3. net user命令

    net user net user 用户名 net user 用户名 密码 /add net user 用户名 /del net localgroup administrators net local ...

  4. 最受IT公司欢迎的50款开源软件

    文章来自:云头条编译 本文介绍了多款知名的开源应用软件,科技公司可以用它们来管理自己的 IT 基础设施.开发产品. 过去十年间,许多科技公司已开始畅怀拥抱开源.许多公司使用开源工具来运行自己的 IT ...

  5. AWK调用SHELL,并将变量传递给SHELL

    在Shell脚本中调用awk是非常自然和简单的,以前还写过一个关于awk/shell相互传递变量的文章:awk与shell之间的变量传递方法在awk脚本中,如果需要调用shell脚本/命令,则需要使用 ...

  6. windows 下文件上传到fastdfs

    php.ini 配置 [fastdfs]; the base pathfastdfs_client.base_path = D:/tmp ; connect timeout in seconds; d ...

  7. EXCEL VBA 选择文件对话框

    Sub XXX() Dim arr() arr = Application.GetOpenFilename("所有支付文件 (*.xls;*.xlsx;*.csv),*.xls;*.xlsx ...

  8. 【设计模式 - 5】之适配器模式(Adapter)

    1      模式简介 适配器模式解决的问题:让原本因为接口不兼容而不能一起工作的类可以一起工作. 适配器模式的UML原理图如下图所示: 从上图可见,客户想要用Target接口实现Adaptee接口中 ...

  9. Mac OS使用技巧之十六:系统失去响应怎么办?

    再好的系统,再快的本本,也会在执行时由于种种原因出现卡顿或者死机等失去响应的情况.Mac用户也会时不时碰到这样的情况,最常见的表现为鼠标变为七彩圆圈.通常等上一会儿系统会自己恢复.假设迟迟没有响应的话 ...

  10. MySQL slave状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们能够通过在slave上运行show slave status来查看slave的一些状态信息,当中有一个比較重要的參数Seconds_Behind_Master.那么你是否 ...