SGU 339. Segments
339. Segments
Time limit per test: 2.25 second(s)
Memory limit: 65536 kilobytes
Memory limit: 65536 kilobytes
input: standard
output: standard
output: standard
Consider a real axis and segments on it. You are to write a program that processes queries of two kinds.
Query '
+ L R' adds a segment from point L to point R. You program should print the number of segments that are (non-strictly) inside the new one.
Query '
- L R' removes one segment from point L to point R. If there is no such segment, ignore this query.
You may assume that no more than 1000 segments will present on the axis simultaneously.
Input
Each line of the input file contains one query. Query can be in the form '
- L R' or '
+ L R', where L and R are integers (-109 ≤ L < R ≤ 109). There will be no more than 250 · 103queries.
Output
Print to the output one line for each '
+ L R' query, containing the number of segments inside the added segment.
Example(s)
sample input |
sample output |
+ 1 2 + 1 2 + 0 3 - 1 2 + 1 2 |
0 1 2 1 |
Online Contester Team © 2002 - 2010. All rights reserved. |
#include <iostream> #include<stdio.h> #include<algorithm> #include<vector> #include<math.h> #include<set> #define inf 1000000000 using namespace std; multiset<pair<int,int> > st; multiset<pair<int,int> >::iterator it; int main() { int left,right,tot; char ch; while(scanf("%c%d%d",&ch,&left,&right)!=EOF) { if (ch=='-') { it=st.find(make_pair(left,right)); if (it!=st.end()) st.erase(it); } else { tot=0; for(it=st.begin();it!=st.end();it++) if (it->first>=left&&it->second<=right) tot++; printf("%d\n",tot); st.insert(make_pair(left,right)); } scanf("%c",&ch); } return 0; } /* + 1 3 + 2 4 + 0 5 - 2 4 + 1 4 + 0 6 - 1 3 + 2 7 + 1 2 + 2 3 + 3 4 + 5 6 + 1 6 + 2 4 + 3 5 + 4 6 */