BZOJ 3772: 精神污染 (dfs序+树状数组)
CODE
#include <set>
#include <queue>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getchar());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getchar());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 100005;
struct node { int l, r, y, val; }a[MAXN<<2];
struct query { int x, y; }q[MAXN];
inline bool cmp(const node &A, const node &B) { return A.y < B.y; }
inline bool Cmp(const query &A, const query &B) { return A.y < B.y; } //zz写成了A.y<A.y调样例调了半天
int n, T[MAXN];
inline void upd(int x, int val) {
while(x <= n) T[x] += val, x += x&-x;
}
inline int qsum(int x) { int re = 0;
while(x) re += T[x], x -= x&-x;
return re;
}
int m, tot, in[MAXN], out[MAXN], tmr, f[MAXN][17], dep[MAXN];
int fir[MAXN], to[MAXN<<1], nxt[MAXN<<1], cnt;
inline void Add(int u, int v) { to[++cnt] = v; nxt[cnt] = fir[u]; fir[u] = cnt; }
void dfs(int u) {
in[u] = ++tmr; dep[u] = dep[f[u][0]] + 1;
for(int i = 1; i < 17; ++i) f[u][i] = f[f[u][i-1]][i-1];
for(int i = fir[u]; i; i = nxt[i])
if(to[i] != f[u][0])
f[to[i]][0] = u, dfs(to[i]);
out[u] = tmr;
}
inline int find(int u, int v) {
for(int j = 16; ~j; --j)
if((dep[v]-dep[u]-1)&(1<<j))
v = f[v][j];
return v;
}
inline void insert(int minx, int maxx, int miny, int maxy) {
a[++tot] = (node) { minx, maxx, miny, 1 };
if(maxy < n) a[++tot] = (node) { minx, maxx, maxy+1, -1 };
}
int main () {
read(n), read(m);
for(int i = 1, x, y; i < n; ++i)
read(x), read(y), Add(x, y), Add(y, x);
dfs(1);
for(int i = 1, x, y, z; i <= m; ++i) {
read(x), read(y);
if(in[x] > in[y]) swap(x, y);
q[i].x=in[x], q[i].y=in[y];
if(in[x] <= in[y] && out[y] <= out[x]) {
z = find(x, y);
insert(1, in[z]-1, in[y], out[y]);
if(out[z] < n) insert(in[y], out[y], out[z]+1, n);
}
else insert(in[x], out[x], in[y], out[y]);
}
sort(a + 1, a + tot + 1, cmp);
sort(q + 1, q + m + 1, Cmp);
int cur = 1; LL ans = 0;
for(int i = 1; i <= m; ++i) {
while(cur <= tot && a[cur].y <= q[i].y) {
upd(a[cur].l, a[cur].val), upd(a[cur].r+1, -a[cur].val);
++cur;
}
ans += qsum(q[i].x)-1;
}
LL all = 1ll*m*(m-1)/2;
for(int i = 2; 1ll*i*i <= ans; ++i)
while(all % i == 0 && ans % i == 0)
all /= i, ans /= i;
printf("%lld/%lld\n", ans, all);
}
BZOJ 3772: 精神污染 (dfs序+树状数组)的更多相关文章
- BZOJ 2819: Nim( nim + DFS序 + 树状数组 + LCA )
虽然vfleaking好像想卡DFS...但我还是用DFS过了... 路径上的石堆异或和=0就是必败, 否则就是必胜(nim游戏). 这样就变成一个经典问题了, 用DFS序+BIT+LCA就可以在O( ...
- BZOJ 3772: 精神污染(dfs序+主席树)
传送门 解题思路 比较神仙的一道题.首先计算答案时可以每条路径所包含的路径数,对于\(x,y\)这条路径,可以在\(x\)这处开个\(vector\)存\(y\),然后计算时只需要算这个路径上每个点的 ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- 【BZOJ】2819: Nim(树链剖分 / lca+dfs序+树状数组)
题目 传送门:QWQ 分析 先敲了个树链剖分,发现无法AC(其实是自己弱,懒得debug.手写栈) 然后去学了学正解 核心挺好理解的,$ query(a) $是$ a $到根的异或和. 答案就是$ l ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- [BZOJ1103][POI2007]大都市meg dfs序+树状数组
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
随机推荐
- [LuoguP2159][SHOI2009]舞会_动态规划_高精度_排列组合
舞会 题目链接:https://www.luogu.org/problem/P2159 数据范围:略. 题解: 不会.... 看了题解觉得自己好傻逼啊
- [Python3 填坑] 004 关于八进制
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...
- nginx文件服务器搭建
一.安装 (CentOS 7) yum install nginx -y 如果报错: [u3@L3 /]$ sudo yum install nginx -y Loaded plugins: fast ...
- Kubernetes---容器的生命周期
⒈ ⒉Init容器 介绍: Pod 能够具有一个或多个容器,应用运行在容器里面,但是它也可能有一个或多个先于应用容器启动的Init容器. Init容器与普通的容器非常像,除了如下两点: >Ini ...
- 洛谷P2178 [NOI2015]品酒大会 后缀数组+单调栈
P2178 [NOI2015]品酒大会 题目链接 https://www.luogu.org/problemnew/show/P2178 题目描述 一年一度的"幻影阁夏日品酒大会" ...
- Thinking In Java 4th Chap4 控制执行流程
Foreach语法: 例如:float f[]=new float [10]; for(float x:f){/*****/} for(char c:"Afaslkd aslfjala al ...
- python学习-6 猜拳小游戏
import random # 调用随机数模块 pc = random.randint(1,3) # 产生1-3的随机数 print("来玩个猜拳游戏吧!") a = '石头' b ...
- Django-djangorestframework-渲染模块
目录 渲染模块 渲染模块的效果 源码分析 如何自定义配置使用渲染类 自定义渲染模块 渲染模块 可以根据用户请求 URL 或 用户可接受的类型,筛选出合适的 渲染组件. reponse 数据 json ...
- MyBatis MyBatis Generator入门
一.MGB功能简介 MyBatis Generator是一个代码生成工具. MBG是如何运行的呢?它会检查所连接到的数据库的一个或者多个table,然后生成可用来访问这些table的构建(Java代码 ...
- 开始学Python 啦 ,持续不断总结中。。(转)快捷键的使用
最重要的快捷键1. ctrl+shift+A:万能命令行2. shift两次:查看资源文件新建工程第一步操作1. module设置把空包分层去掉,compact empty middle packag ...