zoukankan      html  css  js  c++  java
  • CodeForces 19D Points

    Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coordinate system on it as follows: point (0, 0) is located in the bottom-left corner, Ox axis is directed right, Oy axis is directed up. Pete gives Bob requests of three types:

    • add x y — on the sheet of paper Bob marks a point with coordinates (x, y). For each request of this type it's guaranteed that point(x, y) is not yet marked on Bob's sheet at the time of the request.
    • remove x y — on the sheet of paper Bob erases the previously marked point with coordinates (x, y). For each request of this type it's guaranteed that point (x, y) is already marked on Bob's sheet at the time of the request.
    • find x y — on the sheet of paper Bob finds all the marked points, lying strictly above and strictly to the right of point (x, y). Among these points Bob chooses the leftmost one, if it is not unique, he chooses the bottommost one, and gives its coordinates to Pete.

    Bob managed to answer the requests, when they were 10, 100 or 1000, but when their amount grew up to 2·105, Bob failed to cope. Now he needs a program that will answer all Pete's requests. Help Bob, please!

    Input

    The first input line contains number n (1 ≤ n ≤ 2·105) — amount of requests. Then there follow n lines — descriptions of the requests.add x y describes the request to add a point, remove x y — the request to erase a point, find x y — the request to find the bottom-left point. All the coordinates in the input file are non-negative and don't exceed 109.

    Output

    For each request of type find x y output in a separate line the answer to it — coordinates of the bottommost among the leftmost marked points, lying strictly above and to the right of point (x, y). If there are no points strictly above and to the right of point (x, y), output -1.

    Sample Input

    Input
    7
    add 1 1
    add 3 4
    find 0 0
    remove 1 1
    find 0 0
    add 1 1
    find 0 0
    Output
    1 1
    3 4
    1 1
    Input
    13
    add 5 5
    add 5 6
    add 5 7
    add 6 5
    add 6 6
    add 6 7
    add 7 5
    add 7 6
    add 7 7
    find 6 6
    remove 7 7
    find 6 6
    find 4 4
    Output
    7 7
    -1
    5 5

    题解:
    线段树维护

    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn = 2e5 + 500;
    typedef pair < int ,int > dii;
    struct operation
    {
        int x , y , tp ;
    }op[maxn];
    char str[25];
    int c,n,QueryY;
    
    vector < int > vi;
    set < int > :: iterator it , it2;
    
    struct Segmenttree
    {
        typedef int SgTreeDataType;
        struct treenode
        {
          int L , R  ;
          SgTreeDataType maxy;
          set < int > s;
          void updata(SgTreeDataType v){
               s.insert(v);
               maxy=max(maxy,v);
          }
          void remove(SgTreeDataType v){
              s.erase(v);
              if(s.empty()) maxy=0;
              else{
                  it=s.end();it--;
                  maxy=*it;
              }
          }
        };
    
        treenode tree[maxn * 4];
    
        inline void push_up(int o){
            int lson = o << 1 , rson = o << 1 | 1;
            tree[o].maxy=max(tree[lson].maxy,tree[rson].maxy);
        }
    
        void build(int L , int R , int o){
            tree[o].L = L , tree[o].R = R,tree[o].maxy=0;tree[o].s.empty();
            if (R > L){
                int mid = (L+R) >> 1;
                build(L,mid,o<<1);
                build(mid+1,R,o<<1|1);
            }
        }
    
        void updata(int p,SgTreeDataType v,int o){
            int L = tree[o].L , R = tree[o].R;
            if (L==R) tree[o].updata(v);
            else{
                int mid = (L+R)>>1;
                if (p <= mid) updata(p,v,o<<1);
                else updata(p,v,o<<1|1);
                push_up(o); 
            }
        }
    
        void remove(int p,SgTreeDataType v,int o){
            int L = tree[o].L , R = tree[o].R;
            if (L==R) tree[o].remove(v);
            else{
                int mid = (L+R)>>1;
                if (p <= mid) remove(p,v,o<<1);
                else remove(p,v,o<<1|1);
                push_up(o); 
            }
        }
        
        dii query(int p,int o){
            int L = tree[o].L , R = tree[o].R;
            if (L>=p){
                int lson = o << 1 , rson = o << 1 | 1;
                if(tree[o].maxy > QueryY){
                    if(L==R){
                        it = tree[o].s.upper_bound(QueryY);
                        return make_pair( L , *it);
                    }else{
                        if(tree[lson].maxy > QueryY) return query(p , lson);
                        else if(tree[rson].maxy > QueryY) return query(p , rson);
                    }
                }else return make_pair(-1,-1);
            }
            else{
                int mid = (L+R)>>1;
                dii res(-1,-1);
                if (p <= mid) res = query(p,o<<1);
                if(~res.first) return res;
                return query(p,o<<1|1);
            }
        }
    }Sgtree;
    
    
    inline int GetRank(int x){
        return lower_bound( vi.begin() , vi.begin() + c , x ) - vi.begin();
    }
    
    int main(int argc,char *argv[]){
        scanf("%d",&n);
        for(int i = 1 ; i <= n ; ++ i){
            int x , y;
            scanf("%s%d%d",str,&op[i].x,&op[i].y);
            if(str[0]=='a') op[i].tp = 0;
            else if(str[0]=='r') op[i].tp = 1;
            else op[i].tp = 2;
            vi.push_back(op[i].x);
        }
        sort( vi.begin() , vi.end() );
        c = unique( vi.begin() , vi.end() ) - vi.begin();
        Sgtree.build( 0 , c - 1 , 1);
        for(int i = 1 ; i <= n ; ++ i){
            op[i].x = GetRank( op[i].x );
            if(op[i].tp==0) Sgtree.updata(op[i].x,op[i].y,1);
            else if(op[i].tp==1) Sgtree.remove(op[i].x,op[i].y,1);
            else{
                if(op[i].x == c - 1){
                    printf("-1
    ");
                    continue;
                }
                QueryY = op[i].y;
                dii ans = Sgtree.query(op[i].x + 1 , 1);
                if(ans.first==-1) printf("-1
    ");
                else printf("%d %d
    ",vi[ans.first],ans.second);
            }
        }
        return 0;
    }
  • 相关阅读:
    C语言-10-位域与共用体
    python-并发编程
    计算机操作系统
    网络编程-Socket
    网络编程-基础
    python-面向对象进阶
    python-面向对象
    python-模块分类与导入
    python-函数进阶
    python-函数内置方法
  • 原文地址:https://www.cnblogs.com/Xiper/p/5033850.html
Copyright © 2011-2022 走看看