結果

問題 No.1896 Arrays and XOR Procedure 2
ユーザー Shirotsume
提出日時 2022-03-28 13:40:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 1,937 ms
コンパイル使用メモリ 201,060 KB
最終ジャッジ日時 2025-01-28 13:11:42
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:50:23: warning: ‘now’ may be used uninitialized [-Wmaybe-uninitialized]
   50 |         if (1 & (a[now] >> i)) k = i;
      |                       ^
main.cpp:33:9: note: ‘now’ was declared here
   33 |     int now;
      |         ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
void query(int t, int p, int q, vector<int>& a, vector<int>& b, vector<vector<int>>& ans){
    vector<int> temp;
    temp.push_back(t);
    temp.push_back(p + 1);
    temp.push_back(q + 1);
    if(t == 1) a[p] = a[p] ^ b[q];
    if(t == 2) b[q] = a[p] ^ b[q];
    ans.push_back(temp);
}

void swap(int i, int j, vector<int>& a, vector<int>& b, vector<vector<int>>& ans){
    //a[i] と b[j] を交換
    query(1, i, j, a, b, ans);
    query(2, i, j, a, b, ans);
    query(1, i, j, a, b, ans);
}
int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    vector<vector<int>> ans;
    int maxa = 0, maxb = 0;
    rep(i, n){
        if(a[i] > maxa) maxa = a[i];
        if(b[i] > maxb) maxb = b[i];
    }
    int now;
    if (maxa > maxb){
        rep(i, n) if (a[i] == maxa){
         now = i;
         break;
        }
    }

    else{
        rep(i, n) if (b[i] == maxb){
            swap(0, i, a, b, ans);
            now = 0;
            break;
        }
    }
    int k = 0;
    rep(i, 30){
        if (1 & (a[now] >> i)) k = i;
    }
    rep(i,n){
        if (b[i] < 1 << k){
            query(2, now, i, a, b, ans);
        }
    }
    rep(i, n){
        if (1 & (a[i] >> k)){
            query(1, i, 0, a, b, ans);
        }
    }
    int ans1 = ans.size();
    cout << ans1 << endl;
    rep(i, ans1){
        printf("%d %d %d\n" ,ans[i][0], ans[i][1], ans[i][2]);
    }
    
    return 0;
}
0