COT2 - Count on a tree II

中文题意

离线询问一颗树上路径(u,v)中经过所有点的权值的种类数。

题解

树上莫队。即在树的欧拉序列上进行莫队。同一个点加第一次时增加,第二次时减去增加的影响。

错误记录

用了tarjan LCA,并偷懒将询问和莫队都用一个node表示。意味着每一个询问都要开两个node,写莫队排序sort的时候注意结构体数组长度!莫队排序后两个相同的node不一定相邻,因为可能出现node[i].l/size和node[j].l/size相等,但是node[i].l和node[j].l并不相等的情况。卡了半天。

题目信息不全,权值据说是1e9,别忘了离散化

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <cmath> void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = 1000000;
const int MAXM = 1000000; struct Edge
{
int u, v, nxt;
Edge(int _u, int _v, int _nxt){u = _u, v = _v, nxt = _nxt;}
Edge(){}
}edge[MAXN << 1];
int head[MAXN], cnt;
void insert(int a, int b)
{
edge[++ cnt] = Edge(a, b, head[a]), head[a] = cnt;
edge[++ cnt] = Edge(b, a, head[b]), head[b] = cnt;
} int tong[MAXN], val[MAXN], num[MAXN], id[MAXN];
int fa[MAXN], seq[MAXN], st[MAXN], et[MAXN], t, sum;
int n, m; void dfs(int x)
{
seq[++ t] = x;
st[x] = t;
for(int pos = head[x];pos;pos = edge[pos].nxt)
{
int v = edge[pos].v;
if(v == fa[x]) continue;
fa[v] = x;
dfs(v);
}
seq[++ t] = x;
et[x] = t;
} int cmp(int a, int b)
{
return val[a] < val[b];
} struct Node
{
int l, r, id, lca, nxt, u, v, need_lca;
Node(int _u, int _v, int _id, int _nxt){u = _u, v = _v, id = _id, nxt = _nxt;}
Node(){}
void init()
{
if(lca == u)
l = st[lca], r = st[v], need_lca = 0;
else if(lca == v)
l = st[lca], r = st[u], need_lca = 0;
else
{
if(st[u] < st[v]) l = et[u], r = st[v];
else l = et[v], r = st[u];
need_lca = 1;
}
return ;
}
}node[MAXM];
int head_node[MAXM], cnt_node = 1; void insert_node(int u, int v, int id)
{
node[++ cnt_node] = Node(u, v, id, head_node[u]), head_node[u] = cnt_node;
node[++ cnt_node] = Node(v, u, id, head_node[v]), head_node[v] = cnt_node;
} int ans[MAXM]; int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
} int vis[MAXN]; void dfs_tarjan(int x)
{
vis[x] = 1;
for(int pos = head[x];pos;pos = edge[pos].nxt)
{
int v = edge[pos].v;
if(vis[v]) continue;
dfs_tarjan(v);
int f1 = find(x), f2 = find(v);
fa[f2] = f1;
}
for(int pos = head_node[x];pos;pos = node[pos].nxt)
{
int v = node[pos].v;
if(vis[v])
node[pos].lca = node[pos ^ 1].lca = find(v);
}
} void tarjan_lca()
{
for(int i = 1;i <= n;++ i) fa[i] = i;
dfs_tarjan(1);
} int size; int cmp2(Node& a, Node& b)
{
return a.l/size == b.l/size ? a.r < b.r : a.l/size < b.l/size;
} int w[MAXN]; void add(int x)
{
if(w[x] == 0) tong[val[x]] += 1;
else if(w[x] == 1) tong[val[x]] -= 1; if(tong[val[x]] == 1 && w[x] == 0) ++ sum;
if(tong[val[x]] == 0 && w[x] == 1) -- sum; ++ w[x];
} void del(int x)
{
if(w[x] == 1) tong[val[x]] -= 1;
else if(w[x] == 2) tong[val[x]] += 1; if(tong[val[x]] == 1 && w[x] == 2) ++ sum;
if(tong[val[x]] == 0 && w[x] == 1) -- sum; -- w[x];
} bool is_cal[MAXM << 1]; int main()
{
read(n), read(m);
for(int i = 1;i <= n;++ i) read(val[i]), id[i] = i;
for(int i = 1;i < n;++ i)
{
int tmp1, tmp2;
read(tmp1), read(tmp2);
insert(tmp1, tmp2);
}
std::sort(id + 1, id + 1 + n, cmp); for(int i = 1, j = 1;i <= n;)
{
num[j] = val[id[i]];
while(val[id[i]] == num[j]) val[id[i]] = j, ++ i;
++ j;
} dfs(1); for(int i = 1;i <= m;++ i)
{
int tmp1, tmp2;
read(tmp1), read(tmp2);
insert_node(tmp1, tmp2, i);
} tarjan_lca(); for(int i = 2;i <= cnt_node;++ i)
node[i].init(); size = sqrt(n);
if(size == 0) size = 1;
std::sort(node + 2, node + 1 + cnt_node, cmp2); int l = 1, r = 1;
add(seq[1]);
for(int i = 2;i <= cnt_node;++ i)
{
if(is_cal[node[i].id]) continue;
is_cal[node[i].id] = 1;
while(l < node[i].l) del(seq[l]), ++ l;
while(l > node[i].l) -- l, add(seq[l]);
while(r < node[i].r) ++ r, add(seq[r]);
while(r > node[i].r) del(seq[r]), -- r; if(node[i].need_lca) add(node[i].lca);
ans[node[i].id] = sum;
if(node[i].need_lca) del(node[i].lca);
}
for(int i = 1;i <= m;++ i)
printf("%d\n", ans[i]); return 0;
}

SPOJ10707 COT2-Count on a tree II的更多相关文章

  1. SPOJ10707 COT2 - Count on a tree II 【树上莫队】

    题目分析: 考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同.然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以 ...

  2. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  3. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  4. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  5. COT2 - Count on a tree II(树上莫队)

    COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N ...

  6. 「SPOJ10707」Count on a tree II

    「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...

  7. 【SPOJ10707】COT2 - Count on a tree II

    题目大意:给定一棵 N 个节点的无根树,每个节点有一个颜色.现有 M 个询问,每次询问一条树链上的不同颜色数. 题解:学会了树上莫队. 树上莫队是将节点按照欧拉序进行排序,将树上问题转化成序列上的问题 ...

  8. SPOJ COT2 Count on a tree II(树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...

  9. SPOJ COT2 Count on a tree II (树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...

  10. [SP10707]COT2 - Count on a tree II

    题目大意:有一棵$n$个节点的树,第$i$个点有一个颜色$C_i$,$m$组询问,每次问$x->y$的路径上有多少种颜色 题解:树上莫队,把树按欧拉序展开成一条链,令第$i$个节点第一次出现在序 ...

随机推荐

  1. NX二次开发-创建经典工具栏UF_UI_create_toolbar

    NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...

  2. C#利用栈实现字符串运算解析

    附上参考文章链接:https://blog.csdn.net/qq_34831781/article/details/80104219 本人整合修复一些bug后的代码 using System; us ...

  3. bootstrap中container和container-fluid的区别

    container和container-fluid 在bootstrap中,两者都是设置文本居中,但是它们还是有很大差别的 container 是随屏幕宽度的变化而变化的,是阶段性变化,有一个随浏览器 ...

  4. PAT_A1020#Tree Traversals

    Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...

  5. Android Telephony分析(二) ---- RegistrantList详解

    前言 本文主要讲解RegistrantList的原理,以及如何快速分析RegistrantList相关的代码流程.在Telephony模块中,在RIL.Tracker(ServiceStateTrac ...

  6. 《DSP using MATLAB》Problem 8.43

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. 全球CMOS图像传感器厂商

    近期,台湾地区的Yuanta Research发布报告,介绍了其对CMOS图像传感器(CIS)市场的看法,以及到2022年的前景预期. 从该研究报告可以看出,2018年全球CMOS图像传感器的市场规模 ...

  8. 连接mysql并查询

    1.将mysql-connector-java-5.1.7-bin.jar放入Jmeter安装目录的bin文件夹中 2.在顶层目录<测试计划>中加载驱动 3.添加JDBC Connecti ...

  9. 多版本JDK 切换

    由于一些原因,我本机存在3个版本的jdk. 但是发现,单纯去 修改环境变量,并没有效果. 那么我们下面看看怎么改 1 查看本机版本    java -version 2 查看jdk路径   where ...

  10. AndroidStudio WiFi调试插件

    前言 此篇博客也是Android studio插件篇的一部分,后续有时间我会介绍更多AndroidStudio的插件方便开发. Android设备用WiFi调试在以前一般是通过adb连接的,但是这样的 ...