QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#816479#9865. DollspropaneWA 5ms12496kbC++202.7kb2024-12-16 12:09:382024-12-16 12:09:40

Judging History

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

  • [2025-01-08 13:49:40]
  • hack成功,自动添加数据
  • (/hack/1434)
  • [2024-12-16 12:09:40]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:12496kb
  • [2024-12-16 12:09:38]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<array>
#include<set>
#include<random>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const int maxn = 1e5 + 5;
int a[maxn], p[maxn];
ULL hsh[maxn];

namespace BIT{

ULL tr[maxn];
int n;

void init(int _n){
    n = _n;
    for(int i = 1; i <= n; i++) tr[i] = 0;
}

int lowbit(int x){
    return x & -x;
}

void modify(int x, ULL v){
    while(x <= n){
        tr[x] ^= v;
        x += lowbit(x);
    }
}

ULL query(int x){
    ULL ans = 0;
    while(x){
        ans ^= tr[x];
        x -= lowbit(x);
    }
    return ans;
}

ULL query(int l, int r){
    return query(r) ^ query(l - 1);
}

}

int mn[20][maxn], mx[20][maxn];

void init(int n){
    for(int i = 1; i <= n; i++){
        mn[0][i] = mx[0][i] = a[i];
    }
    for(int i = 1; i <= 19; i++){
        for(int j = 1; j + (1 << i) - 1 <= n; j++){
            mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);
            mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << (i - 1))]);
        }
    }
}

pair<int, int> query(int l, int r){
    int k = __lg(r - l + 1);
    int m1 = min(mn[k][l], mn[k][r - (1 << k) + 1]);
    int m2 = max(mx[k][l], mx[k][r - (1 << k) + 1]);
    return {m1, m2};
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    mt19937_64 rnd(12412541231);
    for(int i = 1; i < maxn; i++){
        hsh[i] = rnd();
    }

    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++) 
            cin >> a[i], p[a[i]] = i;
        
        vector<ULL> b(n + 1);
        for(int i = 1; i <= n; i++){
            b[i] = b[i - 1] ^ hsh[a[i]];
        }
        BIT::init(n);
        init(n);
        int ans = 0;
        set<int> s;
        auto check = [&](int x){
            if (s.size() <= 2) return true;
            if (a[x] < *s.begin() or a[x] > *s.rbegin()) return true;
            auto it = s.lower_bound(a[x]);
            int nxt = *it;
            int pre = *prev(it);
            auto [l, r] = query(max(p[nxt], p[pre]), x - 1);
            return BIT::query(l, r) == (b[x - 1] ^ b[max(p[nxt], p[pre]) - 1]);
        };
        for(int i = 1; i <= n; i++){
            if (check(i)){
                s.insert(a[i]);
                BIT::modify(a[i], hsh[a[i]]);
            }
            else{
                ans += 1;
                for(auto x : s){
                    BIT::modify(x, hsh[a[i]]);
                }
                s.clear();
            }
        }
        cout << n - 1 - ans << '\n';
    }

}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 12496kb

input:

8
4
2 1 4 3
4
1 4 2 3
4
3 1 4 2
5
1 3 5 2 4
5
1 4 2 5 3
5
2 5 3 1 4
6
1 3 6 5 2 4
6
2 5 1 3 6 4

output:

3
3
2
3
3
3
4
4

result:

ok 8 numbers

Test #2:

score: -100
Wrong Answer
time: 5ms
memory: 7956kb

input:

5913
1
1
2
1 2
2
2 1
3
1 2 3
3
1 3 2
3
2 1 3
3
2 3 1
3
3 1 2
3
3 2 1
4
1 2 3 4
4
1 2 4 3
4
1 3 2 4
4
1 3 4 2
4
1 4 2 3
4
1 4 3 2
4
2 1 3 4
4
2 1 4 3
4
2 3 1 4
4
2 3 4 1
4
2 4 1 3
4
2 4 3 1
4
3 1 2 4
4
3 1 4 2
4
3 2 1 4
4
3 2 4 1
4
3 4 1 2
4
3 4 2 1
4
4 1 2 3
4
4 1 3 2
4
4 2 1 3
4
4 2 3 1
4
4 3 1 2
4...

output:

0
1
1
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
2
3
3
2
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
3
4
4
3
4
3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
3
4
3
3
4
4
3
4
3
3
3
4
3
4
4
4
3
3
3
3
4
4
4
4
3
4
4
3
4
4
4
4
3
3
3
3
4
4
4
3
4
3
3
3
4
3
4
4
3
3
4
3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
3
4
3
4
4
3
4
4
4
4
4
4
4
...

result:

wrong answer 49th numbers differ - expected: '4', found: '3'