首页 > 其他分享 > Codeforces Round 368 (Div. 2) D. Persistent Bookcase 主席树维护bitset

Codeforces Round 368 (Div. 2) D. Persistent Bookcase 主席树维护bitset

时间:2023-03-21 21:25:38浏览次数:39  
标签:rt cout int shelf Persistent Codeforces Bookcase bitset op

在学主席树时找到了这道题

本来yyyy了一个二维的主席树这种东西,然后发现很多信息好像维护不了

观察到n和m都很小,考虑把一整行看成一个节点,开一个bitset

然后区间取反、单点修改,就都可以直接做啦。

最开始不敢直接这么做,总觉得在结构体里再封装一个bitset太大

但其实还好,时间复杂度1000/w=15.625,这题还能接受

学了一种新的写法,之前的update总是用void 搭配 &传参

这题还需要类似push_up地合并子区间信息,用return 当前节点编号的写法会清晰很多。

    有个笨蛋没算好空间,开太小wa,开不够re,开太大me,中间不小心ce 是谁我不说:D J就是说正常是节点数的32倍,但这题大概能开多大开多大。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
const int N=maxn*maxn*2;
int History=0,n,m,q;
struct tree{
    int ls,rs,sum;
    bitset<maxn>f;
}t[N];
int rt[N],tot;
int op_123(int p,int shelf,int pos,int op,int l=1,int r=n){
    int q=++tot;
    t[q]=t[p];// 其实不会cost太多时间,1000/w 
    //cout<<p<<" "<<shelf<<" "<<pos<<" "<<l<<" "<<r<<endl;
    if(l==r){ 
        if(pos!=-1 ) {
            if(op==1) t[q].f[pos]=1;
            else t[q].f[pos]=0;
        }
        else {for(int j=1;j<=m;j++) t[q].f.flip(j);}
        t[q].sum=t[q].f.count();
        return q;// 这种写法扩展性好像更强一点,起码清楚、支持合并子信息 
    }
    int mid=l+r>>1;
    if(shelf<=mid) t[q].ls=op_123(t[p].ls,shelf,pos,op,l,mid);
    else t[q].rs=op_123(t[p].rs,shelf,pos,op,mid+1,r);
    t[q].sum=t[t[q].ls].sum+t[t[q].rs].sum;
    return q;
}
int main(){
    //freopen("lys.in","r",stdin);
    cin>>n>>m>>q;
    for(int i=1;i<=q;i++){
        int op;cin>>op;
        //cout<<":: "<<op<<endl;
        if(op==1){
            int x,y;cin>>x>>y;
            //cout<<x<<" "<<y<<endl;
            rt[i]=op_123(rt[i-1],x,y,1);
        }
        if(op==2){
            int x,y;cin>>x>>y;
            rt[i]=op_123(rt[i-1],x,y,2);
        }
        if(op==3){
            int x;cin>>x;
            //op_3(int& p,int shelf,int k,int l=1,int r=n){
            rt[i]=op_123(rt[i-1],x,-1,3);
        }
        if(op==4){
            int k;cin>>k;
            rt[i]=rt[k];
        }
        cout<<t[rt[i]].sum<<endl;
    }
}

 

标签:rt,cout,int,shelf,Persistent,Codeforces,Bookcase,bitset,op
From: https://www.cnblogs.com/liyishui2003/p/17241482.html

相关文章

  • Persistent entity ‘Users‘ should have primary key
    #情景:在使用springjpa,想要根据实体类自动生成代码的时候,实体类名称报错#检查鼠标移入实体类名称,实体类的类名下面是不是有一条下划线显示内容如下所示:Persistententity......
  • Codeforces Round 857 (Div. 2) C-The Very Beautiful Blanket
    题目地址题意:构造一个二维数组,使得任意一个4*4的子矩阵满足:A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42Solution(思路来源:知乎xioac......
  • Codeforces Round 859 (Div. 4)
    A.PlusorMinus#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);i......
  • CF 1368B Codeforces Subsequences
    题目地址题意:给你一个数n,构造一个字符串,使得至少有n个子串为codeforcesSolution用贪心的思想肯定是只在codeforces基础上修改对于每个字符,对答案的贡献都是乘以字符的......
  • Codeforces Round 858 (Div. 2)
    Preface这场CF打的好难受啊,A刚开始忘记切换语言了CE了两发,B漏了一种情况挂了一发,C纯靠暴力找性质,D比赛时没人写我题目都没仔细看然后E本来秒出了解法,结果就是因为unorder......
  • Educational Codeforces Round 115 (Rated for Div. 2)(D,E)
    EducationalCodeforcesRound115(RatedforDiv.2)(D,E)DD题目给出\(n\)个问题,每个问题都会有一个主题和一个难度,并且没有两个题目的问题和主题都是一样的,我们需要选......
  • Codeforces 87D Beautiful Road
    Codeforces87DBeautifulRoadCF传送门Description​ 给定一个无向图,\(n\)个点,\(n-1\)条边,保证图联通(就是一棵树),并且给定每条边的权值。任取两个点,连接二者的路径上......
  • Codeforces Round 350 (Div. 2) ABCD1D2
    https://codeforces.com/contest/670A.Holidays题目大意:给定n天,求出可以休息的最大小时间和最大时间。input14output44#include<bits/stdc++.h>usingnamesp......
  • Codeforces Round 851 (Div. 2) (CF1788) 题解
    CF1788AOneandTwo对于一个序列,题目要求蓝色部分的乘积等于绿色部分的乘积,因为序列中只有\(1\)和\(2\),所以我们只要蓝色部分和绿色部分的\(2\)的数量相等即可,使用......
  • Codeforces Round 406 (Div. 1) C. Till I Collapse 主席树上二分
    首先贪心是显然的,但是询问有n个先考虑一个朴素的主席树做法对于每个k,对于当前固定的L,二分R,用主席树查询一下[L,R]区间内的不同数个数是否大于K个(主席树的经典应用),更新......