题目戳这里

首先题解给的是并查集的做法。这个做法很好想,但是很难码。用线段树数来维护并查集,暴力合并。

这里推荐另一个做法,可以无视\(K\)的限制。我们给每条边加个边权,这个边权为这条边左端点的值。然后我们将询问离线,按\(r\),从小到大处理。

对于当前询问\([l,r]\)我们用lct维护所有右端点\(\le r\)的边构成的最大生成树,然后用树状数组查询生成树中边权\(\ge l\)的边有几条即可。

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstdlib>
using namespace std; #define lowbit(a) (a&-a)
const int maxn = 200010,maxm = 500010;
int N,K,M,tot,cnt,tree[maxn],Q; bool rev[maxn];
int ch[maxn][2],fa[maxn],ma[maxn],pma[maxn],key[maxn],stack[maxn],ans[maxn];
queue <int> team; inline void ins(int a,int b) { for (;a <= N;a += lowbit(a)) tree[a] += b; }
inline int calc(int a) { int ret = 0; for (;a;a -= lowbit(a)) ret += tree[a]; return ret; } inline int gi()
{
int ret = 0,f = 1; char ch;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') f = -1;
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
} struct Node
{
int l,r,id;
inline void read(int i) { l = gi(),r = gi(),id = i; }
friend inline bool operator <(const Node &a,const Node &b) { return a.r < b.r; }
}query[maxn];
struct EDGE
{
int u,v;
inline void read() { u = gi(),v = gi(); if (u > v) swap(u,v); }
friend inline bool operator <(const EDGE &a,const EDGE &b) { return a.v < b.v; }
}edge[maxm]; inline bool isroot(int x) { return ch[fa[x]][0] != x&&ch[fa[x]][1] != x; }
inline void update(int x)
{
int lc = ch[x][0],rc = ch[x][1];
ma[x] = key[x]; pma[x] = x;
if (lc && ma[lc] < ma[x]) ma[x] = ma[lc],pma[x] = pma[lc];
if (rc && ma[rc] < ma[x]) ma[x] = ma[rc],pma[x] = pma[rc];
}
inline int newnode(int w)
{
int ret;
if (team.empty()) ret = ++cnt; else ret = team.front(),team.pop();
key[ret] = w; fa[ret] = ch[ret][0] = ch[ret][1] = 0;
update(ret); return ret;
}
inline void rotate(int x)
{
int y = fa[x],z = fa[y],l = ch[y][1] == x,r = l^1;
if (!isroot(y)) ch[z][ch[z][1] == y] = x; fa[x] = z;
if (ch[x][r]) fa[ch[x][r]] = y; ch[y][l] = ch[x][r];
fa[y] = x; ch[x][r] = y; update(y); update(x);
}
inline void pushdown(int x)
{
if (rev[x])
{
int lc = ch[x][0],rc = ch[x][1];
rev[x] = false; swap(ch[x][0],ch[x][1]);
if (lc) rev[lc] ^= 1; if (rc) rev[rc] ^= 1;
}
}
inline void splay(int x)
{
int top = 0,i;
for (i = x;!isroot(i);i = fa[i]) stack[++top] = i; stack[++top] = i;
while (top) pushdown(stack[top--]);
while (!isroot(x))
{
int y = fa[x],z = fa[y];
if (!isroot(y))
{
if ((ch[y][0] == x)^(ch[z][0] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
inline int access(int x) { int t = 0; for (;x;t = x,x = fa[x]) splay(x),ch[x][1] = t,update(x); return t; }
inline int evert(int x) { int t = access(x); rev[t] ^= 1; return t; }
inline int find(int x) { int t = access(x); while (pushdown(t),ch[t][0]) t = ch[t][0]; return t; }
inline void cut(int now)
{
evert(now); team.push(now); ins(edge[key[now]].u,-1);
access(edge[key[now]].u); splay(now); ch[now][1] = fa[edge[key[now]].u] = 0;
access(edge[key[now]].v); splay(now); ch[now][1] = fa[edge[key[now]].v] = 0;
}
inline void link(int w)
{
if (find(edge[w].u) == find(edge[w].v))
evert(edge[w].u),cut(pma[access(edge[w].v)]);
int now = newnode(w);
int t1 = evert(edge[w].u),t2 = evert(edge[w].v);
fa[t1] = fa[t2] = now; ins(edge[w].u,1);
} int main()
{
freopen("763E.in","r",stdin);
freopen("763E.out","w",stdout);
cnt = N = gi(); K = gi(); M = gi();
for (int i = 1;i <= M;++i) edge[i].read();
for (int i = 1;i <= N;++i) key[i] = 1<<30,update(i);
sort(edge+1,edge+M+1);
Q = gi();
for (int i = 1;i <= Q;++i) query[i].read(i);
sort(query+1,query+Q+1);
for (int i = 1,now = 1;i <= Q;++i)
{
for (;now <= M&&edge[now].v <= query[i].r;) link(now++);
ans[query[i].id] = query[i].r-query[i].l+1-(calc(query[i].r)-calc(query[i].l-1));
}
for (int i = 1;i <= Q;++i) printf("%d\n",ans[i]);
fclose(stdin); fclose(stdout);
return 0;
}

CF763E Timofey and our friends animals的更多相关文章

  1. Animals and Puzzle

    Animals and Puzzle time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  2. 763A - Timofey and a tree

    A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. 7.11 animals.c 程序

    7.11 animals.c 程序 #include <stdio.h> #include <ctype.h> int main(void) { char ch; printf ...

  4. Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增

    D. Animals and Puzzle 题目连接: http://codeforces.com/contest/713/problem/D Description Owl Sonya gave a ...

  5. Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)

    摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...

  6. 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表

    [题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...

  7. Codeforces Round #371 (Div. 1) D - Animals and Puzzle 二维ST表 + 二分

    D - Animals and Puzzle #include<bits/stdc++.h> #define LL long long #define fi first #define s ...

  8. CodeForces 35D Animals

    G - Animals Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. 【DFS】【打表】Lattice Animals

    [ZOJ2669]Lattice Animals Time Limit: 5 Seconds      Memory Limit: 32768 KB Lattice animal is a set o ...

随机推荐

  1. Git----使用WebHook实现代码自动部署

    起因: 经常本地push到gitee等线上代码仓库,然后登陆服务器在进行pull,很麻烦,想偷懒怎么办?使用git的webhook实现! 1.实现原理 1.1本地提交推送 1.2线上仓库监听push动 ...

  2. 【Thrift一】Thrift安装部署

    Thrift安装部署 Thrift安装部署 下载源码包 安装g++ 解压Thrift安装包 安装boost开发工具 测试(python版) 下载源码包 wget http://apache.fayea ...

  3. plsql 连接数据库无法解析指定的连接标识符

    之前用plsql连接的时候一直出问题,报无法解析指定的连接标识符,但是我加上ip地址就可以连接上. 我百度了很久,有说如下图选择oracle home的,有说清空admin目录下的所有文件, 但是都不 ...

  4. 多线程编程之Apue3rd_Chapter15.10之posix信号量

    看了APUE的chapter15,只重点看了15.10,学习了posix信号量.Posix信号量比起xsi信号量的优点是性能更好,在Linux3.2.0平台上性能提升很大.其中命名信号量使用方法如下. ...

  5. python2中将Unicode编码的中文和str相互转换

    在python2x版本中 关于中文汉字转换 1.中文------字符串格式 >>> s = '汉字' >>> type(s) <type 'str'> ...

  6. PHP.38-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除、修改

    商品分类删除 1.删除商品时,根据商品id删除扩展分类表数据 商品扩展分类修改 1.在控制器GoodsController.class.php/edit()中根据商品id取出对应的所有扩展分类 2.在 ...

  7. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  8. Windows模拟linux终端工具Cmder+Gow

    1. 说明 Cmder:Windows下的终端模拟器. Gow: Windows下模拟Linux命令行工具集合.可以在windows执行linux下的大部分命令,如ls.grep.xargs等. 2. ...

  9. 类和实例属性的查找顺序 mro查找

    如果多个类继承父类,然后又被多个类继承这种复杂的问题,可以使用 mro方法 例如: class A: pass class C(D): pass class B(D): pass class A(B, ...

  10. Datenode无法启动

    执行start-dfs.sh后,或者执行datenode没有启动.很大一部分原因是因为在第一次格式化dfs后,启动并使用了hadoop,后来又重新执行了格式化命令 这时主节点namenode的clus ...