题目:

BZOJ3514

分析:

看到这题真的是一脸懵逼无从下手,只好膜题解。看到「森林的联通块数 = 点数 - 边数」这一句话就立刻什么都会了 QAQ 。

这题最重要的就是意识到上面那个式子(正确性显然)。那么这个问题就变成了:\([l,r]\) 中最多选出多少条边,使得图中不存在环。根据 Kruskal 的原理,贪心地选就能保证选出的边最多,所以我们不妨假定尽量选编号较大的边。

给每条边 \(i\) 设 \(nxt_i\) ,表示从 \(i\) 开始向后依次插入边,插入到 \(nxt_i\) 这条边时会出现 包含 \(i\) 的 环(不存在则为无穷大)。即,如果 \(i\) 和 \(nxt_i\) 同时可选,由于尽量选择标号大的边,所以选上 \(nxt_i\) 而把 \(i\) 删掉。\(nxt_i\) 也可理解为会「废掉」\(i\) 的第一条边。那么答案就是满足 \(i\in[l,r]\) 且 \(nxt[i]\notin[l,r]\) 的边数。这是一个二维数点问题,直接用主席树解决即可。

如何 \(O(n)\) 求出 \(nxt_i\) 呢?考虑从小到大加边,当加入边 \(i\) 时,如果出现了环,那么断掉环上最小的边 \(j\) ,则 \(nxt_j=i\) 。这个过程用 LCT 维护。

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
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;
}
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);
}
const int N = 2e5 + 10, M = 2e5 + 10, B = 20, P = N + M, INF = 0x3f3f3f3f;
class Link_Cut_Tree
{
private:
struct nulltag {};
struct node
{
int val, min;
bool revtag;
node *fa, *s[2];
node(nulltag)
: val(INF), min(INF), revtag(false)
{
fa = s[0] = s[1] = this;
}
node(const int _val)
: val(_val), min(_val), revtag(false)
{
fa = s[0] = s[1] = null;
}
}*pos[P];
static node *null;
static void update(node *const rot)
{
rot->min = min(rot->val, min(rot->s[0]->min, rot->s[1]->min));
}
static void rev(node *const rot)
{
swap(rot->s[0], rot->s[1]), rot->revtag ^= 1;
}
static void pushdown(node *const rot)
{
if (rot->revtag)
{
if (rot->s[0] != null)
rev(rot->s[0]);
if (rot->s[1] != null)
rev(rot->s[1]);
rot->revtag = 0;
}
}
static void pushdown_all(node *const rot)
{
if (!isrot(rot))
pushdown_all(rot->fa);
pushdown(rot);
}
static bool isrot(const node *const rot)
{
return rot != rot->fa->s[0] && rot != rot->fa->s[1];
}
static bool dir(const node *const rot)
{
return rot == rot->fa->s[1];
}
static void rotate(node *const rot)
{
node *f = rot->fa, *ff = f->fa;
bool d = dir(rot);
if (!isrot(f))
ff->s[dir(f)] = rot;
rot->fa = ff;
f->s[d] = rot->s[!d];
if (rot->s[!d] != null)
rot->s[!d]->fa = f;
rot->s[!d] = f;
f->fa = rot;
update(f);
}
static void splay(node *const rot)
{
pushdown_all(rot);
while (!isrot(rot))
{
node *f = rot->fa;
if (isrot(f))
rotate(rot);
else if (dir(f) ^ dir(rot))
rotate(rot), rotate(rot);
else
rotate(f), rotate(rot);
}
update(rot);
}
static void access(node *rot)
{
for (node *last = null; rot != null; last = rot, rot = rot->fa)
splay(rot), rot->s[1] = last, update(rot);
}
static void mkrot(node *const rot)
{
access(rot), splay(rot), rev(rot);
}
static node *findrot(node *rot)
{
access(rot), splay(rot);
while (rot->s[0] != null)
rot = rot->s[0];
return rot;
}
public:
bool connect(const int x, const int y)
{
return findrot(pos[x]) == findrot(pos[y]);
}
void link(const int x, const int y)
{
node *rot1 = pos[x], *rot2 = pos[y];
mkrot(rot1), splay(rot1), rot1->fa = rot2;
}
void cut(const int x, const int y)
{
node *rot1 = pos[x], *rot2 = pos[y];
mkrot(rot1), access(rot2), splay(rot1);
rot1->s[1] = rot2->fa = null;
update(rot1);
}
int query(const int x, const int y)
{
node *rot1 = pos[x], *rot2 = pos[y];
mkrot(rot1), access(rot2), splay(rot1);
return rot1->min;
}
void init(const int n, const int *const w)
{
for (int i = 1; i <= n; i++)
pos[i] = new node(w[i]);
} }lct;
typedef Link_Cut_Tree LCT;
LCT::node *LCT::null = new LCT::node(LCT::nulltag());
namespace Chairman_Tree
{
struct node
{
int sum, lt, rt;
}tree[M * B];
int cnt;
void add(int &rot, const int pre, const int lt, const int rt, const int pos, const int x)
{
rot = ++cnt;
tree[rot] = tree[pre];
tree[rot].sum += x;
if (lt == rt)
return;
int mid = (lt + rt) >> 1;
if (pos <= mid)
add(tree[rot].lt, tree[pre].lt, lt, mid, pos, x);
else
add(tree[rot].rt, tree[pre].rt, mid + 1, rt, pos, x);
}
int query(const int rot1, const int rot2, const int lt, const int rt, const int ls, const int rs)
{
if (!rot2 || (ls <= lt && rt <= rs))
return tree[rot2].sum - tree[rot1].sum;
int mid = (lt + rt) >> 1;
if (rs <= mid)
return query(tree[rot1].lt, tree[rot2].lt, lt, mid, ls, rs);
else if (ls > mid)
return query(tree[rot1].rt, tree[rot2].rt, mid + 1, rt, ls, rs);
else
return query(tree[rot1].lt, tree[rot2].lt, lt, mid, ls, rs)
+ query(tree[rot1].rt, tree[rot2].rt, mid + 1, rt, ls, rs);
}
}
typedef pair<int, int> pii;
int head[M], w[P], n, m, nxt[M];
pii arr[M];
int work()
{
using namespace Chairman_Tree;
int lastans = 0, q, type;
read(n), read(m), read(q), read(type);
for (int i = 1; i <= n; i++)
w[i] = INF;
for (int i = 1; i <= m; i++)
w[i + n] = i, nxt[i] = m + 1;
lct.init(n + m, w);
for (int i = 1; i <= m; i++)
{
int a, b;
read(a), read(b);
arr[i] = pii(a, b);
if (a == b)
{
nxt[i] = i;
continue;
}
if (lct.connect(a, b))
{
int tmp = lct.query(a, b);
nxt[tmp] = i;
lct.cut(tmp + n, arr[tmp].first);
lct.cut(tmp + n, arr[tmp].second);
}
lct.link(a, i + n), lct.link(i + n, b);
}
for (int i = 1; i <= m; i++)
add(head[i], head[i - 1], 1, m + 1, nxt[i], 1);
while (q--)
{
int l, r;
read(l), read(r);
if (type)
l ^= lastans, r ^= lastans;
write(lastans = (n - query(head[l - 1], head[r], 1, m + 1, r + 1, m + 1)));
putchar('\n');
}
return 0;
}
}
int main()
{
return zyt::work();
}

最新文章

  1. java中的移位运算符:&lt;&lt;,&gt;&gt;,&gt;&gt;&gt;总结
  2. 【BZOJ-2179&amp;2194】FFT快速傅里叶&amp;快速傅里叶之二 FFT
  3. NSDictionary 、 NSMutableDictionary
  4. wzplayer2 for windows ActiveX 试用地址
  5. oo面向对象原则
  6. poj1023
  7. xcode 发展史 及 做iOS 必须知道的小知识
  8. 利用WSGI来部署你的网站
  9. python成长之路——第八天
  10. maven生成war包的两种方式
  11. IEnumerable,IQueryable的区别
  12. The 3n + 1 problem
  13. We Chall-Encodings: URL -Writeup
  14. js三大家族offset,scroll,cliennt的区别
  15. [IB配置]PeopleSoft如何重置网关属性administrator密码
  16. TPYBoard v102 驱动28BYJ-48步进电机
  17. I/HwPointEventFilter: do not support AFT because of no config
  18. Linux+.Net Core+Nginx(在Linux上使用Nginx反向代理.Net Core 项目)
  19. Hibernate框架:CRM练习--保存客户
  20. ASP.NET Core2.1 中如何使用 Cookie和Session

热门文章

  1. flask-admin的学习使用
  2. 配置activeMQ
  3. Android从无知到有知——NO.6
  4. smartfoxserver扩展里面过滤聊天的不合法字符
  5. eclipse中导入其它的webproject遇到和解决的问题
  6. SpringBoot项目报错Cannot determine embedded database driver class for database type NONE
  7. js常用的正则表达操作
  8. WebService(2)-XML系列之用Stax操作Xml
  9. mac WebStorm 破解
  10. iOS7获取UUID以及转换MD5