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 ...
随机推荐
- Delphi 过程类型
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Kubernetes-GC
Kubernetes集群中垃圾回收(Garbage Collection)机制由kubelet完成.kubelet定期清理不再使用的容器和镜像,每分钟进行一次容器的GC操作,每五分钟进行一次镜像的GC ...
- 面试-MySQL总结
三范式 三范式定义(范式和反范式) 1NF:每个数据项都是最小单元,不可分割,确定行列之后只能对应一个数据. 2NF:每一个非主属性完全依赖于候选码(属性组的值能唯一的标识一个元组,但是其子集不可以) ...
- 转:C#微信公众号开发之接收事件推送与消息排重的方法
本文实例讲述了C#微信公众号开发之接收事件推送与消息排重的方法.分享给大家供大家参考.具体分析如下: 微信服务器在5秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次.这样的话,问题就来了.有这 ...
- vue整合mui
步骤1:下载https://github.com/dcloudio/mui 步骤2:将下载来的包中的dist文件夹 放到vue项目的assets中 步骤3:修改webpack配置 找到build下 ...
- loadrunner创建测试脚本运行无响应 不记录脚本
解决一运行User Generator直接程序卡死无响应的办法. (1)“我的电脑”点右键->属性->高级 点选“性能”中的“设置” (2)打开对话框后,进入“数据执行保护”,如果空白框中 ...
- 「日常训练」 Soldier and Number Game (CFR304D2D)
题意 (Codeforces 546D) 给定一个数x=a!b!" role="presentation">x=a!b!x=a!b!的形式,问其中有几个质因数. 分 ...
- Qt Qwdget 汽车仪表知识点拆解7 图像绘制,旋转
先贴上效果图,注意,没有写逻辑,都是乱动的 看下最中心的指针旋转,这里使用的QPainter的绘制函数 要显示复杂的效果,需要分层 void Widget::draw_number_pointer() ...
- packstack安装ironic
KVM Centos7.3虚机 安装openstack Pike版本, 其它版本安装方法类似. packstack目前对NetworkManager 还不支持,我们修改下配置: systemctl d ...
- hdu 1556 Color the ball (区间更新 求某点值)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...