QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#569126#9310. Permutation Counting 4yylxWA 364ms3676kbC++144.1kb2024-09-16 20:41:322024-09-16 20:41:32

Judging History

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

  • [2024-09-18 14:56:40]
  • hack成功,自动添加数据
  • (/hack/835)
  • [2024-09-18 14:41:06]
  • hack成功,自动添加数据
  • (/hack/831)
  • [2024-09-17 12:14:52]
  • hack成功,自动添加数据
  • (/hack/825)
  • [2024-09-16 20:41:32]
  • 评测
  • 测评结果:WA
  • 用时:364ms
  • 内存:3676kb
  • [2024-09-16 20:41:32]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Interval {
    int l1, r1; // 第一个区间
    int l2, r2; // 第二个区间(可能不存在)

    Interval(int l = 0, int r = -1) : l1(l), r1(r), l2(0), r2(-1) {}

    // 判断在某一列是否有 1
    bool hasOne(int col) const {
        return (l1 <= col && col <= r1) || (l2 <= col && col <= r2);
    }

    // 计算当前行与另一行的对称差(模 2 加法)
    void xorWith(const Interval& other) {
        vector<pair<int, int>> events;

        // 当前行的区间事件
        events.push_back({l1, 1});
        events.push_back({r1 + 1, -1});
        if (l2 <= r2) {
            events.push_back({l2, 1});
            events.push_back({r2 + 1, -1});
        }

        // 另一行的区间事件
        events.push_back({other.l1, 1});
        events.push_back({other.r1 + 1, -1});
        if (other.l2 <= other.r2) {
            events.push_back({other.l2, 1});
            events.push_back({other.r2 + 1, -1});
        }

        // 按位置排序事件
        sort(events.begin(), events.end());

        vector<pair<int, int>> result_intervals;
        int count = 0;
        int start = -1;

        for (size_t i = 0; i < events.size(); ++i) {
            int pos = events[i].first;
            int delta = events[i].second;

            count += delta;

            // 跳过相同位置的事件,继续累加
            while (i + 1 < events.size() && events[i + 1].first == pos) {
                ++i;
                count += events[i].second;
            }

            if (count % 2 != 0 && start == -1) {
                // 开始新的区间
                start = pos;
            } else if (count % 2 == 0 && start != -1) {
                // 结束当前区间
                result_intervals.push_back({start, pos - 1});
                start = -1;
            }
        }

        // 更新当前行的区间
        if (result_intervals.size() == 0) {
            l1 = 0; r1 = -1;
            l2 = 0; r2 = -1;
        } else if (result_intervals.size() == 1) {
            l1 = result_intervals[0].first; r1 = result_intervals[0].second;
            l2 = 0; r2 = -1;
        } else {
            l1 = result_intervals[0].first; r1 = result_intervals[0].second;
            l2 = result_intervals[1].first; r2 = result_intervals[1].second;
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<Interval> rows(n);
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            rows[i] = Interval(l - 1, r - 1); // 调整为 0 索引
        }

        bool detZero = false;
        for (int i = 0; i < n; ++i) {
            // 找到当前行的最左侧的 1 的位置作为主元列
            int pivot_col = -1;
            if (rows[i].l1 <= rows[i].r1) {
                pivot_col = rows[i].l1;
            } else if (rows[i].l2 <= rows[i].r2) {
                pivot_col = rows[i].l2;
            } else {
                // 当前行全为 0,无法作为主元
                detZero = true;
                break;
            }

            // 如果当前行在主元列没有 1,尝试与下面的行交换
            if (!rows[i].hasOne(pivot_col)) {
                bool found = false;
                for (int j = i + 1; j < n; ++j) {
                    if (rows[j].hasOne(pivot_col)) {
                        swap(rows[i], rows[j]);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    detZero = true;
                    break;
                }
            }

            // 对下面的行进行消元
            for (int j = i + 1; j < n; ++j) {
                if (rows[j].hasOne(pivot_col)) {
                    rows[j].xorWith(rows[i]);
                }
            }
        }

        cout << (detZero ? 0 : 1) << '\n';
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

0
1
0
0

result:

ok 4 tokens

Test #2:

score: -100
Wrong Answer
time: 364ms
memory: 3676kb

input:

66725
14
7 7
4 6
7 8
8 13
2 13
6 13
6 10
14 14
1 10
9 11
7 9
3 8
4 12
5 12
12
2 6
3 6
7 11
2 5
1 1
6 12
8 12
2 3
7 9
7 8
1 10
1 4
10
4 8
4 4
6 10
9 10
2 3
2 7
1 3
3 4
2 2
3 10
20
3 12
10 14
19 20
19 20
1 9
7 9
13 16
17 17
16 18
2 11
5 19
6 17
11 17
3 6
3 11
7 20
8 17
3 18
10 15
9 20
16
5 10
2 10
2 1...

output:

1
1
0
0
1
0
1
1
0
1
1
0
0
0
0
0
1
0
1
0
0
1
0
0
0
1
0
1
0
0
0
0
1
0
1
1
1
1
1
0
1
0
1
1
1
1
1
1
1
1
0
1
1
0
0
0
0
0
0
1
0
1
1
0
1
1
1
0
1
0
1
0
0
0
1
0
1
0
0
1
1
1
1
0
1
1
1
1
1
1
1
0
1
0
0
1
1
0
0
1
1
0
0
1
1
1
1
1
1
0
0
1
1
1
0
0
1
1
0
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
1
1
1
1
1
0
0
0
1
0
0
1
0
1
1
...

result:

wrong answer 76th words differ - expected: '1', found: '0'