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 ...
随机推荐
- 使用deque保留有限的记录
# 使用deque保留有限的记录 >>> from collections import deque >>> q = deque(maxlen=3) # 指定队列的 ...
- PHP基础 (麦子学院 第二阶段)
zendstudio 10.0破解版,新建完项目后,首先修改项目的编码方式,统一改成utf-8 (选中项目,再右键properties:Text file encoding).修改字体大小. apac ...
- elasticsearch 5.x 系列之五 数据导入导出
一.首先给大家发一个福利,分享一个elasticsearch 数据导出工具. esm github 源码地址: https://github.com/medcl/esm 下载编译好的对应elastic ...
- PHP教程专题资源免费下载地址收藏
PHP教程专题资源免费下载地址收藏 PHP,即Hypertext Preprocessor,是一种被广泛应用的开源通用脚本语言,尤其适用于 Web 开发并可嵌入 HTML 中去.它的语法利用了 C. ...
- JavaScript之this解析
1.解析器在调用函数每次都会向函数内部传递进一个隐含的参数,这个隐含的参数就是this,this指向的是一个对象,这个对象我们称为函数执行的上下文对象,根据函数的调用方式不同,this会指向不同的对象 ...
- 008---re正则模块
re正则模块 字符串的匹配规则 匹配模式 re.match() re.search() re.findall() re.split() re.sub() 元字符 print('------------ ...
- 006---hashlib模块
hashlib模块 HASH 一般翻译成散列,也可以叫哈希. 把任意长度的输入通过散列算法变换成固定的长度. 该转换是一种压缩映射 MD5 输入任意长度的信息,经过处理.输出为128位的信息(数字指纹 ...
- WPF ItemsControl 手动刷新
原文:WPF ItemsControl 手动刷新 遇到这样一个问题, 我的ItemsSource是绑定到一个ObservableCollection<T>类型的实力上去的. 但是T类型没有 ...
- 再谈js传值和传址
js的传值和传址还是真绕,前回文说道 1.值类型是传值的 2.对象和数组是传址的 这两点通过例子的到了证实 然而还有一种情况没有讨论 即 函数的参数的传值和传址 通过实验,在函数中用一个新对象去覆盖传 ...
- 对C语言连等式的学习
例子如下 [pgsql@localhost soft]$ cat test1.c #include <stdlib.h> #include <stdio.h> int main ...