大意: 给定无向图, 无偶环, 每次询问求[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 '\n'
#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\n", (ll)(r-l+1)*(r-l+2)/2);
}
else {
ll ans = sum[r]-sum[pos-1];
ll d = pos-l;
printf("%lld\n", ans+d*(1+d)/2);
}
}
}

Bipartite Segments CodeForces - 901C (区间二分图计数)的更多相关文章

  1. Codeforces 901C Bipartite Segments(Tarjan + 二分)

    题目链接  Bipartite Segments 题意  给出一个无偶环的图,现在有$q$个询问.求区间$[L, R]$中有多少个子区间$[l, r]$ 满足$L <= l <= r &l ...

  2. Codeforces 901C Bipartite Segments

    Bipartite Segments 因为图中只存在奇数长度的环, 所以它是个只有奇数环的仙人掌, 每条边只属于一个环. 那么我们能把所有环给扣出来, 所以我们询问的区间不能包含每个环里的最大值和最小 ...

  3. Codeforces Round #453 (Div. 1) 901C C. Bipartite Segments

    题 http://codeforces.com/contest/901/problem/C codeforces 901C 解 首先因为图中没有偶数长度的环,所以: 1.图中的环长度全是奇数,也就是说 ...

  4. 【CodeForces】901 C. Bipartite Segments

    [题目]C. Bipartite Segments [题意]给定n个点m条边的无向连通图,保证不存在偶数长度的简单环.每次询问区间[l,r]中包含多少子区间[x,y]满足只保留[x,y]之间的点和边构 ...

  5. cogs [HZOI 2015]有标号的二分图计数

    题目分析 n个点的二分染色图计数 很显然的一个式子 \[ \sum_{i=0}^n\binom{n}{i}2^{i(n-i)} \] 很容易把\(2^{i(n-i)}\)拆成卷积形式,前面讲过,不再赘 ...

  6. COGS 有标号的二分图计数系列

    其实这三道题都是不错的……(虽然感觉第三题略套路了……) 分别写一下做法好了…… COGS2392 有标号的二分图计数 I 这个就很简单了,Noip难度. 显然可以直接认为黑点和白点分别位于二分图两侧 ...

  7. D - Nested Segments CodeForces - 652D (离散化+树桩数组)

    D - Nested Segments CodeForces - 652D You are given n segments on a line. There are no ends of some ...

  8. Codeforces 901C. Bipartite Segments(思维题)

    擦..没看见简单环..已经想的七七八八了,就差一步 显然我们只要知道一个点最远可以向后扩展到第几个点是二分图,我们就可以很容易地回答每一个询问了,但是怎么求出这个呢. 没有偶数简单环,相当于只有奇数简 ...

  9. Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)

    E1. Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 25 ...

随机推荐

  1. P3380 【模板】二逼平衡树(树套树)

    思路 若opt=1 则为操作1,之后有三个数l,r,k 表示查询k在区间[l,r]的排名 若opt=2 则为操作2,之后有三个数l,r,k 表示查询区间[l,r]内排名为k的数 若opt=3 则为操作 ...

  2. HDU-6033 Add More Zero

    There is a youngster known for amateur propositions concerning several mathematical hard problems. N ...

  3. mysql表分区存储过程

    本文为博主原创,未经允许不得转载: 由于数据库一张表数据量有几千万条,而且在不断增长,看见公司前辈写了一个创建表分区的存储过程,感觉 甚是牛逼,在此供自己保留学习. /*PROCEDURE creat ...

  4. pitch, yaw, roll

    在航空中,pitch, yaw, roll下图所示. pitch是围绕X轴旋转,也叫做俯仰角. yaw是围绕Y轴旋转,也叫偏航角. roll是围绕Z轴旋转,也叫翻滚角.

  5. Python: find the smallest and largest value

    题目要求: Write a program that repeatedly prompts a user for integer numbers until the user enters 'done ...

  6. 「BZOJ2153」设计铁路 - 斜率DP

    A省有一条东西向的公路经常堵车,为解决这一问题,省政府对此展开了调查. 调查后得知,这条公路两侧有很多村落,每个村落里都住着很多个信仰c教的教徒,每周日都会开着自家的车沿公路到B地去"膜拜& ...

  7. 每天一个小程序—0000题(python图像处理)

    第 0000 题: 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果 python中的pillow库是专门用于处理图像的. from PIL ...

  8. vs里32位项目和64位项目的区别

    由于操作系统内存分配的不同,导致软件开发过程中,需要编译不同版本的软件. 1.编译程序根据需要选择不同的编译环境. x86和win32为32位程序,x64为64位程序,可以选择不同的编译条件形成不同位 ...

  9. JsonKey小写

    System.Text.RegularExpressions.MatchCollection ms = System.Text.RegularExpressions.Regex.Matches(eca ...

  10. BZOJ 3878 【AHOI2014】 奇怪的计算器

    题目链接:奇怪的计算器 如果没有溢出的话,所有的标记都可以在线段树上直接维护,所以一棵线段树就解决问题了. 现在有了溢出,怎么办呢? 发现就算溢出了,各个元素的相对大小关系也是不变的.所以,如果一开始 ...