Cow Land (树链剖分)

2023-11-11

测试链接

题面:

Cow Land is a special amusement park for cows, where they roam around, eat delicious grass, and visit different cow attractions (the roller cowster is particularly popular).

There are a total of N different attractions (2≤N≤1e5 ). Certain pairs of attractions are connected by pathways, N-1 in total, in such a way that there is a unique route consisting of various pathways between any two attractions. Each attraction?ii?has an integer enjoyment value ei, which can change over the course of a day, since some attractions are more appealing in the morning and others later in the afternoon.

A cow that travels from attraction i to attraction j gets to experience all the attractions on the route from i to j. Curiously, the total enjoyment value of this entire route is given by the bitwise XOR of all the enjoyment values along the route, including those of attractions i and j.

Please help the cows determine the enjoyment values of the routes they plan to use during their next trip to Cow Land.

INPUT
The first line of input contains N and a number of queries Q (1≤Q≤1e5). The next line contains e1…eN (0≤ei≤1e9). The next N lines each describe a pathway in terms of two integer attraction IDs a and b (both in the range 1…N). Finally, the last Q?lines each describe either an update to one of the ei values or a query for the enjoyment of a route. A line of the form “1 i v” indicates that ei should be updated to value v, and a line of the form “2 i j” is a query for the enjoyment of the route connecting attractions i and j. In test data worth at most 50% of points, there will be no changes to the values of the attractions.

OUTPUT
For each query of the form “2 i j“, print on a single line the enjoyment of the route from i to j.

1 2 4 8 16
1 2
1 3
3 4
3 5
2 1 5
1 1 16
2 3 5
2 1 5
2 1 3

21
20
4
20



题意:

有一棵n个节点的树,每个节点有一个值。
有q个操作,操作分两种:
1 x y:修改节点x的值为y
2 x y:查询点x和点y之间路径上所有点的异或(包括这两个点)

思路:

树链剖分维护区间异或,修改是单点修改,查询是区间查询,都很基础。

参考代码:
#include<bits/stdc++.h>
using namespace std;

#define lson rt<<1
#define rson rt<<1|1

const int maxn=2e5+5;

int n,m;

int val[maxn],nxb[maxn];
struct node
{
    int u,v,nxt;
    node(){}
    node(int u,int v,int nxt):u(u),v(v),nxt(nxt){}
}edge[maxn<<2];
int cnt;
int head[maxn<<2];
int dep[maxn],fa[maxn],sonsize[maxn],hson[maxn],topfa[maxn];
int newval[maxn];
void addedge(int u,int v)
{
    edge[++cnt]=node(u,v,head[u]);
    head[u]=cnt;
}
int tree[maxn<<2],lazy[maxn<<2];

void bulid(int rt,int l,int r)
{
    if(l==r)
    {
        tree[rt]=newval[++cnt];
        return;
    }
    int mid=(l+r)>>1;
    bulid(lson,l,mid);
    bulid(rson,mid+1,r);
    tree[rt]=tree[lson]^tree[rson];
}

void update(int rt,int l,int r,int x,int y,int v)
{
    if(l==r&&l==x&&r==y)
    {
        tree[rt]=v;
        return;
    }
    int mid=(l+r)>>1;
    if(y<=mid)update(lson,l,mid,x,y,v);
    else if(x>=mid+1)update(rson,mid+1,r,x,y,v);
    else update(lson,l,mid,x,mid,v),update(rson,mid+1,r,mid+1,y,v);
    tree[rt]=tree[lson]^tree[rson];
}

int query(int rt,int l,int r,int x,int y)
{
    if(l>=x&&r<=y)
    {
        return tree[rt];
    }
    int mid=(l+r)>>1;
    int ret=0;
    if(y<=mid)ret=query(lson,l,mid,x,y);
    else if(x>=mid+1)ret=query(rson,mid+1,r,x,y);
    else ret=(query(lson,l,mid,x,mid)^query(rson,mid+1,r,mid+1,y));
    return ret;
}

void dfs1(int u,int f,int deep)
{
    dep[u]=deep;
    fa[u]=f;
    sonsize[u]=1;
    hson[u]=0;
    int maxson=-1;
    for(int i=head[u];~i;i=edge[i].nxt)
    {
        int v=edge[i].v;
        if(v==f)continue;
        dfs1(v,u,deep+1);
        sonsize[u]+=sonsize[v];
        if(sonsize[v]>maxson)maxson=sonsize[v],hson[u]=v;
    }
}

void dfs2(int u,int topf)
{
    nxb[u]=++cnt;
    newval[cnt]=val[u];
    topfa[u]=topf;
    if(!hson[u])return;
    dfs2(hson[u],topf);
    for(int i=head[u];~i;i=edge[i].nxt)
    {
        int v=edge[i].v;
        if(v==fa[u]||v==hson[u])continue;
        dfs2(v,v);
    }
}

int sum_lian(int u,int v)
{
    int ret=0;
    while(topfa[u]!=topfa[v])
    {
        if(dep[topfa[u]]<dep[topfa[v]])swap(u,v);
        ret^=query(1,1,n,nxb[topfa[u]],nxb[u]);
        u=fa[topfa[u]];
    }
    if(dep[u]>dep[v])swap(u,v);
    ret=ret^query(1,1,n,nxb[u],nxb[v]);
    return ret;
}

void up_point(int u,int k){
    update(1,1,n,nxb[u],nxb[u],k);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&val[i]);
    int x,y;
    cnt=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        addedge(x,y);
        addedge(y,x);
    }
    cnt=0;
    dfs1(1,0,1);
    dfs2(1,1);
    cnt=0;
    bulid(1,1,n);
    int op;
    while(m--)
    {
        scanf("%d%d%d",&op,&x,&y);
        if(op==1){
            up_point(x,y);
        }
        else {
            printf("%d\n",sum_lian(x,y));
        }
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cow Land (树链剖分) 的相关文章

随机推荐

  • Lock 接口与 synchronized 关键字的区别

    拷贝别的博主总结的9点不同 1 JDK版本不同 synchronized关键字产生于JKD1 5之前 是低版本保证共享资源同步访问的主要技术 Lock接口产生于JDK1 5版本 位于著名的java util concurrent并发包中 是
  • 2018年WiFi、5G和蓝牙的发展以及与VR/AR的联系

    52VR大幅修正了原译文的翻译错误并作润饰编辑 这份文章中涵盖对无线技术们在2018年的表现之期待 包括可能实现的时间 以及它们将可能会怎样影响AR VR的版图 首先我们展望一下 不得不说的是2018年将是很多技术大转折的一年 这其中包括手
  • delphi listview动态添加图片_Qml组件化编程4-i18n动态国际化

    简介 本文是 Qml组件化编程 系列文章的第四篇 涛哥将教大家 如何在Qml中实现动态国际化 i18n 是 internationalization 国际化 的首尾字符加中间的 18 个字符 随着产品越做越大 要推向国际的时候 国际化这一步
  • C语言指针详解

    C语言指针详解 字符指针 1 如何定义 2 类型和指向的内容 3 代码例子 指针数组 1 如何定义 2 类型和内容 数组指针 1 如何定义 2 类型和指向类型 3 数组名vs 数组名 数组指针运用 数组参数 指针参数 一维数组传参 二维数组
  • 多级页表的优点和缺点

    多级页表是基于虚拟地址的分段来划分等级的 最低等级的页表上保存了最终的虚拟页号和物理页号的对应关系 例如拿32位的虚拟地址来说 如果页面的大小为4K 也就是12位 那么地址空间内将有20位 也就是1M的页表项目 每个项目对应一个虚拟页面 那
  • 机器学习2:朴素贝叶斯分类器Naïve Bayes Classifier(基于R language&Python)

    朴素贝叶斯是基于贝叶斯定理与特征条件独立假设的分类方法 朴素贝叶斯法与贝叶斯估计是不同的概念 对于给定的训练数据集 首先基于特征条件独立假设学习输入 输出的联合概率分布 然后基于此模型 对个给定的输入 x x x 利用贝叶斯定理求出后验概率
  • Unity基础篇:Unity2D图集(2):将剪裁好的图片导出。

    转载http blog csdn net hongyouwei article details 45011315 这位大佬讲的很好 但是他没有很好地考虑到我等小白的感受 故在此补充说明 1 在Unity的Project窗口下的Assets里
  • Kubernetes:全面了解 Deployment

    本文为作者的 Kubernetes 系列电子书的一部分 电子书已经开源 欢迎关注 电子书浏览地址 https k8s whuanle cn 适合国内访问 https ek8s whuanle cn gitbook Deployment 是
  • C语言练习

    大家好啊 我是一名职高的学生 即将面临就业 在就业和升本中选择了升本 这四年期间学的专业是计算机网络技术 第一年学过C语言也都忘得差不多了 这个暑假重新开始学的C语言并且从今天起开始写博客 下面这些题目是我在书上和一些资料上做的编程题 都是
  • HashMap、HashTable和Vector的存储扩容解析

    HashMap HashTable和Vector是面试时比较高频问到的知识点 今天就从三个的底层源码的角度分析三者之间的存储 扩容原理和异同点 HashMap 实现Map接口 实现原理 HashMap采用链地址法 即底层是一个数组实现 数组
  • c语言中gets函数可以输入空格吗_C语言 gets()和scanf()函数的区别

    scanf 函数和gets 函数都可用于输入字符串 但在功能上有区别 若想从键盘上输入字符串 hi hello 则应该使用 gets 函数 gets可以接收空格 而scanf遇到空格 回车和Tab键都会认为输入结束 所有它不能接收空格 ch
  • 2023年探究区块链交易所开发:安全、效率和监管问题

    区块链技术已经成为数字经济领域的热门话题 随着数字资产市场的迅速发展 数字资产交易所也开始成为越来越重要的交易场所 本篇报告将从技术角度出发 探讨区块链交易所的开发 分析目前区块链交易所存在的问题以及未来的发展趋势 Background 区
  • Vue最常见的面试题以及答案(面试必过)

    Vue常见面试题 Vue的优点 说说你对SPA单页面的理解 它的优缺点分别是什么 SPA首屏加载速度慢的怎么解决 Vue初始化过程中 new Vue options 都做了什么 对MVVM的理解 Vue数据双向绑定原理 Vue的响应式原理
  • sqli-labs第十一关

    less11 POST Error Based Single quotes String 基于错误的POST型单引号字符型注入 第十一关开始进入登录框这种模式 像登陆框这种模式也是可以当成sql语句注入的 你想想啊 它动态的页面 通过pos
  • 【论文阅读】文献阅读笔记

    论文阅读 论文阅读笔记 逆向工程与自动化控制应用的视图规划问题综述 Conclusion 求解VPP 视图规划view planing 问题 建立一种扫描计划来对目标进行重建 通常可根据输入的数据类型进行定义采用的方法 方法1 基于目标对象
  • 清华同方台式计算机 U盘启动,清华同方台式机BIOS设置U盘启动方法

    清华同方计算机还以节能闻名 实现了稳定的销售增长 最近 清华同方计算机的一个用户问如何设置U盘启动 接下来是我为大家收集的清华同方台式机BIOS设置U盘启动的方法 欢迎大家阅读 清华同方台式机BIOS设置U盘启动方法 1 使用U教授制作启动
  • CSDN新导航的体验

    看到新导航有几天了 当时只有一个感觉 速度快了 今天开始正式使用了 却发现很大的不便 所谓不便 其实是习惯上的改变造成了我的不适应 用户习惯问题 我建议还是不要改变的好 一个界面看习惯了 突然换掉 一时接受不了 另外值得一提的是 也是很多C
  • lol8月7号服务器维护,LOL8月7日更新了什么内容 8.15新版本更新维护公告

    文章目录 LOL在今天也就是8月7日开启了一次新版本8 15版本的更新维护 这次的更新内容主要是针对8 1版本一些改动补充 所以内容不是非常的多 下面就来为大家详细的分享一下LOL的更新维护公告 亲爱的召唤师 LOL将在 8 月 7 日 4
  • python金融数据分析

    python金融数据分析 基金数据分析 https github com memsploitable foundsDataAnalysis git 股票数据分析 大行情数据分析 https github com memsploitable
  • Cow Land (树链剖分)

    测试链接 题面 Cow Land is a special amusement park for cows where they roam around eat delicious grass and visit different cow