# CodeForces 901C Bipartite Segments

### Description

$n,m,q\le 3\times 10^5$

### Solution

$(\sum_{i=l}^{p-1}s[i]-i+1)+(\sum_{i=p}^rr-i+1)$

#include<bits/stdc++.h>
using namespace std;

template <class T> void read(T &x) {
x = 0; bool flag = 0; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) flag |= ch == '-';
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48; flag ? x = ~x + 1 : 0;
}

#define N 300010
#define rep(i, a, b) for (auto i = (a); i <= (b); i++)
#define drp(i, a, b) for (auto i = (a); i >= (b); i--)
#define ll long long

struct { int v, next; }e[N << 1];
int head[N];
bool isBridge[N << 1];
inline void add(int u, int v) {
static int tot = 1; e[++tot].v = v, e[tot].next = head[u], head[u] = tot;
}

int dfn[N], low[N];
void tarjan(int u, int fa) {
static int ind; dfn[u] = low[u] = ++ind;
for (int i = head[u], v; i; i = e[i].next) {
if (!dfn[v = e[i].v]) {
tarjan(v, u), low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) isBridge[i] = isBridge[i ^ 1] = 1;
}
else if (dfn[v] < dfn[u] && v ^ fa) low[u] = min(low[u], dfn[v]);
}
}

int bl[N], siz[N];
void dfs(int u, int ebc) {
siz[bl[u] = ebc]++;
//  printf("%d\n", u);
for (int i = head[u]; i; i = e[i].next) if (!bl[e[i].v] && !isBridge[i]) dfs(e[i].v, ebc);
}

int mn[N], mx[N], s[N];
ll sum[N];

int main() {
int n, m; read(n), read(m);
rep(i, 1, m) {
int u, v; read(u), read(v);
add(u, v), add(v, u);
}
rep(i, 1, n) if (!dfn[i]) tarjan(i, 0);
int ebc = 0;
rep(i, 1, n) if (!bl[i]) dfs(i, ++ebc);
rep(i, 1, n) mx[bl[i]] = i;
drp(i, n, 1) mn[bl[i]] = i;
rep(i, 1, n) s[i] = n;
rep(i, 1, ebc) if (siz[i] > 1) s[mn[i]] = mx[i] - 1;
drp(i, n - 1, 1) s[i] = min(s[i], s[i + 1]);
rep(i, 1, n) sum[i] = sum[i - 1] + s[i] - i + 1;
int q; read(q);
while (q--) {
int l, r; read(l), read(r);
int p = lower_bound(s + l, s + r + 1, r) - s;
printf("%lld\n", 1ll * (r - p + 1) * (r - p + 2) / 2 + sum[p - 1] - sum[l - 1]);
}
return 0;
}
posted @ 2018-09-13 21:06 aziint 阅读(...) 评论(...) 编辑 收藏

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.