CF763E Timofey and our friends animals
题目戳这里。
首先题解给的是并查集的做法。这个做法很好想,但是很难码。用线段树数来维护并查集,暴力合并。
这里推荐另一个做法,可以无视\(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的更多相关文章
- Animals and Puzzle
Animals and Puzzle time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- 763A - Timofey and a tree
A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 7.11 animals.c 程序
7.11 animals.c 程序 #include <stdio.h> #include <ctype.h> int main(void) { char ch; printf ...
- 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 ...
- Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)
摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...
- 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表
[题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...
- 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 ...
- CodeForces 35D Animals
G - Animals Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 【DFS】【打表】Lattice Animals
[ZOJ2669]Lattice Animals Time Limit: 5 Seconds Memory Limit: 32768 KB Lattice animal is a set o ...
随机推荐
- PHP命令行(CLI模式)
CLI模式 CLI模式其实就是命令行运行模式,英文全称Command-Line Interface(命令行接口) $ php -h Usage: php [options] [-f] <file ...
- 015---Django的forms组件
Django form表单 Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用 ...
- R语言学习笔记(一):mode, class, typeof的区别
要了解这三个函数的区别,先了解numeric, double与integer. 在r中浮点数有两个名字叫numeric与double. double是指它的类型(type)名字,numeric是指它的 ...
- Centos7下lamp环境搭建的小笔记
刚刚把校赛弄完,赛前在环境搭建上花了蛮多时间,也正好记一下笔记 0.首先更新源 清华大学开源镜像站的源 https://mirrors.tuna.tsinghua.edu.cn/help/centos ...
- 长沙Uber优步司机奖励政策(3月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【廖雪峰老师python教程】——map/reduce
Map[单个操作对不同单一对象重复进行] map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. 返回结果注 ...
- Sleuth+Zipkin+Log
https://blog.csdn.net/sqzhao/article/details/70568637 https://blog.csdn.net/yejingtao703/article/det ...
- Django数据模型--字段整理
一.字段 1.CharField: 字段数据类型为字符串 2.IntegerField: 字段数据类型为整形 3.BooleanField: 布尔类型 4.NullBooleanField: 允许为空 ...
- Win7系统下删除文件时出现“正在准备再循环”的解决方法
今天,笔者在备份文件的时候,将一个word文档从移动硬盘复制到桌面.经过一系列“复(meng)杂(bi)”的操作之后,笔者突然发现,文件无法删除了.当右键文件点击“删除”时,出现对话框显示“正在准备 ...
- Python 异步编程笔记:asyncio
个人笔记,不保证正确. 虽然说看到很多人不看好 asyncio,但是这个东西还是必须学的.. 基于协程的异步,在很多语言中都有,学会了 Python 的,就一通百通. 一.生成器 generator ...