結果

問題 No.1296 OR or NOR
ユーザー chocoruskchocorusk
提出日時 2020-11-22 03:33:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,232 ms / 3,000 ms
コード長 3,268 bytes
コンパイル時間 1,150 ms
コンパイル使用メモリ 129,080 KB
実行使用メモリ 34,544 KB
最終ジャッジ日時 2023-09-30 22:24:54
合計ジャッジ時間 10,493 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
5,600 KB
testcase_02 AC 98 ms
33,232 KB
testcase_03 AC 96 ms
33,484 KB
testcase_04 AC 160 ms
33,460 KB
testcase_05 AC 165 ms
32,884 KB
testcase_06 AC 111 ms
33,256 KB
testcase_07 AC 108 ms
33,164 KB
testcase_08 AC 109 ms
33,256 KB
testcase_09 AC 123 ms
33,684 KB
testcase_10 AC 139 ms
32,888 KB
testcase_11 AC 118 ms
34,152 KB
testcase_12 AC 134 ms
33,628 KB
testcase_13 AC 117 ms
33,024 KB
testcase_14 AC 2,232 ms
33,620 KB
testcase_15 AC 139 ms
34,136 KB
testcase_16 AC 120 ms
34,272 KB
testcase_17 AC 108 ms
33,224 KB
testcase_18 AC 110 ms
33,160 KB
testcase_19 AC 108 ms
32,904 KB
testcase_20 AC 61 ms
7,784 KB
testcase_21 AC 62 ms
7,588 KB
testcase_22 AC 57 ms
4,376 KB
testcase_23 AC 58 ms
4,380 KB
testcase_24 AC 61 ms
5,588 KB
testcase_25 AC 60 ms
5,520 KB
testcase_26 AC 61 ms
7,660 KB
testcase_27 AC 61 ms
7,636 KB
testcase_28 AC 62 ms
9,632 KB
testcase_29 AC 62 ms
9,632 KB
testcase_30 AC 181 ms
33,416 KB
testcase_31 AC 179 ms
32,896 KB
testcase_32 AC 179 ms
33,852 KB
testcase_33 AC 114 ms
33,012 KB
testcase_34 AC 113 ms
34,544 KB
権限があれば一括ダウンロードができます

ソースコード

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++){
        scanf("%lld", &a[i]);
    }
    ll as[20][200020];
    for(int i=0; i<n; i++) as[0][i]=a[i];
    for(int i=1; (1<<i)<=n; i++){
        for(int j=0; j<n; j++){
            if(j+1-(1<<i)>=0){
                as[i][j]=(as[i-1][j]|as[i-1][j-(1<<(i-1))]);
            }
        }
    }
    int q;
    cin>>q;
    while(q--){
        ll b;
        scanf("%lld", &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{
                for(int t=17; t>=0; t--){
                    if(k+1-(1<<t)<0) continue;
                    if((x&as[t][k])==0) k-=(1<<t);
                }
                if(k==-1){
                    if(b==0) break;
                    else if(x==b){
                        ans++;
                        b=0;
                        break;
                    }else{
                        ans=-1;
                        break;
                    }
                }
                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;
        printf("%d\n", ans);
    }
	return 0;
}

0