Codeforces Round # 626 D. Present XOR determines the double pointer or + binary bit

Codeforces Round # 626 D. Present XOR determines the double pointer or + binary bit

The meaning of problems

A number n, and ask them two by two exclusive OR result n (4e5) range (1E7)

Thinking

XOR problem are generally determined by bit, then how to determine the value of the k bits of it. First, the value of the k-bit only and [1, k] bit of a relationship, that is only in this relationship would have a number and carry some with a [i]. So how to deal with the problem and do. First, we consider only [1..k] bit, and then they are recorded as sum, if the sum of the k-bit value is then the sum of \ (2 ^ k + x \ ) because only consider [1..k ] Therefore, the maximum value of a bit is \ (2 ^ {k + 1 } -1 \) a maximum value of sum is \ (2 ^ {k + 2 } -2 \) so we can get as \ ([^ 2 k, 2 ^ {k + 1 } -1] \) , and [2 ^ {k + 1} + 2 ^ {k} -1,2 ^ {k + 2} -2] If the sum falls two sections on, then its k-th bit is 1. We want to count and is located within the two ranges, it is clear that we can first of a few array mod \ (2 ^ {k + 1 } \) sorted using half looking range. Such complexity is 0 (nlognlogc) same as monotone, may use the double pointer thus less of a log becomes O (nlogc)

Bipartite

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define mkp make_pair
#define pii pair<int,int>
typedef long long ll;
#define int ll
const int inf=0x3f3f3f3f;
const int maxn=4e5+5;
const int mod= 998244353;
int b[maxn],a[maxn];
int n;
int solve(int x,int y,int z){
 
        if(y<x)return 0;
        int l=lower_bound(b+1,b+1+n,x)-b;
        int r=upper_bound(b+1,b+1+n,y)-b;
            //cout<<x<<" fuck"<<y<<" "<<(n-l+1)-(n-r+1)<<endl;
        int flag=0;
        if(z>=x&&z<=y)flag=1;
        return (n-l+1)-(n-r+1)-flag;
}
int32_t main(){
    scanf("%lld",&n);
    int up=0;
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]),up=max(up,a[i]);
    int ans=0;
    
    for(int k=1;k<=2e7;k<<=1){
        for(int j=1;j<=n;j++)b[j]=a[j]%(k*2ll);
        sort(b+1,b+1+n);
        int sum=0;
        for(int j=1;j<=n;j++){
        //  cout<<solve((1<<(k-1))-b[j],(1<<k)-b[j]-1)<<" "<<solve((1<<(k-1))+(1<<(k))-b[j],(1<<(k+1))-2-b[j]);
            //cout<<endl;
        //  cout<<j<<" "<<((1<<(k-1))-b[j])<<" "<<((1<<k)-b[j]-1)<<" "<<solve((1<<(k-1))-b[j],(1<<k)-b[j]-1)<<endl;
            //cout<<j<<" "<<((1<<(k-1))+(1<<(k))-b[j])<<" "<<((1<<(k+1))-2-b[j])<<" "<<solve((1<<(k-1))+(1<<(k))-b[j],(1<<(k+1))-2-b[j])<<endl;
            sum+=solve(k-b[j],2ll*k-b[j]-1,b[j]);
            sum+=solve(2ll*k+k-b[j],k*4ll-2-b[j],b[j]);
        }
        sum/=2;
    //  cout<<sum<<endl;
        if(sum&1)ans^=(k);
    }
    printf("%lld\n",ans);
    return 0;
}

Double pointer

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define mkp make_pair
#define pii pair<int,int>
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=4e5+5;
const int mod= 998244353;
int b[maxn],a[maxn];
int n;
 
int main(){
    scanf("%d",&n);
    int up=0;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),up=max(up,a[i]);
    int ans=0;
    for(int k=1;k<=2e7;k<<=1){
        for(int j=1;j<=n;j++)b[j]=a[j]%(k*2ll);
        sort(b+1,b+1+n);
        long long  sum=0;
        int p1=1,p2=1,p3=1;
        for(int j=n;j>=1;j--){
            while(p1<=n&&b[p1]+b[j]<k)p1++;
            while(p2<=n&&b[p2]+b[j]<k+k)p2++;
            while(p3<=n&&b[p3]+b[j]<k+k+k)p3++;
            sum+=max(0,min(j,p2)-p1);
            sum+=max(0,j-p3);
        }
        if(sum&1)ans^=(k);
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/ttttttttrx/p/12444098.html