zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #19D(Points)线段树

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 test(s)
    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
      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <math.h>
      4 #include <algorithm>
      5 #include <iomanip>
      6 #include <set>
      7 #include <map>
      8 #include <vector>
      9 #include <queue>
     10 #define llt long long int
     11 #define ml(x, y) ((x + y) >> 1)
     12 #define mr(x, y) (((x + y) >> 1) + 1)
     13 #define pl(p) (p << 1)
     14 #define pr(p) ((p << 1) | 1)
     15 #define N 200005
     16 using namespace std;
     17 map<int, int> mp;//离散化使用 
     18 vector<int> tx;//额外记录x
     19 int segtree[N << 2];//维护该区间的最大值,查询用 
     20 set<int> ty[N];//存对应x的所有y值 
     21 struct node
     22 {
     23     int x, y;
     24     char op[7];
     25 }point[N];
     26 void build(int l, int r, int p)//初始化线段树 
     27 {
     28     segtree[p] = 0;
     29     if (l < r)
     30     {
     31         build(l, ml(l, r), pl(p));
     32         build(mr(l, r), r, pr(p));
     33     }
     34     else
     35         ty[l].clear();
     36 }
     37 void update(int l, int r, int p, int x, int y, int tag)
     38 {
     39     if (l == r)
     40     {
     41         if (tag)//add操作 
     42         {
     43             ty[l].insert(y);
     44             if (segtree[p] < y)
     45                 segtree[p] = y;
     46         }    
     47         else
     48         {
     49             ty[l].erase(y);
     50             segtree[p] = ty[l].empty() ? 0 : *(--ty[l].end());
     51         }    
     52         return;
     53     }
     54     if (ml(l, r) >= x)
     55         update(l, ml(l, r), pl(p), x, y, tag);
     56     else
     57         update(mr(l, r), r, pr(p), x, y, tag);
     58     segtree[p] = max(segtree[pl(p)], segtree[pr(p)]);
     59 }
     60 pair<int, int> query(int l, int r, int p, int x, int y)
     61 {
     62     if (r < x || segtree[p] < y)//未找到 
     63         return make_pair(-1, -1);
     64     if (r == l)//找到了最合适的 
     65         return make_pair(r, *ty[r].lower_bound(y));
     66     pair<int, int> ans = query(l, ml(l, r), pl(p), x, y);//先往左找 
     67     if (ans.first != -1)//找到了 
     68         return ans;
     69     return query(mr(l, r), r, pr(p), x, y);//未找到,右边找 
     70 }
     71 int main()
     72 {
     73     int n, i, size;
     74     while (~scanf("%d", &n))
     75     {
     76         tx.clear();
     77         mp.clear();
     78         for (i = 0; i < n; i++)
     79         {
     80             scanf("%s%d%d", point[i].op, &point[i].x, &point[i].y);
     81             tx.push_back(point[i].x);    
     82         }
     83         sort(tx.begin(), tx.end());//排序
     84         tx.erase(unique(tx.begin(), tx.end()), tx.end());//除去相同的元素 
     85         size = tx.size() - 1;
     86         build(0, size, 1);
     87         for (i = 0; i <= size; i++)//进行离散化 
     88             mp[tx[i]] = i;
     89         for (i = 0; i < n; i++)
     90         {
     91             if (point[i].op[0] == 'f')
     92             {
     93                 pair<int, int> ans = query(0, size, 1, mp[point[i].x] + 1, point[i].y + 1);
     94                 if (ans.first >= 0)
     95                     printf("%d %d
    ", tx[ans.first], ans.second);
     96                 else
     97                     puts("-1");
     98                 continue; 
     99             }
    100             update(0, size, 1, mp[point[i].x], point[i].y, point[i].op[0] == 'a'? 1 : 0);
    101         }     
    102     } 
    103     return 0;
    104 }
  • 相关阅读:
    聆听生活——用心创造
    zedboard通过BRAM实现PS和PL的简单通信
    使用FDATOOL生成xilinx中FIR滤波器IP核的系数
    PCI Express
    波若波罗密多心经
    imp导入时出现imp-00017的问题
    数据库表对比,vim裁剪方法
    [测]jieba分词
    [原]批量修改指定名称的文件名
    [原]通过配合ffmpeg.exe获取视频文件时长
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3453760.html
Copyright © 2011-2022 走看看