• ZJNU 2770 MRAVOJED(前缀和+二分+位运算)



    Description

    pic

    在一张 \(n\times m\) 的灰白图中,要求计算出前 \(k\) 步总共走过了多少灰色格子。

    走法见上图右。

    定义左上坐标为 \((0,0)\),右下坐标为 \((n-1,m-1)\)

    对于坐标 \((x,y)\) 的格子,如果 \(x\& y=0\),则涂灰色。


    Solution

    考虑按行枚举或按列枚举,下面将按列进行枚举(自左向右)。

    对于枚举的某列 \(y\),根据题意中的走法,可知存在一个最大的 \(x\),使得对于所有 \(x'\in [0,x]\),坐标 \((x',y)\) 一定曾被走过,且对于所有 \(x''\in[x+1,n]\),坐标 \((x',y)\) 一定未被走过。

    因此可以考虑前缀和 + 二分来求出这个最大的 \(x\)

    最后对于每一列 \(y\),问题就只剩下求 \(0\sim x\) 中有多少个数 \(x'\) 满足 \(x'\& y=0\)了,位处理即可。

    总时间复杂度 \(O(n\log n)\)

    前缀和

    \(sum[i]\) 表示存在多少个格子 \((x,y)\) 满足 \(x+y\le i\),根据边界尺寸分三部分转移处理这个前缀和数组。根据走法,\(x+y\) 较小的格子一定比 \(x+y\) 较大的格子先被走过。

    二分

    在二分时,对于指定的 \(y\),二分 \(x_{mid}\),需要求出 \((x_{mid},y)\) 这个格子是在第几步走到的,再跟 \(k\) 进行比较。

    通过 \(sum[x_{mid}+y-1]\) 直接得出前面的步数,而对于和为 \(x_{mid}+y\) 的格子数量,先根据奇偶性判断是右上往左下走还是相反,再处理边界的影响即可。

    位运算

    给定 \(x,y\),求 \(0\sim x\) 中有多少个数 \(x'\) 满足 \(x'\& y=0\)

    考虑将 \(y\) 分为多段长度为 \(2\) 的幂次段进行处理。例如 \((20)_{10}=(10110)_2\),将其看成 \(00000\sim 01111, 10000\sim 10011,10100\sim 10110\) 三段,长度分别为 \(16,4,2\)

    从高位向低位枚举幂次 \(2^i\),如果 \(x\)\(2^i\) 位为 \(1\)\(y\)\(2^i\) 位为 \(0\),则在后 \(i-1\) 位中,除了 \(y\) 内为 \(1\) 的对应位置需要为 \(0\) 外,其余位置任选,答案加上 \(2^{i-cnt_{y1}}\)

    特殊的,如果 \(x\)\(2^i\) 位为 \(1\),同时 \(y\)\(2^i\) 位也为 \(1\),先假设 \(2^i\)\(0\),如上述方法计入答案后,在其后的分段中该位置恒为 \(1\),因此与运算必不为 \(0\),此时直接退出循环即可。


    // Template Ver.220227
    //#include<bits/stdc++.h>
    #include<algorithm>
    #include<cctype>
    #include<chrono>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<functional>
    #include<iomanip>
    #include<iostream>
    #include<map>
    #include<queue>
    #include<random>
    #include<set>
    #include<sstream>
    #include<stack>
    #include<string>
    #include<utility>
    #include<vector>
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(auto i=(a);i<=(b);i++)
    #define repp(i,a,b) for(auto i=(a);i<(b);i++)
    #define per(i,a,b) for(auto i=(a);i>=(b);i--)
    #define perr(i,a,b) for(auto i=(a);i>(b);i--)
    #define REP(i,a,b,s) for(auto i=(a);i<=(b);i+=(s))
    #define REPP(i,a,b,s) for(auto i=(a);i<(b);i+=(s))
    #define PER(i,a,b,s) for(auto i=(a);i>=(b);i-=(s))
    #define PERR(i,a,b,s) for(auto i=(a);i>(b);i-=(s))
    #define all(a) (a).begin(),(a).end()
    #define mst(a,b) memset(a,b,sizeof(a))
    #define pb push_back
    #define eb emplace_back
    #define YES cout<<"Yes\n"
    #define NO cout<<"No\n"
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    namespace BasicTemplate{
        const int INF=0x3f3f3f3f;
        const ll LINF=0x3f3f3f3f3f3f3f3f;
        const int dx[8]={0,1,0,-1,1,1,-1,-1},dy[8]={1,0,-1,0,1,-1,1,-1};
        const ll mod=998244353;
        mt19937 mtrandom(std::chrono::system_clock::now().time_since_epoch().count());
        ll getRandom(ll l,ll r){return uniform_int_distribution<ll>(l,r)(mtrandom);}
        void debug(){cerr<<'\n';}template<typename T,typename... Args>void debug(T x,Args... args){cerr<<"[ "<<x<< " ] , ";debug(args...);}
        ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
        ll qmul(ll a,ll b){ll r=0;while(b){if(b&1)r=(r+a)%mod;b>>=1;a=(a+a)%mod;}return r;}
        ll qpow(ll a,ll n){ll r=1;while(n){if(n&1)r=(r*a)%mod;n>>=1;a=(a*a)%mod;}return r;}
        ll qpow(ll a,ll n,ll p){ll r=1;while(n){if(n&1)r=(r*a)%p;n>>=1;a=(a*a)%p;}return r;}
    }
    namespace FastIO{
        const int bsz=1<<18;char bf[bsz],*head,*tail;
        inline char gc(){if(head==tail)tail=(head=bf)+fread(bf,1,bsz,stdin);if(head==tail)return 0;return *head++;}
        inline int read(){int x=0,f=1;char c=gc();for(;!isdigit(c);c=gc())if(c=='-')f=-1;for(;isdigit(c);c=gc())x=x*10+c-'0';return x*f;}
        inline ll read64(){ll x=0,f=1;char c=gc();for(;!isdigit(c);c=gc())if(c=='-')f=-1;for(;isdigit(c);c=gc())x=x*10+c-'0';return x*f;}
        inline void print(int x){if(x>9)print(x/10);putchar(x%10+'0');}
        inline void println(int x){print(x);putchar('\n');}
        inline void print(ll x){if(x>9)print(x/10);putchar(x%10+'0');}
        inline void println(ll x){print(x);putchar('\n');}
    }
    using namespace BasicTemplate;
    //using namespace FastIO;
    
    int n,m;
    ll sum[2000050];
    ll k;
    
    int getmax(int y)
    {
        int l=0,r=n-1;
        while(l<=r)
        {
            int mid=l+r>>1;
            ll d=sum[y+mid-1];
            if((y+mid)&1)
                d+=min(mid,m-1-y)+1;
            else
                d+=min(y,n-1-mid)+1;
            if(d<=k)
                l=mid+1;
            else
                r=mid-1;
        }
        return r;
    }
    
    int getans(int x,int y) // count 0~x & y = 0
    {
        //cout<<x<<' '<<y<<' ';
        x++;
        int ans=0,cnt=__builtin_popcount(y);
        per(i,20,0)
        {
            if(y>>i&1)
                cnt--;
            if(x>>i&1)
            {
                ans+=1<<(i-cnt);
                if(y>>i&1)
                    break;
            }
        }
        return ans;
    }
    
    void solve()
    {
        cin>>n>>m>>k;
        if(k==1)
        {
            cout<<1<<'\n';
            return;
        }
        
        sum[0]=1;
        int mn=min(n,m),mx=max(n,m);
        repp(i,1,mn)
            sum[i]=sum[i-1]+i+1;
        repp(i,mn,mx)
            sum[i]=sum[i-1]+mn;
        rep(i,mx,n+m-2)
            sum[i]=sum[i-1]+(n+m-1-i);
        
        ll ans=0;
        repp(y,0,m)
        {
            int x=getmax(y);
            if(x==-1)
                break;
            ans+=getans(x,y);
        }
        cout<<ans<<'\n';
    }
    signed main()
    {
        closeSync;
        //multiCase
        {
            solve();
        }
        return 0;
    }
    

  • 相关阅读:
    操作系统开发系列—13.g.操作系统的系统调用 ●
    操作系统开发系列—13.f.Minix的中断处理(暂时忽略)
    操作系统开发系列—13.e.三进程
    操作系统开发系列—13.d.多进程 ●
    操作系统开发系列—解释typedef void (*int_handler) ();
    操作系统开发系列—13.c.进程之中断重入
    操作系统开发系列—13.b.进程之丰富中断处理程序
    操作系统开发系列—13.a.进程 ●
    操作系统开发系列—12.g.在内核中设置键盘中断
    操作系统开发系列—12.f.在内核中添加中断处理 ●
  • 原文地址:https://www.cnblogs.com/stelayuri/p/15972449.html
Copyright © 2020-2023  润新知