求救该题
查看原帖
求救该题
359614
Forever1507楼主2022/7/30 11:23
// Problem: P2448 无尽的生命
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2448
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Author: Forever1507
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, pos[400005], tree[400005];
struct node {
    int lt, rt;
} a[400005];
struct Node {
    int val, lenth;
} block[400005];
map<int, int> mp;
int lowbit(int x) { return x & -x; }
int query(int x) {
    int sum = 0;
    for (int i = x; i > 0; i -= lowbit(i)) sum += tree[i];
    return sum;
}
int cnt;
void update(int x, int val) {
    for (int i = x; i <= cnt; i += lowbit(i)) tree[i] += val;
    return;
}
signed main() {
    ios::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].lt >> a[i].rt;
        pos[i * 2 - 1] = a[i].lt;
        pos[i * 2] = a[i].rt;
    }
    sort(pos + 1, pos + 2 * n + 1);
    int len = unique(pos + 1, pos + 2 * n + 1) - pos - 1;
    for (int i = 1; i <= len; ++i) {
        if (pos[i] - pos[i - 1] > 1)
            block[++cnt] = {cnt, pos[i] - pos[i - 1] - 1};
        block[++cnt] = {cnt, 1};
        mp[pos[i]] = cnt;
    }
    for (int i = 1; i <= n; ++i) swap(block[mp[a[i].lt]], block[mp[a[i].rt]]);
    int sum = 0, tot = 0;
    for (register int i = 1; i <= cnt; ++i) {
        sum += tot - query(block[i].val);
        update(block[i].val, block[i].lenth);
        tot += block[i].lenth;
    }
    cout << sum;
    return 0;
}

rt,参考的第一篇题解分区的思路,样例过了,但是交上去全WA,有没有大佬看一下()

2022/7/30 11:23
加载中...