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.图中的环长度全是奇数,也就是说 ...
随机推荐
- centos6.9 安装完xampp 7.2.0后,执行/opt/lampp/lampp报错
# /opt/lampp/lampp egrep: error while loading shared libraries: libc.so.6: cannot open shared object ...
- aircrack-ng无线破解实验
查看无线网卡 airmon-ng 开启网卡监听模式 airmon-ng start wlan0 扫描附近的wifi airodump-ng wlan0mon 停止扫描: ctrl c 使用airodu ...
- printf命令详解
基础命令学习目录首页 本文是Linux Shell系列教程的第(八)篇,更多shell教程请看:Linux Shell系列教程 在上一篇:Linux Shell系列教程之(七)Shell输出这篇文章中 ...
- [leetcode-897-Increasing Order Search Tree]
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- 数据库之python操作mysql
目录 一.pymysql 二.SQLAchemy 2.操作使用 (1)连接数据库 (2)执行原生SQL语句 (3)ORM操作-数据表操作 (4)ORM操作-数据行操作 (5)更多例子 一.pymysq ...
- Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValuesByConditionsNoPage
报Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValu ...
- 树莓派 Raspberry-Pi 折腾系列:系统安装及一些必要的配置
入手树莓派将近一个月了,很折腾,许多资源不好找,也很乱.简单整理一下自己用到的东西,方便以后自己或别人继续折腾. 0. 操作系统下载 树莓派官方 Raspbian 系统下载:http://www.ra ...
- 手机访问本地php项目遇到的问题及解决
做html5的本地应用要调试后台,学了下php 按照和连j2ee的时候一样,电脑发射wifi,ipconfig..等等 发现tomcat的可以访问,apache的不能访问,搜索好久,没找到解答, j ...
- 关于map和hashmap
今天做的程序猿那题 在公司里面,程序猿经常有一堆todolist要做,而这些todolist是产品经理分配给他们的.但是当程序员遇到不懂技术的产品狗时,就悲剧了.产品经理经常修改他们的todolist ...
- “吃神么,买神么”的第一个Sprint计划
一.现状 我们这个团队刚接触网络网站的制作,前台后台的链接,数据库链接等还刚刚了解,在制作过程中药边学习边制作. 持着一个尽力做的心. 二.部分需求索引卡 主要的任务是把我们的主页面先大概做出来 三. ...