sdut-2159 Ivan comes again!(set+线段树)

网友投稿 307 2022-08-24

sdut-2159 Ivan comes again!(set+线段树)

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 #include #include #include #include #include #include #include #include #include #include #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 info; typedef struct info { int x,y; friend bool operator<(info a,info b) { if(a.x==b.x) return a.y 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::iterator p=s.find({x,y}); if(p!=s.end()) s.erase(p); } else { set::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. #include2. #include3. #include4. #include5. #include6. #include7. #include8. #include9. #include10. #include11. #include12. #define INF 99999913. #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 pii;19. int n,x,y,kase=1,cnt,Num;20. char str[30];21. set s[maxd];22. struct info23. {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开始查找大于等于num29. {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. else47. {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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:set::count
下一篇:餐饮变局,营销变卷!(餐饮企业的内卷现象)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~