题目:

洛谷4219

分析:

很明显,查询的是删掉某条边后两端点所在连通块大小的乘积。

有加边和删边,想到LCT。但是我不会用LCT查连通块大小啊。果断弃了

有加边和删边,还跟连通性有关,于是开始yy线段树分治做法(不知道线段树分治?推荐一个伪模板题BZOJ4025二分图事实上这个链接是指向我的博客的)。把每次操作(加边或查询)看做一个时刻,一条边存在的区间就是它加入后没有被查询的时间区间的并。于是用可撤销并查集维护一下连通块大小即可。

代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <map>
#include <cassert>
#undef i
#undef j
#undef k
#undef min
#undef max
#undef true
#undef false
#undef swap
#undef sort
#undef if
#undef for
#undef while
#undef printf
#undef scanf
#undef putchar
#undef getchar
#define _ 0
using namespace std; namespace zyt
{
template<typename T>
inline bool read(T &x)
{
char c;
bool f = false;
x = 0;
do
c = getchar();
while (c != EOF && c != '-' && !isdigit(c));
if (c == EOF)
return false;
if (c == '-')
f = true, c = getchar();
do
x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f)
x = -x;
return true;
}
inline bool read(char &c)
{
do
c = getchar();
while (c != EOF && !isgraph(c));
return c != EOF;
}
template<typename T>
inline void write(T x)
{
static char buf[20];
char *pos = buf;
if (x < 0)
putchar('-'), x = -x;
do
*pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf)
putchar(*--pos);
}
typedef long long ll;
const int N = 1e5 + 10, B = 17, QUERY = 0, ADD = 1;
int n, q;
ll ans[N];
struct node
{
bool type;
int x, y;
}arr[N];
namespace UFS
{
int fa[N], rk[N], size[N], top;
struct node
{
int x, y, fax, rky, sizey;
}stack[N];
void init(const int n)
{
for (int i = 1; i <= n; i++)
fa[i] = i, rk[i] = size[i] = 1;
}
int f(const int u)
{
return fa[u] == u ? u : f(fa[u]);
}
bool merge(const int u, const int v)
{
int x = f(u), y = f(v);
if (x == y)
return false;
if (rk[x] > rk[y])
swap(x, y);
stack[top++] = (node){x, y, fa[x], rk[y], size[y]};
fa[x] = y, size[y] += size[x];
if (rk[x] == rk[y])
++rk[y];
return true;
}
int query(const int u)
{
assert(f(u) < N);
return size[f(u)];
}
void undo(const int bck)
{
while (top > bck)
{
--top;
int x = stack[top].x, y = stack[top].y;
assert(x < N && y < N);
fa[x] = stack[top].fax;
rk[y] = stack[top].rky;
size[y] = stack[top].sizey;
}
}
}
namespace Segment_Tree
{
struct edge
{
int x, y, next;
}e[N * (B + 1)];
int head[1 << (B + 1) | 11], ecnt;
inline void init()
{
memset(head, -1, sizeof(head));
}
inline void add(const int a, const int b, const int c)
{
e[ecnt] = (edge){b, c, head[a]}, head[a] = ecnt++;
}
inline void insert(const int rot, const int lt, const int rt, const int ls, const int rs, const int x, const int y)
{
if (ls <= lt && rt <= rs)
{
add(rot, x, y);
return;
}
int mid = (lt + rt) >> 1;
if (ls <= mid)
insert(rot << 1, lt, mid, ls, rs, x, y);
if (rs > mid)
insert(rot << 1 | 1, mid + 1, rt, ls, rs, x, y);
}
inline void solve(const int rot, const int lt, const int rt)
{
int bck = UFS::top;
for (int i = head[rot]; ~i; i = e[i].next)
UFS::merge(e[i].x, e[i].y);
if (lt == rt)
{
if (arr[lt].type == QUERY)
ans[lt] = (ll)UFS::query(arr[lt].x) * UFS::query(arr[lt].y);
UFS::undo(bck);
return;
}
int mid = (lt + rt) >> 1;
solve(rot << 1, lt, mid);
solve(rot << 1 | 1, mid + 1, rt);
UFS::undo(bck);
}
}
map<pair<int, int>, int> lastins;
int work()
{
read(n), read(q);
UFS::init(n);
Segment_Tree::init();
for (int i = 1; i <= q; i++)
{
char opt;
read(opt), read(arr[i].x), read(arr[i].y);
if (arr[i].x > arr[i].y)
swap(arr[i].x, arr[i].y);
arr[i].type = (opt == 'Q' ? QUERY : ADD);
pair<int, int> p = make_pair(arr[i].x, arr[i].y);
if (arr[i].type == ADD)
lastins[p] = i;
else
{
Segment_Tree::insert(1, 1, q, lastins[p], i - 1, p.first, p.second);
lastins[p] = i + 1;
}
}
for (map<pair<int, int>, int>::iterator it = lastins.begin(); it != lastins.end(); it++)
if (it->second <= q)
Segment_Tree::insert(1, 1, q, it->second, q, it->first.first, it->first.second);
Segment_Tree::solve(1, 1, q);
for (int i = 1; i <= q; i++)
if (arr[i].type == QUERY)
write(ans[i]), putchar('\n');
return (0^_^0);
}
}
int main()
{
return zyt::work();
}

【洛谷4219】[BJOI2014]大融合(线段树分治)的更多相关文章

  1. 洛谷4219 BJOI2014大融合(LCT维护子树信息)

    QWQ 这个题目是LCT维护子树信息的经典应用 根据题目信息来看,对于一个这条边的两个端点各自的\(size\)乘起来,不过这个应该算呢? 我们可以考虑在LCT上多维护一个\(xv[i]\)表示\(i ...

  2. 洛谷 P4219 [BJOI2014]大融合 解题报告

    P4219 [BJOI2014]大融合 题目描述 小强要在\(N\)个孤立的星球上建立起一套通信系统.这套通信系统就是连接\(N\)个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的 ...

  3. 洛谷P4219 - [BJOI2014]大融合

    Portal Description 初始有\(n(n\leq10^5)\)个孤立的点,进行\(Q(Q\leq10^5)\)次操作: 连接边\((u,v)\),保证\(u,v\)不连通. 询问有多少条 ...

  4. 洛谷 P4219 [BJOI2014]大融合

    查询,就相当于先删去这条边,然后查询边的两个端点所在连通块大小,乘起来得到答案,然后再把边加回去 可以用线段树分治做 #pragma GCC optimize("Ofast") # ...

  5. 洛谷 P2147 [SDOI2008]洞穴勘测 (线段树分治)

    题目链接 题解 早就想写线段树分治的题了. 对于每条边,它存在于一段时间 我们按时间来搞 我们可把一条边看做一条线段 我们可以模拟线段树操作,不断分治下去 把覆盖\(l-r\)这段时间的线段筛选出来, ...

  6. 洛谷P4219 [BJOI2014]大融合(LCT,Splay)

    LCT维护子树信息的思路总结与其它问题详见我的LCT总结 思路分析 动态连边,LCT题目跑不了了.然而这题又有点奇特的地方. 我们分析一下,查询操作就是要让我们求出砍断这条边后,x和y各自子树大小的乘 ...

  7. 洛谷P4219 [BJOI2014]大融合(LCT)

    LCT维护子树信息的思路总结与其它问题详见我的LCT总结 思路分析 动态连边,LCT题目跑不了了.然而这题又有点奇特的地方. 我们分析一下,查询操作就是要让我们求出砍断这条边后,x和y各自子树大小的乘 ...

  8. 洛谷 P3373 【模板】线段树 2

    洛谷 P3373 [模板]线段树 2 洛谷传送门 题目描述 如题,已知一个数列,你需要进行下面三种操作: 将某区间每一个数乘上 xx 将某区间每一个数加上 xx 求出某区间每一个数的和 输入格式 第一 ...

  9. 【BZOJ-4530】大融合 线段树合并

    4530: [Bjoi2014]大融合 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 280  Solved: 167[Submit][Status] ...

  10. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

随机推荐

  1. 洛谷 1017 进制转换 (NOIp2000提高组T1)

    [题解] 纯模拟题. 我们都知道十进制数化成m进制数可以用短除法,即除m取余.逆序排列.而m进制数化为十进制数,按权展开求和即可. 但在本题中进制的基数R可能为负数,我们知道a%R的符号与R一致,也就 ...

  2. mysql根据用户的邀请码查询该用户所有的上级

    SELECT T1.lvl AS 'level', T2.id AS 'id', T2.zid AS 'zid', T2.self_invite AS 'selfInvite', T2.invite_ ...

  3. hadoop balancer 二

    1.每次迭代一个datanode会移动不超过如下两者中较小的一个的数据量 1)10G 2)能力阈值 dfs.datanode.balance.bandwidthPerSec 每次迭代时间不会超过20分 ...

  4. cocos2dx 用命令行进行编译的指令

    cocos compile -p android -m release -s F:\cocosproj\antest --ap android-20 上面那条就是我常用的指令,这指令修改一下就成了你的 ...

  5. H5存储------localStorage和sessionStorage

    web现在随着计算机的高速发展,客户端干的事情越来越多了,随着事情的增多,很多东西存储就不止在服务器了,本地存储越来越强大了(大神原谅我废话了

  6. HBase的集群搭建

    前提:已经安装过jdk,HDFS集群和zookeeper,我的集群规划见HDFS的文章中 1.在1上安装配置hbase 下载:http://mirror.bit.edu.cn/apache/hbase ...

  7. Thread的方法join()使用

    join()的作用:Waits for this thread to die.等待线程对象销毁.在Thread源码中可以看到join源码是使用了wait()方法来实现等待功能. 因为join()内部使 ...

  8. 10、Java并发性和多线程-线程安全与不可变性

    以下内容转自http://ifeve.com/thread-safety-and-immutability/: 当多个线程同时访问同一个资源,并且其中的一个或者多个线程对这个资源进行了写操作,才会产生 ...

  9. Android: 关于百度地图缩放级别

    18 ~ 3 {"50m","100m","200m","500m","1km","2km ...

  10. Python3基础(十一) 类的拓展

    在类的初印象中,我们已经简单的介绍了类,包括类的定义.类对象和实例对象.本文将进一步学习类的继承.迭代器.发生器等等. 一.类的继承 单继承 派生类的定义如下: class DerivedClassN ...