注意到没有偶环,那么只剩下奇环,题目好做了许多。
众所周知,一个图是二分图当且仅当不存在奇环,那么要让题目中的子图是二分图的话需要满足不包含奇环。这个东西比较难搞,不放把每个环 映射成一个区间 ,其中 ,此时一个包含 点的子图不是二分图当且仅当 且 。
那么我们怎么找环?显然地,我们构建一下图的 DFS 生成树并存储非树边,那么我们发现当我们把非树边加入生成树就产生了环。环怎么映射成区间?(虽然也没啥好讲的)
考虑如下的图的生成树(样例 ):
加入边 :
此时我们找到了一个环,此时可以映射出区间 ,然后你发现我们只要去求解一下 到 的简单路径的点的最小值和最大值就行了。这个地方可以使用倍增实现。
接下来我们要求解的是这个式子:
注意上述式子中的括号是艾弗森括号,接下来考虑优化,预处理 :
原式转换为:
由于 是后缀形式,必然单调不降,那么令 ,又可以转换为:
预处理前缀和再二分查找即可,时间复杂度 。
参考代码:
/*
* author: LostKeyToReach
* created time: 2025-05-16 10:12:51
*/
#include <bits/stdc++.h>
#define int long long
#define vi vt<int>
using ll = long long; using pii = std::pair<int, int>;
template<typename T> using vt = std::vector<T>;
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rof(i, a, b) for(int i = (a); i >= (b); --i)
#define S std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0)
template<typename T> T chkmax(T& x, T y) {
return (x < y) ? (x = y, y) : x;
} template<typename T> T chkmin(T& x, T y) {
return (x > y) ? (x = y, y) : x;
}int fio = (S, 0); constexpr int N = 5e5 + 5;
int32_t main() {int n, m; vt<pii> bk;
std::cin >> n >> m; vi dep(n + 1, 0);
vt<vi> adj(n + 1); For(i, 1, m){
int x, y; std::cin >> x >> y;
adj[x].emplace_back(y);
adj[y].emplace_back(x);
} vt<vi> anc(20, vi(n + 1, 0)), mn = anc, mx = anc;
auto dfs = [&](auto&&self, int x, int s) -> void {
for (auto&y : adj[x]) {if (y ^ anc[0][x]) {
if (!dep[y] && y != s) {
anc[0][y] = x, mn[0][y] = std::min(x, y);
mx[0][y] = std::max(x, y), dep[y] = dep[x] + 1;
self(self, y, s);
} else if (dep[y] < dep[x] - 1) bk.emplace_back(x, y);
} } }; For(i, 1, n) if (!dep[i]) {
anc[0][i] = mn[0][i] = mx[0][i] = i;
dfs(dfs, i, i);
} For(i, 1, 19) For(j, 1, n) {
anc[i][j] = anc[i - 1][anc[i - 1][j]];
mn[i][j] = std::min(mn[i - 1][j], mn[i - 1][anc[i - 1][j]]);
mx[i][j] = std::max(mx[i - 1][j], mx[i - 1][anc[i - 1][j]]);
} auto query = [&](int x, int y) -> pii {
int minn = std::min(x, y), maxn = std::max(x, y);
if (dep[x] < dep[y]) std::swap(x, y);
int d = dep[x] - dep[y]; For(i, 0, 19) if (d & (1 << i)) {
chkmin(minn, mn[i][x]), chkmax(maxn, mx[i][x]);
x = anc[i][x];} if (x == y) return std::make_pair(minn, maxn);
Rof(i, 19, 0) if (anc[i][x] != anc[i][y]) {
chkmin(minn, std::min(mn[i][x], mn[i][y]));
chkmax(maxn, std::max(mx[i][x], mx[i][y]));
x = anc[i][x], y = anc[i][y];}
chkmin(minn, std::min(mn[0][x], mn[0][y]));
chkmax(maxn, std::max(mx[0][x], mx[0][y]));
return std::make_pair(std::min(minn, anc[0][x]), std::max(maxn, anc[0][x]));
}; vt<vi> buc(n + 1); vi lim(n + 1, 0);
for (auto&[x, y] : bk) { auto [a, b] = query(x, y);
buc[a].emplace_back(b);
} int cur = n + 1;Rof(i, n, 1) {
for (auto&x : buc[i]) chkmin(cur, x); lim[i] = cur;
} vi tmp = lim; For(i, 1, n) lim[i] += lim[i - 1];
int q; std::cin >> q; while (q--) {
int l, r; std::cin >> l >> r;
int lo = l, hi = r, p = r + 1; while (lo <= hi){
int mid = (lo + hi) >> 1; if (tmp[mid] <= r)
lo = mid + 1; else hi = (p = mid) - 1;
} std::cout << lim[p - 1] - lim[l - 1]
+ (r - p + 1) * (r + 1) - r * (r + 1) / 2 + l * (l - 1) / 2 << "\n";
}
}
