結果

問題 No.1296 OR or NOR
ユーザー chocoruskchocorusk
提出日時 2020-11-22 03:18:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,188 bytes
コンパイル時間 1,920 ms
コンパイル使用メモリ 138,624 KB
実行使用メモリ 39,124 KB
最終ジャッジ日時 2023-09-30 22:24:35
合計ジャッジ時間 15,606 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 514 ms
34,204 KB
testcase_03 AC 513 ms
34,148 KB
testcase_04 AC 607 ms
34,144 KB
testcase_05 AC 615 ms
34,328 KB
testcase_06 AC 556 ms
34,264 KB
testcase_07 AC 553 ms
34,336 KB
testcase_08 AC 555 ms
34,280 KB
testcase_09 AC 601 ms
34,180 KB
testcase_10 AC 666 ms
34,196 KB
testcase_11 AC 578 ms
34,336 KB
testcase_12 AC 629 ms
34,212 KB
testcase_13 AC 588 ms
34,200 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<typename T>
struct SparseTable{
    using F=function<T(T, T)>;
    const F f;
    const T e;
    vector<vector<T>> spt;
    vector<int> vlog;
    SparseTable(const F f, const T &e, const vector<T> &v):f(f), e(e){
        int b=0, n=v.size();
        while((1<<b)<=n) b++;
        spt.resize(b, vector<T>(n));
        for(int i=0; i<n; i++) spt[0][i]=v[i];
        for(int i=1; i<b; i++){
            for(int j=0; j+(1<<i)<=n; j++){
                spt[i][j]=f(spt[i-1][j], spt[i-1][j+(1<<(i-1))]);
            }
        }
        vlog.resize(n+1);
        for(int i=2; i<=n; i++) vlog[i]=vlog[i>>1]+1;
    }
    T query(int l, int r){
        int b=vlog[r-l];
        return f(spt[b][l], spt[b][r-(1<<b)]);
    }
};
int main()
{
    int n;
    cin>>n;
    vector<ll> a(n);
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    //000111***
    //000000*** -> 111000*** ?
    //000110*** -> 000**1***
    SparseTable<ll> seg([](ll a, ll b){ return (a|b);}, 0, a);
    int q;
    cin>>q;
    while(q--){
        ll b;
        cin>>b;
        int k=n-1;
        ll x=(1ll<<60)-1;
        int ans=0;
        while(k>=0){
            if((x&a[k])>0){
                if(((b|a[k])&x)==b){
                    b^=(x&a[k]);
                    x^=(x&a[k]);
                    k--;
                }else if(((~b|a[k])&x)==(~b&x)){
                    ans++;
                    b=(~b&x);
                    b^=(x&a[k]);
                    x^=(x&a[k]);
                    k--;
                }else{
                    ans=-1;
                    break;
                }
            }else{
                int l=-1, r=k;
                while(r-l>1){
                    int m=(l+r)/2;
                    if((x&seg.query(m, k+1))==0) r=m;
                    else l=m;
                }
                if(l==-1){
                    if(b==0) break;
                    else if(x==b){
                        ans++;
                        b=0;
                        break;
                    }else{
                        ans=-1;
                        break;
                    }
                }
                k=l;
                if(((b|a[k])&x)==b){
                    b^=(x&a[k]);
                    x^=(x&a[k]);
                    k--;
                }else if(((~b|a[k])&x)==(~b&x)){
                    ans++;
                    b=(~b&x);
                    b^=(x&a[k]);
                    x^=(x&a[k]);
                    k--;
                }else{
                    ans=-1;
                    break;
                }
            }
        }
        if(b!=0) ans=-1;
        cout<<ans<<endl;
    }
	return 0;
}
0