柯朵莉树全部MLE求调
  • 板块P5350 序列
  • 楼主SyntaxErr0r
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/10/27 23:21
  • 上次更新2023/10/27 05:30:23
查看原帖
柯朵莉树全部MLE求调
611107
SyntaxErr0r楼主2022/10/27 23:21
#include<iostream>
#include<set>
#include<vector>
using namespace std;

const int N = 3e5+10;
const int MOD = 1000000007;
struct Node {
    int lf, rt;
    mutable long long val;
    Node(int L,int R = 0,long long V = 0) : lf(L), rt(R), val(V) {}
    bool operator< (const Node &a) const {
        return lf < a.lf;
    }
};
int n, T, a[N];
set<Node> Chtholly;
vector<Node> v1, v2;

inline auto split(int pos) {
    auto it = Chtholly.lower_bound(Node(pos));
    if (it->lf == pos && it != Chtholly.end()) return it; // 此区间已经天然的被split了
    --it;
    if (it->rt < pos) return Chtholly.end();// 要找的区间的rt比要split的位置还小了 只可能上一个it指向的是s.end()
    // 现在这就是要找的迭代器了
    long long v = it->val;
    int l = it->lf, r = it->rt;
    Chtholly.erase(it);
    Chtholly.insert(Node(l,pos-1,v));
    return Chtholly.insert(Node(pos,r,v)).first;
}

inline void assign(int l,int r,long long v) {
    auto itR = split(r+1), itL = split(l);
    Chtholly.erase(itL,itR);
    Chtholly.insert(Node(l,r,v));
}

inline void add(int l,int r,long long v) {
    auto itR = split(r+1), itL = split(l);
    for (auto it = itL; it != itR; ++it)
        it->val += v;
}

inline void copy(int l1,int r1,int l2,int r2) {
    auto itR = split(r1+1), itL = split(l1), _itR = split(r2+1), _itL = split(l2);
    int delta = l2-l1;
    v1.clear();
    for (auto it = itL; it != itR; ++it)
        v1.push_back(Node(it->lf+delta,it->rt+delta,it->val));
    Chtholly.erase(_itL,_itR);
    for (auto it:v1) {
        Chtholly.insert(it);
    }
}

inline void swap(int l1,int r1,int l2,int r2) {
    auto itR = split(r1+1), itL = split(l1), _itR = split(r2+1), _itL = split(l2);
    int delta = l2-l1;
    v1.clear(), v2.clear();
    for (auto it = itL; it != itR; ++it)
        v1.push_back(Node(it->lf+delta,it->rt+delta,it->val));
    for (auto it = _itL; it != _itR; ++it)
        v2.push_back(Node(it->lf-delta,it->rt-delta,it->val));
    if (abs(l2-l1) != 1) {
        Chtholly.erase(_itL,_itR);
        Chtholly.erase(itL,itR);
    } else {
        Chtholly.erase(itL);
        Chtholly.erase(_itL);
    }
    for (auto it:v1) Chtholly.insert(it);
    for (auto it:v2) Chtholly.insert(it);
}

inline void reverse(int l,int r) {
    auto itR = split(r+1), itL = split(l);
    v1.clear();
    for (auto it = itL; it != itR; ++it)
        v1.push_back(Node(it->lf,it->rt,it->val));
    Chtholly.erase(itL,itR);
    for (auto it:v1) Chtholly.insert(Node(r-it.rt+l,r-it.lf+l,it.val));
}

inline int sum(int l,int r) {
    auto itR = split(r+1), itL = split(l);
    long long res = 0;
    for (auto it = itL; it != itR; ++it) {
        res += (it->rt-it->lf+1)*it->val;
        res %= MOD;
    }
    return res%MOD;
}

inline void debug(void) {
    for (auto it:Chtholly)
        for (int i = it.lf; i <= it.rt; ++i)
            cout << it.val%MOD << " ";
    cout << "\n";
}

signed main() {
    //ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    cin >> n >> T;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        Chtholly.insert(Node(i,i,a[i]));
    }
    while (T--) {
        int opt; cin >> opt;
        int l, r, l1, r1, v;
        switch(opt) {
        case 1:
            cin >> l >> r;
            cout << sum(l,r) << "\n"; break;
        case 2:
            cin >> l >> r >> v;
            assign(l,r,v); break;
        case 3:
            cin >> l >> r >> v;
            add(l,r,v); break;
        case 4:
            cin >> l >> r >> l1 >> r1;
            copy(l,r,l1,r1); break;
        case 5:
            cin >> l >> r >> l1 >> r1;
            swap(l,r,l1,r1); break;
        case 6:
            cin >> l >> r;
            reverse(l,r); break;
        }
    }
    debug();
    return 0;
}
2022/10/27 23:21
加载中...