Title: POJ1733.
Given nnn intervals [l i, R i] [l_i, r_i] [l i, R i] and aia_iai, the weights of [l i, R i] [l_i, r_i] [l i, R i] and aia_iai are odd or even. Ask which interval is not contradictory, but its next interval is contradictory.
1≤n≤5∗1031\leq n\leq 5*10^31≤n≤5∗103.
It's easy to think of prefixes and sums, so intervals become information on two points li_1l_i-1li_1 and rir_iri.
Consider maintaining the prefix of each point and d[i]d[i]d[i], the point weight of a point I I I represents the prefix of I I I and the point weight of the father of exclusive or I I i, that is, the parity of the interval [fa[i]+1,i][fa[i]+1,i][fa[i]+1,i].
When an interval [l i, R i] [l_i, r_i] [l i, R i] is added, if l i_1, R I l_i-1, r_i L i_1 and R I are in the same interval, it will be vigorously judged whether their point weights and differences are equal to a[i]a[i]a[i] [i]. Then adding this interval will be equivalent to setting the ancestor of R I r of R I r_i R I as l i_1 l_i-1 and updating the point weights at the same time.
Note that at this time, the path compression also needs to update the point weight.
Time complexity O (n log n) O (n log n) O (n log n).
The code is as follows:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define Abigail inline void typedef long long LL; const int N=5000; int ri(){ char c=getchar(); int x=0,y=1; for (;c<'0'||c>'9';c=getchar()) if (c=='-') y=-1; for (;c<='9'&&c>='0';c=getchar()) x=x*10+c-'0'; return x*y; } int rc(){ char c=getchar(); while (c<'a'||c>'z') c=getchar(); return c=='o'?1:0; } int n,ord[N*2+9],x[N+9],y[N+9],a[N+9]; int fa[N*2+9],d[N*2+9],ans; int lower(int k){ int l=1,r=n<<1,mid=l+r>>1; for (;l<r;mid=l+r>>1) k<=ord[mid]?r=mid:l=mid+1; return l; } int get(int u){ if (u==fa[u]) return u; int rot=get(fa[u]);d[u]^=d[fa[u]]; return fa[u]=rot; } Abigail into(){ ri();n=ri(); for (int i=1;i<=n;++i){ x[i]=ri()-1;y[i]=ri();a[i]=rc(); ord[i*2-1]=x[i];ord[i*2]=y[i]; } } Abigail work(){ sort(ord+1,ord+1+2*n); for (int i=0;i<=n;++i) x[i]=lower(x[i]),y[i]=lower(y[i]); for (int i=1;i<=n<<1;++i) fa[i]=i; int u,v; ans=n; for (int i=1;i<=n;++i){ u=get(x[i]);v=get(y[i]); if (u==v&&d[x[i]]^d[y[i]]^a[i]){ans=i-1;return;} fa[v]=u,d[v]=d[x[i]]^d[y[i]]^a[i]; } } Abigail outo(){ printf("%d\n",ans); } int main(){ into(); work(); outo(); return 0; }