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. MessageFormat.format处理单引号和大括号

    在MessageFormat.format方法中组装jason数据字符串:{code:"w1",des:"w2"},起止分别有左大括号和右大括号.方法是将单引号 ...

  2. Linux的IP设置参考

    位置:etc/network/interfaces 内容: 第一段是网口1自动从DHCP处获得IP 第二段是网口2静态分配IP 如果是IPv6,请把 iface eth0 inet dhcp(stat ...

  3. [Java] Java IO 概况

    Java IO 是 Java 的一套 API, 用于读入和写出数据(输入和输出).Java IO API 位于 java.io package.实际上 java.io package 没有解决所有的输 ...

  4. IDEA新建SpringMVC项目报错解决办法

    网页运行的错误: HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundE ...

  5. js实现table中前端搜索(模糊查询)

    项目中用到js前端搜索功能,根据 姓名或姓名 进行 搜索,实现方法如下,遍历table所有行中的某列,符合条件则置tr为display:'',不满足条件置tr为display:none. 代码如下: ...

  6. SQL server 创建表,索引,主键,外键

    if object_id('student', 'U') is not null drop table student go create table student( sno varchar(20) ...

  7. DevExpress 控件 GridControl常见用法

    刚接触DevExpress第三方控件,把GridControl的常见用法整理一下,以供参考: 说明: gcTest   GridControl gvText    GridView //隐藏最上面的G ...

  8. android 37 线程通信Looper

    安卓程序的主线程也叫UI线程. 工作线程和主线程的差别:安卓主线程已经调用了Looper.prepare()方法了,已经有一个MessageQueue对象了,所以才可以在工作线程用Handler发消息 ...

  9. 一次优化web项目的经历记录(三)

    一次优化web项目的经历记录 这段时间以来的总结与反思 前言:最近很长一段时间没有更新博客了,忙于一堆子项目的开发,严重拖慢了学习与思考的进程. 开水倒满了需要提早放下杯子,晚了就会烫手,这段时间以来 ...

  10. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之二

    /** ****************************************************************************** * @author    暴走的小 ...