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
Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 10 9. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. 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). The input is ended with a line containing −1 .
Output
Each line of 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.
Example
input output
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1
3
题意:有长度为n的01串,有m个询问,每次给出区间内1的个数奇偶性,判断前多少个合法 |
思路:
可以说是带权并查集的模板题了。每次询问我就相当于区间和为1或者0(因为只考虑奇偶性),然后每次判断的时候看是不是在一个集合里,是的话看是否同奇偶,不是的话就更新区间。
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
map<ll,ll> fa;
map<ll,ll> sum;
ll get(ll x)
{
if(fa[x]!=x)
{
ll i = fa[x];
fa[x] = get(fa[x]);
sum[x] += sum[i];
}
return fa[x];
}
int main()
{
ll n;
while(cin>>n&&n!=-1)
{
ll m; ll ans = 1e18; m = read();
fa.clear(); sum.clear();
rep(i,1,m)
{
ll L , R; string s;
L = read(), R = read(); cin>>s;
ll add = 0; if(s=="odd") add ++;
L --;
if(fa[L]==0) fa[L] = L; if(fa[R]==0) fa[R] = R;
ll fl = get(L);
ll fr = get(R);
if(fl==fr)
{
if(((sum[L]-sum[R])&1) != add)
ans = min(ans,i);
}
else
fa[fl] = fr, sum[fl] = sum[R] + add - sum[L];
}
if(ans==1e18) ans = m+1;
cout<<ans-1<<endl;
}
return 0;
}