1 solutions

  • 0
    @ 2025-3-9 21:48:45

    本题由于数据不强,可以通过暴力通过。但是本质是想考察线段树,ac码如下

    #include<bits/stdc++.h>
    
    using namespace std;
    
    #define int long long 
    
    struct N{
    	int l,r,_xor;
    	int w[30];
    }t[1000010];
    
    int a[5000005],c[5000005];
    int n,m;
    int ans=0;
    
    int check[30],sum[30];
    
    void b(int x,int l,int r)
    {
    	t[x].l=l;
    	t[x].r=r;
    	t[x]._xor=0;
    	if(l==r)
    	{
    		int p=a[l];
    		for(int i=22;i>=0;i--)
    		{
    			if(p>=c[i])t[x].w[i]=1,p-=c[i];
    			else t[x].w[i]=0;
    		}
    		return ;
    	}
    	int mid=(l+r)/2;
    	b(x*2,l,mid);
    	b(x*2+1,mid+1,r);
    	
    	for(int i=0;i<=22;i++)
    	{
    		t[x].w[i]=t[x*2].w[i]+t[x*2+1].w[i];
    	}
    	
    }
    
    void push_down(int x)
    {
    	t[x*2]._xor^=t[x]._xor;
    	t[x*2+1]._xor^=t[x]._xor;
    	for(int i=0;i<=22;i++)check[i]=0;
    	int p=t[x]._xor;
    		for(int i=22;i>=0;i--)
    		{
    			if(p>=c[i])check[i]=1,p-=c[i];
    		}
    		for(int i=0;i<=22;i++)
    		{
    			if(check[i]==1)t[x].w[i]=t[x].r-t[x].l+1-t[x].w[i];
    		}
    		//return ;
    	t[x]._xor=0;
    }
    
    void Xor(int x,int l,int r,int k)
    {
    	
    	if(t[x].l>=l&&t[x].r<=r)
    	{
    		t[x]._xor=t[x]._xor^k;
    		push_down(x);
    		return ;
    	}
    	push_down(x);
    	push_down(x*2);
    	push_down(x*2+1);
    	if(t[x*2].r>=l)Xor(x*2,l,r,k);
    	if(t[x*2+1].l<=r)Xor(x*2+1,l,r,k);
    	
    	for(int i=0;i<=22;i++)
    	{
    		t[x].w[i]=t[x*2].w[i]+t[x*2+1].w[i];
    	}
    }
    
    void s(int x,int l,int r)
    {
    	push_down(x);
    	if(t[x].l>=l&&t[x].r<=r)
    	{
    		for(int i=0;i<=22;i++)
    		{
    			sum[i]+=t[x].w[i];
    		}
    		return ;
    	}
    	//push_down(x);
    	if(t[x*2].r>=l)s(x*2,l,r);
    	if(t[x*2+1].l<=r)s(x*2+1,l,r);	
    }
    
    signed  main()
    {
    	ios::sync_with_stdio(0);
    	cin.tie(0);cout.tie(0);
        cin >> n;
        for(int i=1;i<=n;i++)cin >> a[i];   
        cin >> m;
        for(int i=0;i<30;i++)
        {
        	if(i==0)c[i]=1;
        	else c[i]=c[i-1]*2;
    	}
        b(1,1,n);
        while(m--)
        {	 
        	int t;
        	cin >> t;
        	if(t==1)
        	{
        		int x,y;
        		cin >> x >> y;
        		for(int i=0;i<=22;i++)
    			{
    				sum[i]=0;
    			}
        		int ans=0;
        		s(1,x,y);
        		for(int i=0;i<=22;i++)
    			{
    				ans+=sum[i]*c[i];
    			}
    			cout << ans << "\n";
    		}
    		else
    		{
    			int x,y,z;
    			cin >> x >> y >> z;
    			Xor(1,x,y,z);
    			
    		}
    		
    	}
    	
    	return 0;
    }
    
    
    • 1

    Information

    ID
    7010
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    # Submissions
    64
    Accepted
    6
    Uploaded By