首页 > ACM题库 > HDU-杭电 > HDU 3627-Giant For-线段树-[解题报告]HOJ
2014
11-29

HDU 3627-Giant For-线段树-[解题报告]HOJ

Giant For

问题描述 :

It is known to us all that YY and LMY are mathematics lovers. They like to find and solve interesting mathematic problems together. Now LMY designs a game for matrix. There is a large matrix whose rows and columns are both not more than 1000000000. And there are three operations for the matrix:
1) add: Mark an element in the matrix. It is guaranteed that the element has not been marked before.
2) remove: Delete an element’s mark. It is guaranteed that the element has been marked before.
3) find: For a given element’s row and column, return a marked element’s row and column, where the marked element’s row and column are larger than the given element’s row and column respectively. If there are multiple solutions, return the element whose row is the smallest; and if there are still multiple solutions, return the element whose column is the smallest. If there is no solution, return -1.
LMY lets YY develop a program to solve the problem. Could you also develop a program to solve the problem?

输入:

The input consists of multiple test cases. For each test case, the first line contains only one integer n. n ≤ 200000. Each of the next n lines describes an operation. There is a blank line between two consecutive test cases.
End of input is indicated by a line containing a zero.

输出:

The input consists of multiple test cases. For each test case, the first line contains only one integer n. n ≤ 200000. Each of the next n lines describes an operation. There is a blank line between two consecutive test cases.
End of input is indicated by a line containing a zero.

样例输入:

5
add 48 1
add 25 69
add 88 52
remove 25 69
add 23 89

10
add 47 23
find 66 83
find 27 73
add 84 97
find 10 58
remove 47 23
add 41 89
remove 41 89
find 65 68
add 25 41

0

样例输出:

Case 1:

Case 2:
-1
-1
84 97
84 97

线段树?这题目明显坑爹,哪有这种线段树!!真无聊,map + set  秒掉~

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<map>
#include<set>
#include<cstring>
using namespace std;

map<int,set<int> > m;
map<int,set<int> >::iterator mi;
set<int>::iterator si;
set<int>s;
map<int,int>yy;
int n;
int tx,ty;
char c[11];
int temp,t2;
int tt;

int main()
{
    int cas=1;
    while(cin>>n)
    {
        if(n==0)
        {
            break;
        }
        if(cas!=1)
        {
            cout<<endl;
        }
        printf("Case %d:\n",cas++);
        m.clear();
        s.clear();
        yy.clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",c);
            scanf("%d%d",&tx,&ty);
            if(c[0]=='a')
            {
                if(yy.find(ty) == yy.end())
                {
                    yy[ty] = 1;
                }
                else
                {
                    yy[ty]++;
                }
                if(s.find(tx)==s.end())
                {
                    s.insert(tx);
                }
                mi = m.find(tx);
                if(mi != m.end())
                {
                    si = mi -> second.find(ty);
                    if(si!=mi -> second.end())
                    {
                        continue;
                    }
                    else
                    {
                        mi->second.insert(ty);
                    }
                }
                else
                {
                    m[tx].insert(ty);
                }
            }
            else if(c[0]=='f')
            {
                if(yy.upper_bound(ty) == yy.end())
                {
                    printf("-1\n");
                    continue;
                }
                tt = tx;
                while(true)
                {
                    if(s.empty())
                    {
                        printf("-1\n");
                        break;
                    }
                    si = s.upper_bound(tt);
                    if(si!=s.end())
                    {
                        temp = *si;
                        tt = temp;
                        if(m[temp].empty())
                        {
                            continue;
                        }
                        si = m[temp].upper_bound(ty);
                        if(si != m[temp].end())
                        {
                            t2 = *si;
                            printf("%d %d\n",temp,t2);
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        printf("-1\n");
                        break;
                    }
                }
            }
            else if(c[0]=='r')
            {
                m[tx].erase(ty);
                if(m[tx].empty())
                {
                    s.erase(tx);
                }
                if(!--yy[ty])
                {
                    yy.erase(ty);
                }
            }
        }
    }
    return 0;
}

参考:http://blog.csdn.net/zz_1215/article/details/7318800


  1. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。