Bipartite Segments CodeForces - 901C (区间二分图计数)
Posted uid001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bipartite Segments CodeForces - 901C (区间二分图计数)相关的知识,希望对你有一定的参考价值。
大意: 给定无向图, 无偶环, 每次询问求[l,r]区间内, 有多少子区间是二分图.
无偶环等价于奇环仙人掌森林, 可以直接tarjan求出所有环, 然后就可以预处理出每个点为右端点时的答案.
这样的话区间询问等价于区间求和, 特殊处理一下左右边界的环即可.
要注意同一个点可能属于多个环!!
#include <iostream> #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘ ‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘ ‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head #ifdef ONLINE_JUDGE const int N = 1e6+10; #else const int N = 111; #endif int n, m, cnt; vector<int> g[N]; int dfn[N], fa[N], L[N], R[N]; void dfs(int x) { dfn[x] = ++*dfn; for (int y:g[x]) { if (dfn[y]) { if (dfn[y]<dfn[x]||y==x) continue; fa[x] = y, ++cnt; L[cnt]=R[cnt]=x; for (; y!=x; y=fa[y]) { L[cnt] = min(L[cnt], y); R[cnt] = max(R[cnt], y); } } else fa[y]=x, dfs(y); } } int f[N]; ll sum[N]; int main() { scanf("%d%d", &n, &m); REP(i,1,m) { int u, v; scanf("%d%d", &u, &v); g[u].pb(v),g[v].pb(u); } REP(i,1,n) if (!dfn[i]) dfs(i); REP(i,1,cnt) f[R[i]] = max(f[R[i]], L[i]); REP(i,1,n) { f[i]=max(f[i],f[i-1]); sum[i]=sum[i-1]+(i-f[i]); } int q; scanf("%d", &q); REP(i,1,q) { int l, r; scanf("%d%d", &l, &r); int pos = lower_bound(f+1,f+1+n,l)-f; if (pos>r) { printf("%lld ", (ll)(r-l+1)*(r-l+2)/2); } else { ll ans = sum[r]-sum[pos-1]; ll d = pos-l; printf("%lld ", ans+d*(1+d)/2); } } }
以上是关于Bipartite Segments CodeForces - 901C (区间二分图计数)的主要内容,如果未能解决你的问题,请参考以下文章
Bipartite Segments CodeForces - 901C (区间二分图计数)
luogu CF901C Bipartite Segments |Tarjan+二分
[LeetCode] Is Graph Bipartite? 是二分图么?