QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#746738#9742. 优秀的拆分ucup-team004AC ✓84ms6832kbC++236.4kb2024-11-14 15:24:302024-11-14 15:24:31

Judging History

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

  • [2024-11-14 15:24:31]
  • 评测
  • 测评结果:AC
  • 用时:84ms
  • 内存:6832kb
  • [2024-11-14 15:24:30]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u128 = unsigned __int128;
template<class Info, class Tag>
struct LazySegmentTree {
    int n;
    std::vector<Info> info;
    std::vector<Tag> tag;
    LazySegmentTree() : n(0) {}
    LazySegmentTree(int n_, Info v_ = Info()) {
        init(n_, v_);
    }
    template<class T>
    LazySegmentTree(std::vector<T> init_) {
        init(init_);
    }
    void init(int n_, Info v_ = Info()) {
        init(std::vector(n_, v_));
    }
    template<class T>
    void init(std::vector<T> init_) {
        n = init_.size();
        info.assign(4 << std::__lg(n), Info());
        tag.assign(4 << std::__lg(n), Tag());
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init_[l];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }
    void pull(int p) {
        info[p] = info[2 * p] + info[2 * p + 1];
    }
    void apply(int p, const Tag &v) {
        info[p].apply(v);
        tag[p].apply(v);
    }
    void push(int p) {
        apply(2 * p, tag[p]);
        apply(2 * p + 1, tag[p]);
        tag[p] = Tag();
    }
    void modify(int p, int l, int r, int x, const Info &v) {
        if (r - l == 1) {
            info[p] = v;
            return;
        }
        int m = (l + r) / 2;
        push(p);
        if (x < m) {
            modify(2 * p, l, m, x, v);
        } else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }
    void modify(int p, const Info &v) {
        modify(1, 0, n, p, v);
    }
    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        push(p);
        return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
    }
    Info rangeQuery(int l, int r) {
        return rangeQuery(1, 0, n, l, r);
    }
    void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
        if (l >= y || r <= x) {
            return;
        }
        if (l >= x && r <= y) {
            apply(p, v);
            return;
        }
        int m = (l + r) / 2;
        push(p);
        rangeApply(2 * p, l, m, x, y, v);
        rangeApply(2 * p + 1, m, r, x, y, v);
        pull(p);
    }
    void rangeApply(int l, int r, const Tag &v) {
        return rangeApply(1, 0, n, l, r, v);
    }
    template<class F>
    int findFirst(int p, int l, int r, int x, int y, F &&pred) {
        if (l >= y || r <= x) {
            return -1;
        }
        if (l >= x && r <= y && !pred(info[p])) {
            return -1;
        }
        if (r - l == 1) {
            return l;
        }
        int m = (l + r) / 2;
        push(p);
        int res = findFirst(2 * p, l, m, x, y, pred);
        if (res == -1) {
            res = findFirst(2 * p + 1, m, r, x, y, pred);
        }
        return res;
    }
    template<class F>
    int findFirst(int l, int r, F &&pred) {
        return findFirst(1, 0, n, l, r, pred);
    }
    template<class F>
    int findLast(int p, int l, int r, int x, int y, F &&pred) {
        if (l >= y || r <= x) {
            return -1;
        }
        if (l >= x && r <= y && !pred(info[p])) {
            return -1;
        }
        if (r - l == 1) {
            return l;
        }
        int m = (l + r) / 2;
        push(p);
        int res = findLast(2 * p + 1, m, r, x, y, pred);
        if (res == -1) {
            res = findLast(2 * p, l, m, x, y, pred);
        }
        return res;
    }
    template<class F>
    int findLast(int l, int r, F &&pred) {
        return findLast(1, 0, n, l, r, pred);
    }
};

struct Tag {
    int add = 0;
    void apply(const Tag &t) & {
        add += t.add;
    }
};

struct Info {
    int max = 0;
    void apply(const Tag &t) & {
        max += t.add;
    }
};

Info operator+(const Info &a, const Info &b) {
    return {std::max(a.max, b.max)};
}

void solve() {
    int n;
    std::cin >> n;
    
    std::vector<int> p(n);
    for (int i = 0; i < n; i++) {
        std::cin >> p[i];
        p[i]--;
    }
    
    std::vector<int> fpi, fpd, fsi, fsd;
    std::vector<int> vpi(n), vsi(n), vpd(n), vsd(n);
    for (int i = 0; i < n; i++) {
        auto iti = std::lower_bound(fpi.begin(), fpi.end(), p[i]);
        auto itd = std::lower_bound(fpd.begin(), fpd.end(), -p[i]);
        if (iti == fpi.end()) {
            vpi[i] = n;
            fpi.push_back(p[i]);
        } else {
            vpi[i] = *iti;
            *iti = p[i];
        }
        if (itd == fpd.end()) {
            vpd[i] = 1;
            fpd.push_back(-p[i]);
        } else {
            vpd[i] = *itd;
            *itd = -p[i];
        }
    }
    for (int i = n - 1; i >= 0; i--) {
        auto iti = std::lower_bound(fsi.begin(), fsi.end(), -p[i]);
        auto itd = std::lower_bound(fsd.begin(), fsd.end(), p[i]);
        if (iti == fsi.end()) {
            vsi[i] = 1;
            fsi.push_back(-p[i]);
        } else {
            vsi[i] = *iti;
            *iti = -p[i];
        }
        if (itd == fsd.end()) {
            vsd[i] = n;
            fsd.push_back(p[i]);
        } else {
            vsd[i] = *itd;
            *itd = p[i];
        }
    }
    
    int ans = 0;
    
    LazySegmentTree<Info, Tag> seg(n + 1);
    for (int i = 0; i < fsi.size(); i++) {
        seg.rangeApply(0, -fsi[i] + 1, {1});
    }
    for (int i = 0; i < fsd.size(); i++) {
        seg.rangeApply(fsd[i] + 1, n + 1, {1});
    }
    
    for (int i = 0; i < n; i++) {
        seg.rangeApply(p[i] + 1, vpi[i] + 1, {1});
        seg.rangeApply(-vpd[i] + 1, p[i] + 1, {1});
        seg.rangeApply(p[i] + 1, vsd[i] + 1, {-1});
        seg.rangeApply(-vsi[i] + 1, p[i] + 1, {-1});
        ans = std::max(ans, seg.rangeQuery(0, n + 1).max);
    }
    
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3628kb

input:

3
5
3 5 1 4 2
4
1 2 4 3
5
3 5 2 1 4

output:

4
4
5

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 31ms
memory: 3520kb

input:

20000
2
2 1
10
6 10 2 7 9 3 8 4 1 5
9
3 6 4 5 8 9 2 7 1
7
3 7 6 4 1 5 2
7
7 2 3 6 5 1 4
5
4 1 3 2 5
9
6 7 5 3 8 1 9 2 4
3
1 2 3
8
7 2 4 6 1 8 3 5
7
4 2 6 3 1 7 5
8
1 7 3 4 2 5 6 8
2
1 2
10
10 2 3 8 6 9 1 4 7 5
4
3 2 1 4
9
7 5 3 4 1 2 9 6 8
7
2 4 5 1 6 7 3
10
3 1 10 4 9 5 6 8 2 7
5
1 2 5 3 4
6
2 6 5 ...

output:

2
8
8
6
7
5
7
3
6
6
8
2
8
4
7
6
9
5
6
7
7
8
9
7
8
1
5
6
5
7
3
3
4
3
6
7
6
1
6
5
1
8
6
8
6
6
2
3
2
6
8
5
6
5
7
3
2
7
7
6
3
1
3
1
8
7
8
4
1
1
8
7
7
2
7
2
1
9
2
5
6
3
5
6
8
6
2
2
1
6
6
7
8
3
1
8
6
10
2
8
8
4
6
3
8
8
2
4
1
3
5
8
9
1
7
7
2
6
7
4
8
1
2
5
3
1
8
6
7
1
9
7
5
7
3
8
1
5
5
6
4
5
4
1
6
2
7
6
1
7...

result:

ok 20000 lines

Test #3:

score: 0
Accepted
time: 62ms
memory: 3712kb

input:

20
895
97 29 511 535 178 149 710 846 37 191 684 504 309 528 524 189 659 42 337 688 213 343 859 433 606 445 693 608 232 585 577 313 759 746 612 341 480 875 610 222 28 637 11 91 796 261 296 333 377 871 275 552 788 371 763 862 522 322 447 126 492 694 799 620 842 394 434 706 460 479 240 241 676 502 749 ...

output:

115
165
331
171
112
204
247
226
239
114
231
241
57
229
371
243
347
120
62
352

result:

ok 20 lines

Test #4:

score: 0
Accepted
time: 84ms
memory: 6832kb

input:

2
66375
22486 8985 25896 9585 22371 18677 32794 51856 4976 20566 19519 11668 36820 19785 27213 14206 5728 54919 55392 20146 5373 20907 66131 64447 53265 22521 1393 31296 58406 54362 2294 13520 13660 59044 13345 44636 52942 37423 64998 54440 50291 61802 16224 5240 59589 52028 52268 6841 12466 65025 5...

output:

1000
317

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 36ms
memory: 5044kb

input:

1
32819
25454 18203 11285 15122 31242 9557 17292 31209 2640 19069 28006 6007 31106 8326 3990 27947 7492 12774 32211 1020 4848 30327 16472 8098 19636 1940 15391 18106 3008 22302 23602 4422 10926 27042 7619 27628 6755 30569 4901 25656 27915 32646 10310 24167 3546 24772 7010 14128 29519 24693 2421 3121...

output:

705

result:

ok single line: '705'

Test #6:

score: 0
Accepted
time: 79ms
memory: 3572kb

input:

200000
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 200000 lines

Extra Test:

score: 0
Extra Test Passed