Luogu3613

实现细节较多,详见代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define Debug(x) cout<<#x<<"="<<x<<endl
using namespace std;
typedef unsigned long long LL;//64为,要开ull
const LL INF=0-1;
inline LL read(){
register LL x=0;register char c=getchar();
while(c<48||c>57){c=getchar();}
while(c>=48&&c<=57)x=(x<<3)+(x<<1)+(c&15),c=getchar();
return x;
} const int MAXN=200005;
const int MAXM=400005; struct Edge{
int v,next;
}e[MAXM];
int first[MAXN],Ecnt=1;
inline void Add_edge(int u,int v){
e[++Ecnt]=(Edge){v,first[u]};
first[u]=Ecnt;
} int fa[MAXN],son[MAXN],size[MAXN],dep[MAXN],seg[MAXN],rev[MAXN],top[MAXN];
int n,m,K;
LL ans; inline void dfs1(int u,int pre){
dep[u]=dep[pre]+1,size[u]=1,fa[u]=pre;
for(int i=first[u];i;i=e[i].next){
int v=e[i].v;
if(v==pre) continue;
dfs1(v,u);
size[u]+=size[v];
if(size[v]>size[son[u]]) son[u]=v;
}
} inline void dfs2(int u,int pre){
seg[u]=++seg[0];
rev[seg[0]]=u;
if(son[u]){
top[son[u]]=top[u];
dfs2(son[u],u);
}
for(int i=first[u];i;i=e[i].next){
int v=e[i].v;
if(v==pre||v==son[u]) continue;//记得判重儿子不能再走
top[v]=v;
dfs2(v,u);
}
} namespace SGT{
struct data{
LL f0,f1,inv0,inv1;
data(){f0=f1=inv0=inv1=0;}
}a[MAXN<<2],ans1[MAXN],ans2[MAXN]; LL val[MAXN];int op[MAXN];
int cnt1,cnt2;
#define ls (rt<<1)
#define rs (rt<<1|1) inline LL calc(LL num,int x){
if(op[x]==1) return num&val[x];
if(op[x]==2) return num|val[x];
if(op[x]==3) return num^val[x];
} inline data update(data l,data r){
data res;
res.f0=((l.f0&r.f1)|((~l.f0)&r.f0));//想清楚
res.f1=((l.f1&r.f1)|((~l.f1)&r.f0));
res.inv0=((r.inv0&l.inv1)|((~r.inv0)&l.inv0));
res.inv1=((r.inv1&l.inv1)|((~r.inv1)&l.inv0));
return res;
} inline void pushup(int rt){
a[rt]=update(a[ls],a[rs]);
} inline void build(int rt,int l,int r){
if(l==r){
a[rt].f0=a[rt].inv0=calc(0,rev[l]);
a[rt].f1=a[rt].inv1=calc(INF,rev[l]);
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(rt);
} inline void modify(int rt,int l,int r,int pos){
if(l==r){
a[rt].f0=a[rt].inv0=calc(0,rev[l]);
a[rt].f1=a[rt].inv1=calc(INF,rev[l]);
return;
}
int mid=(l+r)>>1;
if(pos<=mid) modify(ls,l,mid,pos);
else modify(rs,mid+1,r,pos);
pushup(rt);
} inline data query(int rt,int l,int r,int x,int y){
if(l>=x&&r<=y) return a[rt];
int mid=(l+r)>>1;
if(y<=mid) return query(ls,l,mid,x,y);
if(x>mid) return query(rs,mid+1,r,x,y);
return update(query(ls,l,mid,x,y),query(rs,mid+1,r,x,y));
} inline data solve(int x,int y){
cnt1=cnt2=0;
while(top[x]!=top[y]){
if(dep[top[x]]>=dep[top[y]]){
ans1[++cnt1]=query(1,1,n,seg[top[x]],seg[x]);
x=fa[top[x]];
}
else{
ans2[++cnt2]=query(1,1,n,seg[top[y]],seg[y]);
y=fa[top[y]];
}
}
if(dep[x]>dep[y]) ans1[++cnt1]=query(1,1,n,seg[y],seg[x]);
else ans2[++cnt2]=query(1,1,n,seg[x],seg[y]);
data res;
for(int i=1;i<=cnt1;i++) swap(ans1[i].f0,ans1[i].inv0),swap(ans1[i].f1,ans1[i].inv1);//选一个方向反过来
if(cnt1){
res=ans1[1];
for(int i=2;i<=cnt1;i++) res=update(res,ans1[i]);
if(cnt2) res=update(res,ans2[cnt2]);
}
else res=ans2[cnt2];
for(int i=cnt2-1;i>=1;i--) res=update(res,ans2[i]);
return res;
}
#undef ls
#undef rs
}using namespace SGT; signed main(){
//freopen("asd.in","r",stdin);
n=read(),m=read(),K=read();
for(int i=1;i<=n;i++) op[i]=read(),val[i]=read();
for(int i=1;i<=n-1;i++){
int x=read(),y=read();
Add_edge(x,y);
Add_edge(y,x);
}
dfs1(1,0);
top[1]=1;dfs2(1,0);
build(1,1,n);
while(m--){
int opt=read(),x=read(),y=read();LL z=read();
if(opt==1){
data res=solve(x,y);ans=0;//把64的状态存到一个unsigned long long里面,优化掉k
for(int i=63;i>=0;i--){
LL tmp0=(res.f0>>i)&(1ull);
LL tmp1=(res.f1>>i)&(1ull);
if(tmp0>=tmp1||(1ull<<i)>z) ans|=(tmp0?(1ull<<i):0);//贪心,也要想清楚
else ans|=tmp1?(1ull<<i):0,z-=(1ull<<i);//所选数不能超过z
}
printf("%llu\n",ans);
}
if(opt==2){
op[x]=y,val[x]=z;
modify(1,1,n,seg[x]);
}
}
}

P3613 睡觉困难综合征(码力)的更多相关文章

  1. 【刷题】洛谷 P3613 睡觉困难综合征

    题目背景 刚立完Flag我就挂了WC和THUWC... 时间限制0.5s,空间限制128MB 因为Claris大佬帮助一周目由乃通过了Deus的题,所以一周目的由乃前往二周目世界找雪辉去了 由于二周目 ...

  2. 洛谷P3613 睡觉困难综合征(LCT)

    题目: P3613 睡觉困难综合症 解题思路: LCT,主要是维护链上的多位贪心答案,推个公式:分类讨论入0/1的情况,合并就好了(公式是合并用的) 代码(我不知道之前那个为啥一直wa,改成结构体就好 ...

  3. 洛谷P3613 睡觉困难综合征(LCT,贪心)

    洛谷题目传送门 膜拜神犇出题人管理员!!膜拜yler和ZSY!! 没错yler连续教我这个蒟蒻写起床困难综合症和睡觉困难综合症%%%Orz,所以按位贪心的思路可以继承下来 这里最好还是写树剖吧,不过我 ...

  4. P3613 睡觉困难综合征(LCT + 位运算)

    题意 NOI2014 起床困难综合症 放在树上,加上单点修改与链上查询. 题解 类似于原题,我们只需要求出 \(0\) 和 \(2^{k - 1} - 1\) 走过这条链会变成什么值,就能确定每一位为 ...

  5. [洛谷]P3613 睡觉困难综合征

    题目大意:给出一棵n个点的树,每个点有一个运算符(与.或.异或)和一个数,支持两种操作,第一种修改一个点的运算符和数,第二种给出x,y,z,询问若有一个0~z之间的数从点x走到点y(简单路径),并且对 ...

  6. [P3613]睡觉困难综合征

    Description: 给定一个n个点的树,每个点有一个操作符号 "&" "|" "^" ,以及一个权值 要求支持以下操作: 1. ...

  7. 洛谷P3613 睡觉困难综合征

    传送门 题解 人生第一道由乃…… 做这题之前应该先去把这一题给切掉->这里 我的题解->这里 然后先膜一波zsy大佬和flashhu大佬 大体思路就是先吧全0和全1的都跑答案,然后按位贪心 ...

  8. P3613 睡觉困难综合征 LCT+贪心+位运算

    \(\color{#0066ff}{ 题目描述 }\) 由乃这个问题越想越迷糊,已经达到了废寝忘食的地步.结果她发现--晚上睡不着了!只能把自己的一个神经元(我们可以抽象成一个树形结构)拿出来,交给D ...

  9. Luogu 睡觉困难综合征 ([NOI2014]起床困难综合症)

    一.[NOI2014]起床困难综合症 题目描述 网址:https://daniu.luogu.org/problemnew/show/2114 大意: 有一条链,链上每一个节点包含一个位运算f 与 一 ...

随机推荐

  1. Yii::app()

    Yii::app()返回的是你在index.php里创建的CWebApplication实例. 在一次请求处理过程中,这是个唯一的实例. Yii::app()主要负责一些全局性的功能模块,比如Yii: ...

  2. 【坑】idea+tomcat

    idea用tomcat外置容器部署debug要清掉webapp里面的同名项目,否则就呵呵呵呵了

  3. 环境变量,include搜索路径,lib库搜索路径

    环境变量 系统环境变量 我们知道,我们经常要设置一些环境变量,系统环境变量我们非常容易理解.其实我们在windows中经常容易接触.其实环境变量是一个非常广泛的一个概念,它与web应用程序中的web. ...

  4. python去掉括号之间的字符

    在字符串中识别括号并删除括号及其中的内容括号包括 大中小 3种括号 输入为 1个字符串 s="我是一个人(中国人)[真的]{确定}"; 输出为 result = "我是一 ...

  5. JAVA基础知识总结9(特殊类)

    1.Object: 所有类的直接或者间接父类,Java认为所有的对象都具备一些基本的共性内容,这些内容可以不断的向上抽取,最终就抽取到了一个最顶层的类中的,该类中定义的就是所有对象都具备的功能. 具体 ...

  6. Ajax笔记(一)

    Ajax三步骤: Asynchronous Javascript And XML 1.运用HTML和CSS实现页面,表达信息: 2.运用XMLHttpRequest和web服务器进行数据的异步交换: ...

  7. css样式文件命名规范

    样式文件命名规范 主要 master.css, style.css, main.css 布局 layout.css 专栏 columns.css 文字 font.css 打印 print.css 主题 ...

  8. 磨刀——python及相关工具

    1.python语言包 1.1去https://www.python.org/,在download栏下载最新版python2或者python3 tips:1.点击下载会很慢,推荐:迅雷,百度云盘下载, ...

  9. 283E&EZOJ #89 Cow Tennis Tournament

    传送门 分析 我们考虑用所有的情况减去不合法的情况 不难想出所有情况为$C_n^3$ 于是我们考虑不合法的情况 我们知道对于一个不合法的三元组$(a,b,c)$一定是修改后$a<b,b>c ...

  10. Entity Framework Tutorial Basics(30):

    CRUD using Stored Procedure: In the previous chapter, we have seen how to get data using a stored pr ...