BZOJ4046 [Cerc2014] Pork barre
我们把边按权值从大到小依次加入图中
如果加到边权$V$,则当前的最小生成森林中边权$v\in[V, V']$(其中$V'$是任意值)形成的森林的边权和就是对于询问$[V, V']$的答案
由于点数不多,所以可以每次暴力$dfs$找环上最大边以及暴力删除。。。
又由于是强制在线,于是用可持久化线段树维护不同权值的出现次数即可
/**************************************************************
Problem: 4046
User: rausen
Language: C++
Result: Accepted
Time:8788 ms
Memory:46132 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 1e3 + ;
const int M = 1e5 + ;
const int maxV = 1e6 + ; int read(); struct Edge {
int x, y, v; inline bool operator < (const Edge &e) const {
return v > e.v;
} inline void get() {
x = read(), y = read(), v = read();
}
} E[M]; struct edge {
int next, to, v;
edge() {}
edge(int _n, int _t, int _v) : next(_n), to(_t), v(_v) {}
} e[M << ]; struct chair_tree {
chair_tree *ls, *rs;
int sum; #define Cnt 3500000
inline void* operator new(size_t, chair_tree *_c = NULL, int f = ) {
static chair_tree mempool[Cnt], *c;
if (f) c = mempool;
if (_c == NULL)
c -> ls = c -> rs = NULL, c -> sum = ;
else *c = *_c;
return c++;
}
#undef Cnt #define mid (l + r >> 1)
void modify(int l, int r, int pos, int d) {
sum += d;
if (l == r) return;
if (pos <= mid) {
ls = new(ls)chair_tree;
ls -> modify(l, mid, pos, d);
} else {
rs = new(rs)chair_tree;
rs -> modify(mid + , r, pos, d);
}
} int query(int l, int r, int L, int R) {
if (L <= l && r <= R) return sum;
int res = ;
if (ls && L <= mid) res += ls -> query(l, mid, L, R);
if (rs && mid < R) res += rs -> query(mid + , r, L, R);
return res;
}
#undef mid
} *root[M]; int n, m, ans;
int first[N], tot;
int fa[N];
int tmp[M], len; inline void Add_Edges(int x, int y, int v) {
e[++tot] = edge(first[x], y, v), first[x] = tot;
e[++tot] = edge(first[y], x, v), first[y] = tot;
} #define y e[x].to
inline void Delete_Edge(int p, int q) {
int x;
if (e[first[p]].to == q) {
first[p] = e[first[p]].next;
return;
}
for (x = first[p]; x; x = e[x].next)
if (e[e[x].next].to == q) {
e[x].next = e[e[x].next].next;
return;
}
} inline void Delete_Edges(int p, int q) {
Delete_Edge(p, q);
Delete_Edge(q, p);
} int find_max(int p, int fa, int tar) {
int x, tmp;
for (x = first[p]; x; x = e[x].next)
if (y != fa) {
if (y == tar) return x;
tmp = find_max(y, p, tar);
if (tmp != -) {
if (e[tmp].v > e[x].v) return tmp;
return x;
}
}
return -;
}
#undef y int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
} inline int get(int x) {
return lower_bound(tmp + , tmp + len + , x) - tmp;
} int main() {
int T, now, i, x, y, Q;
for (T = read(); T; --T) {
n = read(), m = read(), ans = ;
for (i = ; i <= n; ++i) fa[i] = i;
for (i = ; i <= m; ++i) {
E[i].get();
tmp[i] = E[i].v;
}
sort(E + , E + m + );
sort(tmp + , tmp + m + ), len = unique(tmp + , tmp + m + ) - tmp - ;
memset(first, , sizeof(first)), tot = ; root[len + ] = new(NULL, )chair_tree;
for (now = len, i = ; now; --now) {
root[now] = new(root[now + ])chair_tree;
for (; E[i].v == tmp[now] && i <= m; ++i) {
x = find(E[i].x), y = find(E[i].y);
if (x != y) fa[x] = y;
else {
x = find_max(E[i].x, , E[i].y);
Delete_Edges(e[x].to, e[x ^ ].to);
root[now] -> modify(, len, get(e[x].v), -e[x].v);
}
Add_Edges(E[i].x, E[i].y, E[i].v);
root[now] -> modify(, len, get(E[i].v), E[i].v);
}
}
for (Q = read(); Q; --Q) {
x = read(), y = read();
x = upper_bound(tmp + , tmp + len + , x - ans - ) - tmp;
y = upper_bound(tmp + , tmp + len + , y - ans) - tmp - ;
if (x > y) printf("%d\n", ans = );
else printf("%d\n", ans = root[x] -> query(, len, x, y));
}
}
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}
BZOJ4046 [Cerc2014] Pork barre的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- luogu_4762: [CERC2014]Virus synthesis
洛谷_4762:[CERC2014]Virus synthesis 题目描述: 初始有一个空串,利用下面的操作构造给定串\(S\).\(len(S)\leq10^5\) 1: 串开头或末尾加一个字符. ...
- [CERC2014]Virus synthesis【回文自动机+DP】
[CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...
- bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)
bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...
- BZOJ4049 [Cerc2014] Mountainous landscape
首先对于一个给定的图形,要找到是否存在答案非常简单... 只要维护当然图形的凸包,看一下是否有线段在这条直线上方,直接二分即可,单次询问的时间复杂度$O(logn)$ 现在用线段树维护凸包,即对于一个 ...
- BZOJ3928 [Cerc2014] Outer space invaders
第一眼,我勒个去...然后看到n ≤ 300的时候就2333了 首先把时间离散化,则对于一个时间的区间,可以知道中间最大的那个一定要被选出来,然后把区间分成左右两份 于是区间DP就好了,注意用左开右开 ...
- bzoj4046
分组赛的题……madan原题,考试想不出来真是SB得不行 首先,从大往小加边,每次加边如果成环必然弹出环上最大边 考虑询问[x,y],如果边权在[x,y]的边弹出了小于等于y的边j,说明j不在最小生成 ...
- bzoj4044 [Cerc2014] Virus synthesis
回文自动机上dp f[x]表示形成x代表的回文串所需的最小步数, 若len[x]为奇数,f[x]=len[x],因为即使有更优的,也是直接添加,没有复制操作,那样就不用从x转移了. 若len[x]为偶 ...
- [CERC2014] Virus synthesis
设f[i]为形成极长回文串i的最小操作数.答案为min f[i]+n-len[i]. 在不形成偶回文的情况下形成奇回文的最小操作数为该串长度.可以不考虑(但ans赋为len). 正确性基于: 1)奇. ...
随机推荐
- 实用命令dd
1.命令简介 dd 的主要选项: 指定数字的地方若以下列字符结尾乘以相应的数字: b=512, c=1, k=1024, w=2, xm=number m if=file #输入文件名,缺省为标准输入 ...
- C# xml压缩包不解压的情况下解析xml内容
string sourceFilePath = @"E:\文件拷贝\xx\3773\3773.zip"; FileInfo fileInfo = new FileInfo(sour ...
- C#.Net 中的 new 的几个用法
之前面试的时候,有人问过我这个问题,当时自己只记得两种.后来上msdn看了下,发现有三种,第三种用法基本没怎么用过 这里先贴出来: 三种用法如下: 在 C# 中,new 关键字可用作运算符.修饰符或约 ...
- Main.C时钟设定
void Main(void) 时钟设定:400M 100M 50M 得到 1:4:8 进而需要得到 hdivn=2 : pdivn=1: i = 2 ; //用于选择CUP核的频率 ...
- 如何在page_load方法判断是服务器端控件引发的page_load方法
动态获取单击的服务器端控件的id值 private string getPostBackControlName() { Control control=null; s ...
- 生产者-消费者问题【Java实现】
生产者-消费者问题是经典的并发问题, 非常适合并发入门的编程练习. 生产者-消费者问题是指, 有若干个生产者和若干个消费者并发地读写一个或多个共享存储空间:生产者创建对象并放入到共享存储空间,消费 ...
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
http://blog.csdn.net/bao19901210/article/details/41724355 http://www.cnblogs.com/leiOOlei/p/3725911. ...
- SVN merge的主干,分支的相互合并操作
本文只研究了 在本地如何进行主干,分支的相互合并 的操作:从主干到分支,从分支到主干. 本地客户端工具是tortoisesvn 测试用例. 1.本地添加test文件夹 在test文件夹下分别建立tru ...
- mysql 允许远程登陆
参考:http://blog.chinaunix.net/uid-23215128-id-2951624.html 1.以root账户登录 2.grant all PRIVILEGES on disc ...
- arraylist与linkedlist的区别与性能测试
/** *arraylist和linkedlist的适用场合. **/ import java.util.List; import java.util.ArrayList; import java.u ...