当前位置:网站首页>P5354 [ynoi2017] yoyo OJ (tree section and bit operation)
P5354 [ynoi2017] yoyo OJ (tree section and bit operation)
2022-06-09 04:12:00 【wind__ whisper】
Preface
When thinking about violence and solving problems “ violence ” Different time , If you continue to think about optimization, you tend to drift away …
So when there is no clue , Have the courage to jump out of the original transformation !
This optimization of the bit operation type always seems not to be in my register … Need to strengthen !
analysis
It's not hard to think of a place - by - place O ( n k log 2 n ) O(nk\log^2n) O(nklog2n) Tree section method of (LCT You can just log).
Consider such an implementation : f 0 / 1 f_{0/1} f0/1 It means that a person turned out to be 0/1 The current value .
Merge : f o p = f r , f l , o p f_{op}=f_{r,f_{l,op}} fop=fr,fl,op
We can write it in the following form : f o p = ( f l , o p & f r , 1 ) ∣ ( ( f l , o p ⊕ 1 ) & f r , 0 ) f_{op}=(f_{l,op}\&f_{r,1})|((f_{l,op}\oplus1)\&f_{r,0}) fop=(fl,op&fr,1)∣((fl,op⊕1)&fr,0).
Then we can put 64 A Boolean is pressed into a unsigned long long, To quickly merge .
In this way, the complexity of k Take it off , Time complexity O ( n log 2 n ) O(n\log^2n) O(nlog2n).
Code
1ull<<i It's written in 1<<i WA For a long time …
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define ll long long
#define ull unsigned ll
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ok debug("OK\n")
inline ll read() {
ll x(0),f(1);char c=getchar();
while(!isdigit(c)) {
if(c=='-') f=-1;c=getchar();}
while(isdigit(c)) {
x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*f;
}
const int N=1e5+100;
const int mod=998244353;
bool mem1;
int n,m,k;
int op[N];
ull w[N],o;
int dep[N],siz[N],hson[N],top[N],fa[N],dfn[N],pos[N],tim;
vector<int>e[N];
void dfs1(int x,int f){
siz[x]=1;
dep[x]=dep[f]+1;
fa[x]=f;
for(int to:e[x]){
if(to==f) continue;
dfs1(to,x);
if(siz[to]>siz[hson[x]]) hson[x]=to;
siz[x]+=siz[to];
}
return;
}
void dfs2(int x,int tp){
top[x]=tp;
dfn[++tim]=x;pos[x]=tim;
if(hson[x]) dfs2(hson[x],tp);
for(int to:e[x]){
if(to==fa[x]||to==hson[x]) continue;
dfs2(to,to);
}
return;
}
#define mid ((l+r)>>1)
#define ls (k<<1)
#define rs (k<<1|1)
struct node{
ull w[2];
};
node operator + (const node &x,const node &y){
node o;
o.w[0]=(x.w[0]&y.w[1])|((~x.w[0])&y.w[0]);
o.w[1]=(x.w[1]&y.w[1])|((~x.w[1])&y.w[0]);
return o;
}
node tr1[N<<2],tr2[N<<2];
inline void pushup(int k){
tr1[k]=tr1[ls]+tr1[rs];
tr2[k]=tr2[rs]+tr2[ls];
}
void build(int k,int l,int r){
if(l==r){
int x=dfn[l];
if(op[x]==1){
tr1[k]=tr2[k]=(node){
0,w[x]};
}
else if(op[x]==2){
tr1[k]=tr2[k]=(node){
w[x],o};
}
else if(op[x]==3){
tr1[k]=tr2[k]=(node){
w[x],o^w[x]};
}
//printf("k=%d (%d %d) %llu %llu\n",k,l,r,tr1[k].w[0],tr1[k].w[1]);
return;
}
build(ls,l,mid);
build(rs,mid+1,r);
pushup(k);
//printf("k=%d (%d %d) %llu %llu\n",k,l,r,tr1[k].w[0],tr1[k].w[1]);
}
void upd(int k,int l,int r,int p){
if(l==r){
int x=dfn[l];
if(op[x]==1){
tr1[k]=tr2[k]=(node){
0,w[x]};
}
else if(op[x]==2){
tr1[k]=tr2[k]=(node){
w[x],o};
}
else if(op[x]==3){
tr1[k]=tr2[k]=(node){
w[x],o^w[x]};
}
return;
}
if(p<=mid) upd(ls,l,mid,p);
else upd(rs,mid+1,r,p);
pushup(k);
}
node ask(int k,int l,int r,int x,int y,int op){
//if(k==1) printf("ask: (%d %d) op=%d\n",x,y,op);
if(x<=l&&r<=y){
if(op==1) return tr1[k];
else return tr2[k];
}
if(y<=mid) return ask(ls,l,mid,x,y,op);
else if(x>mid) return ask(rs,mid+1,r,x,y,op);
else if(op==1) return ask(ls,l,mid,x,y,op)+ask(rs,mid+1,r,x,y,op);
else return ask(rs,mid+1,r,x,y,op)+ask(ls,l,mid,x,y,op);
}
#undef mid
#undef ls
#undef rs
node query(int x,int y){
node res1=(node){
0,o},res2=(node){
0,o};
while(top[x]!=top[y]){
if(dep[top[x]]>=dep[top[y]]){
res1=res1+ask(1,1,n,pos[top[x]],pos[x],2);
x=fa[top[x]];
}
else{
res2=ask(1,1,n,pos[top[y]],pos[y],1)+res2;
y=fa[top[y]];
}
}
if(dep[x]>=dep[y]) res1=res1+ask(1,1,n,pos[y],pos[x],2);
else res2=ask(1,1,n,pos[x],pos[y],1)+res2;
return res1+res2;
}
bool mem2;
signed main() {
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();m=read();k=read();
if(k==0) o=0;
else{
o=1;
for(int i=0;i<k;i++) o=(o<<1)|1;
}
for(int i=1;i<=n;i++) scanf("%d%llu",&op[i],&w[i]);
for(int i=1;i<n;i++){
int x=read(),y=read();
e[x].push_back(y);
e[y].push_back(x);
}
dfs1(1,0);
dfs2(1,1);
build(1,1,n);
for(int i=1;i<=m;i++){
int oop,x,y;
ull z;
scanf("%d%d%d%llu",&oop,&x,&y,&z);
if(oop==2){
op[x]=y;w[x]=z;
upd(1,1,n,pos[x]);
}
else{
node o=query(x,y);
//printf("%llu %llu\n",o.w[0],o.w[1]);
ull res(0);
for(int i=k-1;i>=0;i--){
if(o.w[0]&(1<<i)) res+=(1<<i);
else if((o.w[1]&(1<<i))&&z>=(1<<i)){
z-=(1<<i);
res+=(1<<i);
}
}
printf("%llu\n",res);
}
}
return 0;
}
/* 3 1 2 3 3 1 1 1 */
边栏推荐
- 基于PyQt5完成的图转文功能
- Variable promotion and function promotion
- (9)分支循环结构
- 基于PyQt5完成的抠图功能-程序实现
- (5) 双向数据绑定
- Merkle Patricia trie principle
- What do complex queries mean and include
- Principle analysis of the bottom plate of Ruixin micro rk3399 development board
- Simple reservation system for C language course design classroom
- [excellent design] opencv based face recognition punch in / sign in / attendance management system (the simplest basic library development, which can be based on raspberry pie)
猜你喜欢

WinForm UI interface design routine - Custom Control ProgressBar

PHP e签宝电子签名Saas API 对接流程
![[软件工具][教程]一个很好用的可以将csdn博客文章导出word的工具使用教程](/img/40/556cd8c4868a93d4cc06a4a945475d.png)
[软件工具][教程]一个很好用的可以将csdn博客文章导出word的工具使用教程
![[SWPU2019]ReverseMe](/img/82/e8160f4128bfa09af53443cf219491.png)
[SWPU2019]ReverseMe

Matting function based on pyqt5 - program implementation

C语言课程设计教室简单预约系统

Interface performance test: Web service interface test

golang---并发Goroutine

微信小程序:(异常)Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 解决方案和分析流程(这里一定有你要的答案)

Dart:基础
随机推荐
基于PyQt5完成的抠图功能-程序实现
Common port record
Software Test Engineer, to what extent can I get 1W in a month?
How to write a blueprint for the data center
(4)数据响应式
golang ---image--热力图与照片的重叠
在线摩斯密码在线翻译转换工具
【word】错误!文档中没有指定样式的文字。 1
JVM memory view and setting ideas
全国大学生信息安全竞赛(CISCN)-reverse-复现(部分)
hisi3559av100,MIPI相机输入接口调试
ACM tutorial - Hill sort
基于PyQt5完成的图转文功能
Iscc-2022-reverse-mobile- part WP
Expansion chip, hisi3559av100 I2C debugging
(7)属性绑定
举例说明tf中LSTMCell的cell、num_unit是什么意思
RAVDESS语音情感分类数据集的介绍
Kotlin basics from getting started to advanced series (Introduction) basic use of file storage
Gradle 渠道包配置