結果

問題 No.1296 OR or NOR
ユーザー chocoruskchocorusk
提出日時 2020-11-22 03:02:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,106 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 139,712 KB
実行使用メモリ 34,732 KB
最終ジャッジ日時 2023-09-30 22:23:52
合計ジャッジ時間 22,045 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 567 ms
34,240 KB
testcase_08 AC 554 ms
34,332 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 560 ms
34,240 KB
testcase_17 AC 554 ms
34,316 KB
testcase_18 AC 560 ms
34,196 KB
testcase_19 AC 559 ms
34,196 KB
testcase_20 AC 393 ms
4,376 KB
testcase_21 AC 399 ms
4,376 KB
testcase_22 AC 398 ms
4,376 KB
testcase_23 AC 395 ms
4,376 KB
testcase_24 AC 389 ms
4,380 KB
testcase_25 AC 395 ms
4,384 KB
testcase_26 AC 400 ms
4,380 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 397 ms
4,380 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 551 ms
34,444 KB
testcase_34 AC 550 ms
34,264 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++){
        cin>>a[i];
    }
    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