当前位置: 首页 > news >正文

[BZOJ3757] 苹果树

嘟嘟嘟vjudge

树上莫队入门题,还没有修改。
刚做完糖果公园,顿时觉得这题水爆了。

注意有\(a = b\)的情况。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e4 + 5;
const int maxm = 1e5 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

const int S = 1357;
int n, m, col[maxn];

struct Edge
{
  int nxt, to;
}e[maxn << 1];
int head[maxn], ecnt = -1;
void addEdge(int x, int y)
{
  e[++ecnt] = (Edge){head[x], y};
  head[x] = ecnt;
}

int bel[maxn], st[maxn], top = 0, cntB = 0;
int dep[maxn], fa[21][maxn];
void dfs(int now, int _f)
{
  for(int i = 1; (1 << i) <= dep[now]; ++i)
    fa[i][now] = fa[i - 1][fa[i - 1][now]];
  int tp = top;
  for(int i = head[now], v; i != -1; i = e[i].nxt)
    {
      if((v = e[i].to) != _f)
    {
      dep[v] = dep[now] + 1;
      fa[0][v] = now;
      dfs(v, now);
      if(top - tp >= S)
        {
          cntB++;
          while(top > tp) bel[st[top--]] = cntB;
        }
    }
    }
  st[++top] = now;
}
int lca(int x, int y)
{
  if(dep[x] < dep[y]) swap(x, y);
  for(int i = 20; i >= 0; --i)
    if(dep[x] - (1 << i) >= dep[y]) x = fa[i][x];
  if(x == y) return x;
  for(int i = 20; i >= 0; --i)
    if(fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
  return fa[0][x];
}

struct Node
{
  int x, y, id, a, b;
  bool operator < (const Node& oth)const
  {
    if(bel[x] != bel[oth.x]) return bel[x] < bel[oth.x];
    if(bel[y] != bel[oth.y]) return bel[y] < bel[oth.y];
    return id < oth.id;
  }
}q[maxm];

bool vis[maxn];
int tot[maxn], ans[maxm], cnt = 0;

void Rev(int now)
{
  if(vis[now])
    {
      if(!--tot[col[now]]) cnt--;
    }
  else if(!tot[col[now]]++) cnt++;
  vis[now] ^= 1;
}
void move(int x, int y)
{
  int z = lca(x, y);
  while(x != z) Rev(x), x = fa[0][x];
  while(y != z) Rev(y), y = fa[0][y];
}

int main()
{
  Mem(head, -1);
  n = read(); m = read();
  for(int i = 1; i <= n; ++i) col[i] = read();
  for(int i = 1; i <= n; ++i)
    {
      int x = read(), y = read();
      addEdge(x, y); addEdge(y, x);
    }
  dfs(1, 0);
  while(top) bel[st[top--]] = cntB;
  for(int i = 1; i <= m; ++i)
    q[i].id = i, q[i].x = read(), q[i].y = read(), q[i].a = read(), q[i].b = read();
  sort(q + 1, q + m + 1);
  int px = 1, py = 1;
  for(int i = 1; i <= m; ++i)
    {
      move(px, q[i].x), px = q[i].x;
      move(py, q[i].y), py = q[i].y;
      Rev(lca(px, py));
      ans[q[i].id] = cnt;
      if(tot[q[i].a] && tot[q[i].b] && q[i].a != q[i].b) ans[q[i].id]--;
      Rev(lca(px, py));
    }
  for(int i = 1; i <= m; ++i) write(ans[i]), enter;
  return 0;
}

转载于:https://www.cnblogs.com/mrclr/p/10082625.html

相关文章:

  • PythonSelenium 数据驱动【unittest+ddt+mysql】
  • shell 判断是否是目录
  • taglist-plus 安装使用
  • Eureka控制台参数说明
  • restful 规范
  • NOIP2018爆炸记
  • JavaScript中的attachEvent和addEventListener
  • 12.9
  • Redis之分布式锁
  • eclipse中的debug模式的使用
  • Qt中中文字符 一劳永逸的解决方法
  • 4、基本命令-系统管理
  • 数据库期末考试
  • pandas (loc、iloc、ix)的区别
  • 【.NET开发之美】使用ComponentOne提高.NET DataMap中的加载速度
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • AHK 中 = 和 == 等比较运算符的用法
  • Apache Pulsar 2.1 重磅发布
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • CSS中外联样式表代表的含义
  • C学习-枚举(九)
  • download使用浅析
  • ES6系统学习----从Apollo Client看解构赋值
  • iOS编译提示和导航提示
  • java第三方包学习之lombok
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • Python socket服务器端、客户端传送信息
  • React-redux的原理以及使用
  • Redux系列x:源码分析
  • spring cloud gateway 源码解析(4)跨域问题处理
  • SQLServer之创建显式事务
  • Vue组件定义
  • WePY 在小程序性能调优上做出的探究
  • 不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 基于webpack 的 vue 多页架构
  • 山寨一个 Promise
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 一文看透浏览器架构
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • #define用法
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • (13):Silverlight 2 数据与通信之WebRequest
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (四)图像的%2线性拉伸
  • (算法)Game
  • (五)c52学习之旅-静态数码管
  • (转)Sublime Text3配置Lua运行环境
  • .NET Core 项目指定SDK版本
  • .net Signalr 使用笔记
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • /ThinkPHP/Library/Think/Storage/Driver/File.class.php  LINE: 48