擦。。没看见简单环。。已经想的七七八八了,就差一步

  显然我们只要知道一个点最远可以向后扩展到第几个点是二分图,我们就可以很容易地回答每一个询问了,但是怎么求出这个呢。

  没有偶数简单环,相当于只有奇数简单环,没有环套环。因为如果有环套环,必定是两个奇数环合并1个或几个点,也就是同时保持奇数或者同时变为偶数,而我们知道奇数+奇数=偶数,偶数+偶数=偶数,所以就证明了只有奇数简单环,不存在环套环。

  我们现在有一些点,再加入一个点,最多会形成一个环,并且一定是奇环,这时候,编号为1~环上的最小编号的点,最远能扩展到的编号不会超过环上最大编号。所以我们tarjan缩点求出所有环后,把每一个环按照环上最大编号排序,然后从小到大统计每一个点最远能扩展到的点就好了。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #define ll long long
  7. using namespace std;
  8. const int maxn=, inf=1e9;
  9. struct poi{int too, pre;}e[maxn<<];
  10. struct tjm{int mx, mn;}q[maxn];
  11. int n, m, x, y, tot, tott, top, color, L, R, Q;
  12. int last[maxn], dfn[maxn], low[maxn], st[maxn], lack[maxn], mx[maxn], mn[maxn], col[maxn], nxt[maxn];
  13. ll sum[maxn], nxtsum[maxn];
  14. inline void read(int &k)
  15. {
  16. int f=; k=; char c=getchar();
  17. while(c<'' || c>'') c=='-' && (f=-), c=getchar();
  18. while(c<='' && c>='') k=k*+c-'', c=getchar();
  19. k*=f;
  20. }
  21. inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
  22. void tarjan(int x, int fa)
  23. {
  24. dfn[x]=low[x]=++tott; st[++top]=x; lack[x]=top;
  25. for(int i=last[x], too;i;i=e[i].pre)
  26. if((too=e[i].too)!=fa)
  27. {
  28. if(!dfn[too=e[i].too]) tarjan(too, x), low[x]=min(low[x], low[too]);
  29. else if(!col[too]) low[x]=min(low[x], dfn[too]);
  30. }
  31. if(dfn[x]==low[x])
  32. for(q[++color].mn=inf;lack[x]<=top;top--)
  33. {
  34. col[st[top]]=color;
  35. q[color].mx=max(q[color].mx, st[top]);
  36. q[color].mn=min(q[color].mn, st[top]);
  37. }
  38. }
  39. inline bool cmp(tjm a, tjm b){return a.mx<b.mx;}
  40. int main()
  41. {
  42. read(n); read(m);
  43. for(int i=;i<=m;i++) read(x), read(y), add(x, y), add(y, x);
  44. for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i, );
  45. sort(q+, q++color, cmp);
  46. int j=;
  47. for(int i=;i<=color;i++)
  48. if(q[i].mn!=q[i].mx)
  49. for(;j<=q[i].mn;j++) nxt[j]=q[i].mx-;
  50. for(int i=j;i<=n;i++) nxt[i]=n;
  51. for(int i=;i<=n;i++) sum[i]=sum[i-]+nxt[i]-i+;
  52. read(Q);
  53. for(int i=;i<=Q;i++)
  54. {
  55. read(L); read(R);
  56. int l=L-, r=R;
  57. while(l<r)
  58. {
  59. int mid=(l+r+)>>;
  60. if(nxt[mid]<=R) l=mid;
  61. else r=mid-;
  62. }
  63. printf("%lld\n", sum[l]-sum[L-]+1ll*(R+)*(R-l)-(1ll*(l++R)*(R-l)>>));
  64. }
  65. }

Codeforces 901C. Bipartite Segments(思维题)的更多相关文章

  1. Codeforces 901C Bipartite Segments

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

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

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

  3. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  4. Codeforces 675C Money Transfers 思维题

    原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...

  5. 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 ...

  6. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. Codeforces 957 水位标记思维题

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  8. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

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

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

随机推荐

  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 ...

  2. aircrack-ng无线破解实验

    查看无线网卡 airmon-ng 开启网卡监听模式 airmon-ng start wlan0 扫描附近的wifi airodump-ng wlan0mon 停止扫描: ctrl c 使用airodu ...

  3. printf命令详解

    基础命令学习目录首页 本文是Linux Shell系列教程的第(八)篇,更多shell教程请看:Linux Shell系列教程 在上一篇:Linux Shell系列教程之(七)Shell输出这篇文章中 ...

  4. [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 ...

  5. 数据库之python操作mysql

    目录 一.pymysql 二.SQLAchemy 2.操作使用 (1)连接数据库 (2)执行原生SQL语句 (3)ORM操作-数据表操作 (4)ORM操作-数据行操作 (5)更多例子 一.pymysq ...

  6. Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValuesByConditionsNoPage

    报Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValu ...

  7. 树莓派 Raspberry-Pi 折腾系列:系统安装及一些必要的配置

    入手树莓派将近一个月了,很折腾,许多资源不好找,也很乱.简单整理一下自己用到的东西,方便以后自己或别人继续折腾. 0. 操作系统下载 树莓派官方 Raspbian 系统下载:http://www.ra ...

  8. 手机访问本地php项目遇到的问题及解决

    做html5的本地应用要调试后台,学了下php 按照和连j2ee的时候一样,电脑发射wifi,ipconfig..等等  发现tomcat的可以访问,apache的不能访问,搜索好久,没找到解答, j ...

  9. 关于map和hashmap

    今天做的程序猿那题 在公司里面,程序猿经常有一堆todolist要做,而这些todolist是产品经理分配给他们的.但是当程序员遇到不懂技术的产品狗时,就悲剧了.产品经理经常修改他们的todolist ...

  10. “吃神么,买神么”的第一个Sprint计划

    一.现状 我们这个团队刚接触网络网站的制作,前台后台的链接,数据库链接等还刚刚了解,在制作过程中药边学习边制作. 持着一个尽力做的心. 二.部分需求索引卡 主要的任务是把我们的主页面先大概做出来 三. ...