n<=300000,m<=300000的图,图上只有奇环,q<=300000个询问每次问:一个区间内有多少个子区间,满足只保留编号在该区间的点以及他们之间的边,可以构成一个二分图。

终于走出了第一步。。Pi--从点i开始往前延伸最早到哪里就不是二分图了。由于这个数组是单调的,只要这个数组求出来就可以回答询问:每次回答询问时,输出$\sum_{i=L}^{R} Max(L-1,P_i)$即可。

然后就是这个数组怎么求了。。要支持删除点、插入点、查询是不是二分图。。LCT??并查集??动态图??懵逼。。。

然而题目有特殊性质。。只有奇环就是没有环套环的意思啦,如果有环套环肯定是有偶环的,然后在一个环内,最大编号a,最小编号b,那么相当于对$[a,n]$区间的P数组对b取个Max。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
//#include<queue>
//#include<math.h>
//#include<time.h>
//#include<iostream>
using namespace std; int n,m,q;
#define maxn 300011
#define maxm 600011
struct Edge{int to,next;};
struct Graph
{
Edge edge[maxm]; int first[maxn],le;
Graph() {le=; memset(first,,sizeof(first));}
void in(int x,int y) {Edge &e=edge[le]; e.to=y; e.next=first[x]; first[x]=le++;}
void insert(int x,int y) {in(x,y); in(y,x);}
}g,bg; #define LL long long
int p[maxn]; LL sum[maxn]; int dfn[maxn],low[maxn],Time=,sta[maxn],top=,tag[maxn]; bool insta[maxn];
void tarjan(int x,int fa)
{
// cout<<"tarjan"<<x<<endl;
dfn[x]=low[x]=++Time;
sta[++top]=x; insta[x]=;
for (int i=g.first[x];i;i=g.edge[i].next)
{
const Edge &e=g.edge[i]; if (e.to==fa) continue;
if (!dfn[e.to]) tarjan(e.to,x),low[x]=min(low[x],low[e.to]);
else if (insta[e.to]) low[x]=min(low[x],dfn[e.to]);
}
if (dfn[x]==low[x])
{
int Min=0x3f3f3f3f,Max=;
while (sta[top]!=x) Min=min(Min,sta[top]),Max=max(Max,sta[top]),insta[sta[top]]=,top--;
Min=min(Min,x); Max=max(Max,x); top--; insta[x]=;
if (Min!=Max) tag[Max]=max(tag[Max],Min);
}
} int main()
{
scanf("%d%d",&n,&m);
for (int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
g.insert(x,y);
} for (int i=;i<=n;i++) if (!dfn[i]) tarjan(i,);
int now=;
for (int i=;i<=n;i++) now=max(now,tag[i]),p[i]=now;
// for (int i=1;i<=n;i++) cout<<p[i]<<' ';cout<<endl; for (int i=;i<=n;i++) sum[i]=sum[i-]+p[i];
scanf("%d",&q);
while (q--)
{
int x,y; scanf("%d%d",&x,&y);
int L=x,R=y+;
while (L<R)
{
const int mid=(L+R)>>;
if (p[mid]>=x) R=mid;
else L=mid+;
}
printf("%lld\n",-1ll*(x-)*(L-x)-(sum[y]-sum[L-])+1ll*(x+y)*(y-x+)/);
}
return ;
}

CF901C. Bipartite Segments的更多相关文章

  1. CF901C Bipartite Segments[点双+二分+前缀优化]

    不想翻译了,直接放luogu翻译 说了没有偶环,也就是说全是奇环,再结合二分图性质,那么暴力的话,固定左端点,增大序号,加点直到产生环就不合法了.也就是说,任何一个环,只要他上面的数全都被加了,就不合 ...

  2. Codeforces 901C Bipartite Segments

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

  3. 【CodeForces】901 C. Bipartite Segments

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

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

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

  5. Bipartite Segments CodeForces - 901C (区间二分图计数)

    大意: 给定无向图, 无偶环, 每次询问求[l,r]区间内, 有多少子区间是二分图. 无偶环等价于奇环仙人掌森林, 可以直接tarjan求出所有环, 然后就可以预处理出每个点为右端点时的答案. 这样的 ...

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

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

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

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

  8. Codeforces Round #453 (Div. 1)

    Codeforces Round #453 (Div. 1) A. Hashing Trees 题目描述:给出一棵树的高度和每一层的节点数,问是否有两棵树都满足这个条件,若有,则输出这两棵树,否则输出 ...

  9. Codeforces Round #453

    Visiting a Friend Solution Coloring a Tree 自顶向下 Solution Hashing Trees 连续2层节点数都超过1时能异构 Solution GCD ...

随机推荐

  1. Linux oraenv Tips

    Linux for the Oracle DBA -Customizing the Oracle User's Environment There are many ways to customize ...

  2. WPF学习09:数据绑定之 Binding to List Data

    从WPF学习03:Element Binding我们可以实现控件属性与控件属性的绑定. 从WPF学习07:MVVM 预备知识之数据绑定 我们可以实现控件属性与自定义对象属性的绑定. 而以上两个功能在实 ...

  3. Java 线程 —— Wait (等待)和 Notify(唤醒)

    Wait (等待)和 Notify(唤醒) 这里讲了一个Wait (等待)和 Notfity(唤醒),下面这个实例(工厂,商店,消费者) 额,然后,你就知道了,需要写三个类:工厂类,Shop类,消费者 ...

  4. poj1930 Dead Fraction

    思路: 循环小数化分数,枚举所有可能的循环节,取分母最小的那个. 实现: #include <iostream> #include <cstdio> #include < ...

  5. [BZOJ4815][CQOI2017]小Q的表格 数论+分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4815 题目中所给条件中的$(a,a+b)$和$(a,b)$的关系很瞩目. 然后大家都知道$ ...

  6. asp.net 实现treeview 选中父节点其子节点也选种中 选中子节点其父节点与根节点也被选中

    1.在 Page_Load(object sender, EventArgs e) 里面加入: TreeView1.Attributes.Add("onclick", " ...

  7. Winform webbrowser 隐藏 html 元素

    目的:用webbrowser打开网页,并隐藏网页上某个html元素 1.如果已知元素ID,比较好办 直接使用webbrowser1.Document.getElementById("id&q ...

  8. CSS3 四边形 凹角写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. tf.app.run() got unexpected keyword argument 'argv'

    运行的代码是mnist_with_summaries.py.出现的问题是 tf.app.run() got unexpected keyword argument 'argv' 昨天一直以为是我自己不 ...

  10. JS Object 属性判断

    in 方法 var shapeInfo = {name:“lium”}; if (“name” in shapeInfo) {...}