-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiquid.cpp
More file actions
146 lines (126 loc) · 2.16 KB
/
Copy pathLiquid.cpp
File metadata and controls
146 lines (126 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 2005;
const int INF = 0x3F3F3F3F;
const double eps = 1e-6;
const int MOD = 1e9 + 7;
set<pair<int, int> > seg;
void doTask(int a, int b)
{
if(!b) return;
set<pair<int, int> >::iterator it1, it2;
int st, ed;
it1 = seg.upper_bound(make_pair(a, INF));
it2 = it1;
it1 --;
int dis1 = a - it1->second, dis2;
if(dis1 > 1)
{
st = a, ed = a + b - 1;
dis2 = it2->first - ed;
if(dis2 > 1)
{
seg.insert(make_pair(st, ed));
}
else
{
ed = it2->first - 1;
seg.insert(make_pair(st, it2->second));
seg.erase(it2);
}
}
else
{
st = it1->second + 1, ed = st + b - 1;
dis2 = it2->first - ed;
if(dis2 > 1)
{
seg.insert(make_pair(it1->first, ed));
seg.erase(it1);
}
else
{
ed = it2->first - 1;
seg.insert(make_pair(it1->first, it2->second));
seg.erase(it1);
seg.erase(it2);
}
}
printf("%d %d\n", st, ed);
}
void doClean(int a)
{
set<pair<int, int> >::iterator it1;
it1 = seg.upper_bound(make_pair(a, INF));
it1 --;
int st = it1->first, ed = it1->second;
if(st == ed)
{
if(st == a)
seg.erase(it1);
}
else if(st == a)
{
seg.insert(make_pair(st + 1, ed));
seg.erase(it1);
}
else if(ed == a)
{
seg.insert(make_pair(st, ed + 1));
seg.erase(it1);
}
else if(st < a && ed > a)
{
seg.insert(make_pair(st, a - 1));
seg.insert(make_pair(a + 1, ed));
seg.erase(it1);
}
}
void show()
{
set<pair<int, int> >::iterator it1;
for(it1 = seg.begin(); it1 != seg.end(); it1 ++)
{
cout << "(" << it1->first << "," << it1->second << ") ";
}
cout << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
int n, a, b;
while(scanf("%d", &n) != EOF)
{
seg.clear();
seg.insert(make_pair(-INF,-INF));
seg.insert(make_pair(INF, INF));
for(int i = 0; i < n; i++)
{
scanf("%d", &a);
if(a >= 0)
{
scanf("%d", &b);
doTask(a, b);
}
else
{
doClean(- a);
}
// show();
}
}
return 0;
}