Ivan comes again!


​Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^​


题目描述


The Fairy Ivan gave Saya three problems to solve (Problem F). After Saya finished the first problem (Problem H), here comes the second.



This is the enhanced version of Problem H.



There is a large matrix whose row and column are less than or equal to 1000000000. And there are three operations for the matrix:



1)add: Mark an element in the matrix. The element wasn’t marked before it is marked.



2)remove: Delete an element’s mark. The element was marked before the element’s mark is deleted.



3)find: Show an element’s row and column, and return a marked element’s row and column, where the marked element’s row and column are larger than the showed 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.



Of course, Saya comes to you for help again.


输入


The input consists of several test cases.



The first line of input in each test case contains one integer N≤200000), which represents the number of operations.



Each of the next N lines containing an operation, as described above.



The last case is followed by a line containing one zero.


输出


For each case, print the case number (1, 2 …) first. Then, for each “find” operation, output the result. Your output format should imitate the sample output. Print a blank line after each test case.


示例输入






4



add 2 3



find 1 2



remove 2 3



find 1 2




0


示例输出




Case 1:



2 3
-1
用线段树维护每行的列的最大值,查找的时候就可以确定需要查找的set的位置,给的数虽然大,但是只有200000个操作,最多也就200000个数,所以可以先离线下来再离散处理一下
代码:
1、set的方法
[cpp] 
view plain
copy
print
?

#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<string> #include<cmath> #include<queue> #include<map> #include<set> #include<vector> #define INF 999999 #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxd=100; typedef long long ll; //typedef pair<int,int> info; typedef struct info { int x,y; friend bool operator<(info a,info b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } } ; int main() { "in.txt", "r", stdin); int n,x,y,kase=1; char str[maxd]; set<info> s; while(scanf("%d",&n)!=EOF && n) { s.clear(); "Case %d:\n",kase++); while(n--) { "%s %d%d",str,&x,&y); info a={x,y}; if(!strcmp(str,"add")) s.insert(a); else if(!strcmp(str,"remove")) { set<info>::iterator p=s.find({x,y}); if(p!=s.end()) s.erase(p); } else { set<info>::iterator p=s.lower_bound(a); if(p==s.end()) p=s.begin(); for(p; p!=s.end(); ++p) if(p->first>x && p->second>y) { "%d %d\n",p->first,p->second); break; } if(p==s.end()) printf("-1\n"); } } "\n"); } return 0; }



2、set+线段树优化



[cpp]  ​​view plain​​​ ​​​copy​​​ ​​​print​​​ ​​​?​



1. #include<cstdio>
2. #include<cstdlib>
3. #include<cstring>
4. #include<iostream>
5. #include<algorithm>
6. #include<string>
7. #include<cmath>
8. #include<queue>
9. #include<map>
10. #include<set>
11. #include<vector>
12. #define INF 999999
13. #define mem(a,b) memset(a,b,sizeof(a))
14. using namespace std;
15. const int maxd=200000+5;
16. const int maxn=1000000000+5;
17. typedef long long ll;
18. typedef pair<int,int> pii;
19. int n,x,y,kase=1,cnt,Num;
20. char str[30];
21. set<int> s[maxd];
22. struct info
23. {
24. int x,y,z;
25. } a[maxd];
26. int b[maxd],maxv[maxd*2];
27.
28. int query(int num,int pos1,int l,int r,int o)///从pos1开始查找大于等于num
29. {
30. if(l==r)
31. {
32. if(maxv[o]>=num) return l;
33. return -1;
34. }
35. int m= l+(r-l)/2,ans=-1;
36. if(pos1<=m && maxv[o*2]>=num) ans=query(num,pos1,l,m,o*2);
37. if(ans!=-1) return ans;
38. if(maxv[o*2+1]>=num) ans=query(num,pos1,m+1,r,o*2+1);
39. return ans;
40. }
41.
42. void update(int p,int v,int l,int r,int o)///a[p]=v;
43. {
44.
45. if(l==r) maxv[o]=v;
46. else
47. {
48. int m=l+(r-l)/2;
49. if(p<=m) update(p,v,l,m,o*2);
50. else update(p,v,m+1,r,o*2+1);
51. maxv[o]=max(maxv[o*2],maxv[o*2+1]);
52. }
53. }
54.
55. void init()
56. {
57. Num=0;
58. mem(maxv,-1);
59. for(int i=0; i<maxd; ++i)
60. s[i].clear();
61. "Case %d:\n",kase++);
62. for(int i=0; i<n; ++i)
63. {
64. "%s %d%d",str,&a[i].x,&a[i].y);
65. if(!strcmp(str,"add"))
66. {
67. a[i].z=1;
68. b[Num++]=a[i].x;
69. }
70. else if(!strcmp(str,"find"))
71. a[i].z=2;
72. else
73. a[i].z=3;
74.
75. ///先离线下来
76. cnt=1;
77. for(int i=1; i<Num; ++i)
78. {
79. if(b[i]!=b[i-1])
80. b[cnt++]=b[i];
81. ///离散化处理
82. sort(b,b+cnt);
83. }
84.
85. void solve()
86. {
87. for(int i=0; i<n; ++i)
88. if(a[i].z==1)///add
89. {
90. int pos=lower_bound(b,b+cnt,a[i].x)-b;
91. // cout<<pos<<endl;
92. s[pos].insert(a[i].y);
93. update(pos,*(--s[pos].end()),0,cnt,1);
94. }
95. else if(a[i].z==2)///find
96. {
97. int pos=upper_bound(b,b+cnt,a[i].x)-b;
98. if(pos==cnt)
99. {
100. "-1\n");
101. continue;
102. }
103. int ans=query(a[i].y+1,pos,0,cnt,1);
104. if(ans==-1) printf("-1\n");
105. else printf("%d %d\n",b[ans],*(s[ans].lower_bound(a[i].y+1)));
106.
107. }
108. else///remove
109. {
110. int pos=lower_bound(b,b+cnt,a[i].x)-b;
111. s[pos].erase(a[i].y);
112. if(s[pos].size()==0) update(pos,-1,0,cnt,1);
113. else
114. update(pos,*(--s[pos].end()),0,cnt,1);
115. }
116. }
117.
118. int main()
119. {
120. "in.txt", "r", stdin);
121. while(scanf("%d",&n)!=EOF && n)
122. {
123. init();
124. solve();
125. "\n");
126. }
127. return 0;
128. }