Codeforces 901C. Bipartite Segments(思维题)
擦。。没看见简单环。。已经想的七七八八了,就差一步
显然我们只要知道一个点最远可以向后扩展到第几个点是二分图,我们就可以很容易地回答每一个询问了,但是怎么求出这个呢。
没有偶数简单环,相当于只有奇数简单环,没有环套环。因为如果有环套环,必定是两个奇数环合并1个或几个点,也就是同时保持奇数或者同时变为偶数,而我们知道奇数+奇数=偶数,偶数+偶数=偶数,所以就证明了只有奇数简单环,不存在环套环。
我们现在有一些点,再加入一个点,最多会形成一个环,并且一定是奇环,这时候,编号为1~环上的最小编号的点,最远能扩展到的编号不会超过环上最大编号。所以我们tarjan缩点求出所有环后,把每一个环按照环上最大编号排序,然后从小到大统计每一个点最远能扩展到的点就好了。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=, inf=1e9;
struct poi{int too, pre;}e[maxn<<];
struct tjm{int mx, mn;}q[maxn];
int n, m, x, y, tot, tott, top, color, L, R, Q;
int last[maxn], dfn[maxn], low[maxn], st[maxn], lack[maxn], mx[maxn], mn[maxn], col[maxn], nxt[maxn];
ll sum[maxn], nxtsum[maxn];
inline void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
void tarjan(int x, int fa)
{
dfn[x]=low[x]=++tott; st[++top]=x; lack[x]=top;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
if(!dfn[too=e[i].too]) tarjan(too, x), low[x]=min(low[x], low[too]);
else if(!col[too]) low[x]=min(low[x], dfn[too]);
}
if(dfn[x]==low[x])
for(q[++color].mn=inf;lack[x]<=top;top--)
{
col[st[top]]=color;
q[color].mx=max(q[color].mx, st[top]);
q[color].mn=min(q[color].mn, st[top]);
}
}
inline bool cmp(tjm a, tjm b){return a.mx<b.mx;}
int main()
{
read(n); read(m);
for(int i=;i<=m;i++) read(x), read(y), add(x, y), add(y, x);
for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i, );
sort(q+, q++color, cmp);
int j=;
for(int i=;i<=color;i++)
if(q[i].mn!=q[i].mx)
for(;j<=q[i].mn;j++) nxt[j]=q[i].mx-;
for(int i=j;i<=n;i++) nxt[i]=n;
for(int i=;i<=n;i++) sum[i]=sum[i-]+nxt[i]-i+;
read(Q);
for(int i=;i<=Q;i++)
{
read(L); read(R);
int l=L-, r=R;
while(l<r)
{
int mid=(l+r+)>>;
if(nxt[mid]<=R) l=mid;
else r=mid-;
}
printf("%lld\n", sum[l]-sum[L-]+1ll*(R+)*(R-l)-(1ll*(l++R)*(R-l)>>));
}
}
Codeforces 901C. Bipartite Segments(思维题)的更多相关文章
- Codeforces 901C Bipartite Segments
Bipartite Segments 因为图中只存在奇数长度的环, 所以它是个只有奇数环的仙人掌, 每条边只属于一个环. 那么我们能把所有环给扣出来, 所以我们询问的区间不能包含每个环里的最大值和最小 ...
- Codeforces 901C Bipartite Segments(Tarjan + 二分)
题目链接 Bipartite Segments 题意 给出一个无偶环的图,现在有$q$个询问.求区间$[L, R]$中有多少个子区间$[l, r]$ 满足$L <= l <= r &l ...
- CF--思维练习-- CodeForces - 215C - Crosses(思维题)
ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...
- Codeforces 675C Money Transfers 思维题
原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...
- Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]
题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...
- codeforces 1140D(区间dp/思维题)
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 957 水位标记思维题
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- Codeforces Round #453 (Div. 1) 901C C. Bipartite Segments
题 http://codeforces.com/contest/901/problem/C codeforces 901C 解 首先因为图中没有偶数长度的环,所以: 1.图中的环长度全是奇数,也就是说 ...
随机推荐
- golang -- 字符串就地取反
字符串 定义 在golang中字符串是一种不可变的字节序列,它可以包含任意的数据,包括0值字节,但主要是人类可以阅读的文本.golang中默认字符串被解读为utf-8编码的Unicode码点(文字符号 ...
- Azure web site和web job的config文件加密方式
1.分析 由于Azure Web AppService平台的特殊性,所以在C#中原先的config加密方法DataProtectionConfigurationProvider和RSAProtecte ...
- Ajax请求返回Error:200无数据的解决方法
先看代码 $.ajax({ type:"GET", url:"https://****/charts/data/genre2.json", dataType:& ...
- Centos7 zabbix 分布式监控
分布式监控 zabbix Server ===> zabbix agent (只能同一个局域网监控) 分布式监控: a. 分担压力,降低负载 b. 多机房 ...
- mv命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/piaozhe116/p/6084214.html mv命令是move的缩写,可以用来移动文件或者将文件改名(move ...
- vi/vim命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/mondol/p/vi-examples.html 进入vi vi filename # 打开或新建文件,并将光标置于第 ...
- CentOS 6.7 安装配置 nagios
一.简介 Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警,第一时间 ...
- Java多线程编程之不可变对象模式
在多线程环境中,为了保证共享数据的一致性,往往需要对共享数据的使用进行加锁,但是加锁操作本身就会带来一定的开销,这里可以使用将共享数据使用不可变对象进行封装,从而避免加锁操作. 1. 模 ...
- maven:新建的maven工程需要添加一下插件
//下面的代码是改变maven的默认使用Java版本,本机是jdk1.7所以用1.7<plugins> <plugin> <groupId>org.apache.m ...
- 20145214 《网络对抗技术》 Web安全基础实践
20145214 <网络对抗技术> Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的 ...