我们把边按权值从大到小依次加入图中

如果加到边权$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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. luogu_4762: [CERC2014]Virus synthesis

    洛谷_4762:[CERC2014]Virus synthesis 题目描述: 初始有一个空串,利用下面的操作构造给定串\(S\).\(len(S)\leq10^5\) 1: 串开头或末尾加一个字符. ...

  3. [CERC2014]Virus synthesis【回文自动机+DP】

    [CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...

  4. bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)

    bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...

  5. BZOJ4049 [Cerc2014] Mountainous landscape

    首先对于一个给定的图形,要找到是否存在答案非常简单... 只要维护当然图形的凸包,看一下是否有线段在这条直线上方,直接二分即可,单次询问的时间复杂度$O(logn)$ 现在用线段树维护凸包,即对于一个 ...

  6. BZOJ3928 [Cerc2014] Outer space invaders

    第一眼,我勒个去...然后看到n ≤ 300的时候就2333了 首先把时间离散化,则对于一个时间的区间,可以知道中间最大的那个一定要被选出来,然后把区间分成左右两份 于是区间DP就好了,注意用左开右开 ...

  7. bzoj4046

    分组赛的题……madan原题,考试想不出来真是SB得不行 首先,从大往小加边,每次加边如果成环必然弹出环上最大边 考虑询问[x,y],如果边权在[x,y]的边弹出了小于等于y的边j,说明j不在最小生成 ...

  8. bzoj4044 [Cerc2014] Virus synthesis

    回文自动机上dp f[x]表示形成x代表的回文串所需的最小步数, 若len[x]为奇数,f[x]=len[x],因为即使有更优的,也是直接添加,没有复制操作,那样就不用从x转移了. 若len[x]为偶 ...

  9. [CERC2014] Virus synthesis

    设f[i]为形成极长回文串i的最小操作数.答案为min f[i]+n-len[i]. 在不形成偶回文的情况下形成奇回文的最小操作数为该串长度.可以不考虑(但ans赋为len). 正确性基于: 1)奇. ...

随机推荐

  1. DNS协议

    DNS Message: Header   消息头部 Question    DNS请求 Answer  回答请求的资源记录(Resource Record(s)) Authority   指向域的资 ...

  2. .net mvc onexception capture; redirectresult;

    need to set filtercontext.result=new redirectresult('linkcustompage'); done. so... ASP.NET MVC异常处理模块 ...

  3. Pyhton 学习总结 21 :fileinput模块

    fileinput模块可以对一个或多个文件中的内容进行迭代.遍历等操作.该模块的input()函数有点类似文件readlines()方法,区别在于前者是一个迭代对象,需要用for循环迭代,后者是一次性 ...

  4. 1.2 如何在visual studio 中建立C#程序

    这一节简单介绍一下怎么在visual studio 2015中建立第一个C#程序,我使用的是2015版的visual studio,不同版本可能有一些差异,不过大体上是相同的,这些信息仅供新手参考,大 ...

  5. 14. 星际争霸之php设计模式--状态模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  6. 处理SecureCRT中使用vim出现中文乱码问题

    处理SecureCRT中使用vim出现中文乱码问题 引用原文:http://blog.chinaunix.net/uid-20639775-id-3475608.html因为cat没有问题,定位是vi ...

  7. eclipse快捷方式

    虽说右键可以直接发送快捷方式到桌面,但是点击桌面图标确提示错误,偶然发现右键选个什么,配置下启动文件就ok了(就是链接到安装目录里面那个可以启动的exe),后来怎么复现不了,伤感了,不过是可以用了,还 ...

  8. 给tabBar设置图片和字体颜色的几种方法

    现在很多应用都使用到了tabBar,我们往往在给tabBar设置图片和字体的时候,当选中状态下,往往字体的颜色和图片的颜色不匹配,有时候就显得无从下手,我也常常忘了,所有写这个博客的目的,相当于给自己 ...

  9. eclipse编辑struts.xml 代码提示

    先确定xml文件 window-preferences-查询catalog 点击add 关于这个Location 先找到你下载的struts压缩包 然后找到 解压这个jar包 你会得到一些dtd文件 ...

  10. bootstrap弹出框提示框无法调用

    使用bootstrap的js插件真的很好用啊有木有!! 但是第一次使用这个弹出框跟提示框的时候就被打击了,没有反应啊!! 然而这并不是一个大问题,一句话搞定,看代码: //首先是工具提示: $(fun ...