其他分享
首页 > 其他分享> > 洛谷 P1110 [ZJOI2007]报表统计

洛谷 P1110 [ZJOI2007]报表统计

作者:互联网

刷 Splay 时碰见了这题。发现根本不用写 Splay,直接链表 + std::multiset 乱搞就好了。
由于我自带大常数,吸氧才过掉。。。

#include <cstdio>
#include <set>

inline int read(void){
    int res = 0; char ch = getchar();
    while(ch < '0' || ch > '9')
        ch = getchar();
    while(ch >= '0' && ch <= '9')
        res = res * 10 + ch - 48, ch = getchar();
    return res; 
} 

const int MAXN = 5e5 + 19;

struct Node{
    int val;
    Node *l, *r;
    Node(){
        l = NULL, r = NULL;
    }
};

int n, m;
Node *pos[MAXN];

std::set<int>mySet;
std::set<int>::iterator it;
std::multiset<int>myMul;

int ans1 = 0x3f3f3f3f, ans2 = 0x3f3f3f3f;

inline int min(const int& a, const int& b){
    return a < b ? a : b;
}

inline int abs(const int& a){
    return a > 0 ? a : -a;
}

char ch[19];

int main(){
    n = read(), m = read();
    for(int i = 1; i <= n; ++i)
        pos[i] = new Node, pos[i]->val = read();
    for(int i = 1; i <= n; ++i){
        if(i != 1)
            pos[i]->l = pos[i - 1];
        if(i != n)
            pos[i]->r = pos[i + 1];
        if(pos[i]->l != NULL)
            myMul.insert(abs(pos[i]->val - pos[i]->l->val));
        it = mySet.lower_bound(pos[i]->val);
        if(it != mySet.end())
            ans2 = min(ans2, abs(*it - pos[i]->val));
        if(it != mySet.begin())
            ans2 = min(ans2, abs(*(--it) - pos[i]->val));
        mySet.insert(pos[i]->val);
    }
    while(m--){
        std::scanf("%s", ch);
        if(ch[4] == 'R'){
            int g = read();
            Node *m = pos[g], *n = m->r;
            Node *p = new Node;
            p->val = read();
            m->r = p, p->l = m;
            if(n != NULL){
                n->l = p, p->r = n;
                if((it = myMul.find(abs(m->val - n->val))) != myMul.end())
                    myMul.erase(it);
                myMul.insert(abs(p->val - n->val));
            }
            myMul.insert(abs(p->val - m->val));
            it = mySet.lower_bound(p->val);
            if(it != mySet.end())
                ans2 = min(ans2, abs(*it - p->val));
            if(it != mySet.begin())
                ans2 = min(ans2, abs(*(--it) - p->val));
            mySet.insert(p->val);
            pos[g] = p;
        }
        else if(ch[4] == 'G')
            std::printf("%d\n", *myMul.begin());
        else
            std::printf("%d\n", ans2);
    }
    return 0;
}

标签:P1110,ch,洛谷,val,int,ans2,pos,abs,ZJOI2007
来源: https://www.cnblogs.com/natsuka/p/12492145.html