社区讨论

ODT27ps求条

P1503鬼子进村参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhj1fb88
此快照首次捕获于
2025/11/03 19:09
4 个月前
此快照最后确认于
2025/11/08 07:49
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
deque<int> de;
struct node{
    int l,r;
    node (int l,int r = 0): l(l),r(r){}
    bool operator < (const node& other)const {
        return l  < other.l;
    }
};
set<node> zhaoxutong;
auto spilt(int pos){
    auto it = zhaoxutong.lower_bound(node(pos));
    if(it != zhaoxutong.end() && it->l == pos){
        return it;
    }
    it--;
    int l = it->l;
    int r = it->r;
    zhaoxutong.erase(it);
    zhaoxutong.insert(node(l,pos-1));
    return zhaoxutong.insert(node(pos + 1,r)).first;
}
void tie(int pos){
    auto new_it = zhaoxutong.insert(node(pos, pos)).first;
    int new_l = pos, new_r = pos;
    if(new_it != zhaoxutong.begin()){
        auto left_it = new_it;
        left_it--;
        if(left_it->r + 1 == pos){
            new_l = left_it->l;
            zhaoxutong.erase(left_it);
        }
    }
    auto right_it = new_it;
    right_it++;
    if(right_it != zhaoxutong.end() && pos + 1 == right_it->l){
        new_r = right_it->r;
        zhaoxutong.erase(right_it);
    }
    zhaoxutong.erase(new_it);
    zhaoxutong.insert(node(new_l, new_r));
}
int query(int pos){
    auto it = zhaoxutong.lower_bound(node(pos));
    if(it != zhaoxutong.end() && it->l == pos){
        return it->r - it->l + 1;
    }
    it--;
    if(pos > it->r){
        return 0;
    } else {
        return it->r - it->l + 1;
    } 
}
int main(){
    int n,m;
    cin >> n >> m;
    int lat;
    zhaoxutong.insert(node(1,n));
    while(m--){
        char aa;
        cin >> aa;
        if(aa == 'D'){
            int po;
            cin >> po;
            de.push_back(po);
            lat = po;
            spilt(po);
        } else if(aa == 'R'){
            tie(de.back());
            de.pop_back();
        } else {
            int x;
            cin >> x;
            cout << query(x) << endl;
        }
    }
}

回复

1 条回复,欢迎继续交流。

正在加载回复...