琥珀色黄昏像糖在很美的远方,思念跟影子在傍晚一起被拉长……
小 B 带着 GF 去逛公园,公园一共有 n 个景点,标号为 1 . . . n。景点之间有 m 条路径相连。
小 B 想选择编号在一段区间 [l, r] 内的景点来游玩,但是如果这些景点的诱导子图形成了环,那么 GF 将会不高兴。
小 B 给出很多个询问 [x, y],想让你求有多少个区间 [l, r] 满足 x ≤ l, r ≤ y 且不会使 GF不高兴。
考试的时候写了一个BCC得了80分,但实际上这道题是一个V-DCC,毕竟是仙人掌嘛。
首先用V-DCC预处理出所有的环,然后更新一个点最远可以到哪里,这是显然单调的
二分查询的左端点。
最后求答案的时候把范围内的用前缀和求了,其余的用等差数列求和求出。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long LL;
const int N=3e5+100;
inline void read(int &x){
x=0;
int f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
x*=f;
}
struct Front_star{
int u,v,nxt;
}e[N<<1];
int cnt=1;
int first[N];
void add(int u,int v){
++cnt;
e[cnt].u=u;
e[cnt].v=v;
e[cnt].nxt=first[u];
first[u]=cnt;
}
//
int n,m;
struct Node{
int Mn,Mx;
}G[N];
//
int dfn[N];
int low[N];
int siz[N];
int Belong[N];
int vis[N<<1];
stack<int>S;
int dcc=0;
int tot=0;
void Tarjan(int u,int fat){
dfn[u]=low[u]=++tot;
S.push(u);
for(int i=first[u];i;i=e[i].nxt){
if((i^1)==fat)continue;
int v=e[i].v;
if(!dfn[v]){
Tarjan(v,i);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])S.pop();
if(dfn[u]==low[v]){
int Mn=u;
int Mx=u;
int x;
++dcc;
do{
x=S.top();
S.pop();
siz[dcc]++;
Mn=min(Mn,x);
Mx=max(Mx,x);
}while(x!=v);
G[dcc].Mn=Mn;
G[dcc].Mx=Mx;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
//
LL sum[N];
LL L[N];
int main(){
freopen("parka.in","r",stdin);
freopen("parka.out","w",stdout);
read(n);
read(m);
for(int i=1;i<=m;++i){
int u,v;
read(u);
read(v);
add(u,v);
add(v,u);
}
for(int i=1;i<=n;++i)if(!dfn[i])Tarjan(i,0);
m=0;
for(int i=1;i<=dcc;++i){
L[G[i].Mx]=max(L[G[i].Mx],(LL)G[i].Mn);
}
for(int i=1;i<=n;++i){
L[i]=max(L[i],L[i-1]);
sum[i]=sum[i-1]+(i-L[i]);
}
int Q;
read(Q);
while(Q--){
int l,r;
read(l);
read(r);
int pos=upper_bound(L+1,L+1+r,l-1)-L;
if(pos>=l)cout<<sum[r]-sum[pos-1]+1LL*(pos-l)*(pos-l+1)/2<<'\n';
else cout<<sum[r]-sum[l-1]<<'\n';
}
return 0;
}