QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#118984#6631. Maximum Bitwise OR8BQube#TL 3ms11000kbC++202.5kb2023-07-04 17:40:002023-07-04 17:40:01

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-04 17:40:01]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:11000kb
  • [2023-07-04 17:40:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

const int L = 30;
int arr[100005], cnt[100005][L], qcnt[100005][L];
pii ans[100005], ud[100005];
vector<pii> query[100005];
deque<int> dq[L][L];

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; ++i)
        cin >> arr[i];
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < L; ++j) {
            cnt[i][j] = cnt[i - 1][j];
            if (arr[i] >> j & 1) ++cnt[i][j];
        }
    }
    for (int i = 1; i <= q; ++i) {
        int l, r;
        cin >> l >> r;
        ud[i] = pii(-1, -1);
        for (int j = 0; j < L; ++j)
            qcnt[i][j] = cnt[r][j] - cnt[l - 1][j];
        for (int j = L - 1; j >= 0; --j)
            if (qcnt[i][j] > 0) {
                ans[i].X = (1 << (j + 1)) - 1;
                break;
            }
        if (ans[i].X == 0) continue;
        for (int j = __lg(ans[i].X); j >= 0; --j) {
            if (qcnt[i][j] == 0) {
                if (!~ud[i].X) ud[i].X = j;
                ud[i].Y = j;
            }
        }
        if (~ud[i].X) {
            ans[i].Y = 2;
            query[r].pb(pii(l, i));
        }
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < L; ++j)
            if (arr[i] > (1 << j))
                for (int k = j; k >= 0; --k) {
                    if ((arr[i] >> k & 1) == 0) {
                        dq[k][j].pb(i);
                        if (SZ(dq[k][j]) > L + 5) dq[k][j].pop_front();
                    }
                    else break;
                }
        for (auto [l, idx] : query[i]) {
            for (int j : dq[ud[idx].Y][ud[idx].X])
                if (j >= l) {
                    for (int k = 0; k < L; ++k)
                        qcnt[idx][k] -= arr[j] >> k & 1;
                    int tmp = arr[j] ^ (arr[j] - (1 << ud[idx].Y));
                    for (int k = 0; k < L; ++k)
                        if (qcnt[idx][k])
                            tmp |= 1 << k;
                    if (tmp == ans[idx].X) ans[idx].Y = 1;
                    for (int k = 0; k < L; ++k)
                        qcnt[idx][k] += arr[j] >> k & 1;
                }
        }
    }

    for (int i = 1; i <= q; ++i)
        cout << ans[i].X << " " << ans[i].Y << "\n";
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 11000kb

input:

1
3 2
10 10 5
1 2
1 3

output:

15 2
15 0

result:

ok 4 number(s): "15 2 15 0"

Test #2:

score: -100
Time Limit Exceeded

input:

100000
1 1
924704060
1 1
1 1
149840457
1 1
1 1
515267304
1 1
1 1
635378394
1 1
1 1
416239424
1 1
1 1
960156404
1 1
1 1
431278082
1 1
1 1
629009153
1 1
1 1
140374311
1 1
1 1
245014761
1 1
1 1
445512399
1 1
1 1
43894730
1 1
1 1
129731646
1 1
1 1
711065534
1 1
1 1
322643984
1 1
1 1
482420443
1 1
1 1
20...

output:

1073741823 2
268435455 2
536870911 2
1073741823 2
536870911 2
1073741823 2
536870911 2
1073741823 2
268435455 2
268435455 2
536870911 2
67108863 2
134217727 2
1073741823 2
536870911 2
536870911 2
268435455 2
536870911 2
536870911 2
536870911 2
268435455 2
268435455 2
1073741823 2
16777215 2
10737418...

result: