-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path2D LIS segment tree complete array.cpp
211 lines (188 loc) · 5.45 KB
/
2D LIS segment tree complete array.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// main.cpp
// practice
//
// Created by Mahmud on 04/18/18.
// Copyright © 2018 Mahmud. All rights reserved.
//
#pragma GCC optimize("-0g")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
const int MAX = 100005;
int values[MAX * 75] = {-1};
int leftChildID[MAX * 75];
int rightChildID[MAX * 75];
int offset = 0;
int N, Nx, Ny;
int x[MAX], y[MAX];
int roots[MAX];
int dp[MAX];
void update(int rootID, int low, int high, int position, int value) {
if (values[low] == -1) return;
if (low == high) {
values[rootID] = value;
return;
}
int middle = (low + high) >> 1;
if (position <= middle) {
if (leftChildID[rootID] == 0) {
leftChildID[rootID] = ++offset;
values[offset] = 0;
}
update(leftChildID[rootID], low, middle, position, value);
}
else {
if (rightChildID[rootID] == 0) {
rightChildID[rootID] = ++offset;
values[offset] = 0;
}
update(rightChildID[rootID], middle + 1, high, position, value);
}
values[rootID] = max(values[leftChildID[rootID]], values[rightChildID[rootID]]);
}
int get(int rootID, int low, int high, int l, int r) {
if (values[rootID] == -1) return 0;
if (low > r || high < l) return 0;
if (low >= l && high <= r) return values[rootID];
int middle = (low + high) >> 1;
int q1 = get(leftChildID[rootID], low, middle, l, r);
int q2 = get(rightChildID[rootID], middle + 1, high, l, r);
return q1 > q2 ? q1 : q2;
//return max(get(pool[root.leftChildID], low, middle, l, r),
// get(pool[root.rightChildID], middle + 1, high, l, r));
}
void outerUpdate(int position, int innerPosition, int value) {
for ( ; position <= Nx; position += (position & -position)) {
if (values[roots[position]] == -1) {
roots[position] = ++offset;
values[roots[position]] = 0;
}
update(roots[position], 1, Ny, innerPosition, value);
}
}
int outerGet(int position, int innerL, int innerR) {
int s = 0;
for ( ; position > 0; position -= (position & -position)) {
int current = get(roots[position], 1, Ny, innerL, innerR);
if (current > s) s = current;
}
return s;
}
template <class T>
void fastInput(T &N) {
char ch;
int sign = 1;
N = 0;
while ((ch = getchar_unlocked()) && ch == ' ') {};
if (ch == '-') sign = -1;
else if (isdigit(ch)) N = ch - '0';
while ((ch = getchar_unlocked()) && isdigit(ch)) {
N = (N << 1) + (N << 3) + ch - '0';
}
if (sign == -1) N = ~N + 1;
}
/**** FAST IO ****/
inline int readChar();
template <class T = int> inline T readInt();
template <class T> inline void writeInt( T x, char end = 0 );
inline void writeChar( int x );
inline void writeWord( const char *s );
/** Read */
static const int buf_size = 4096;
inline int getChar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len)
return -1;
return buf[pos++];
}
inline int readChar() {
int c = getChar();
while (c <= 32)
c = getChar();
return c;
}
template <class T>
inline T readInt() {
int s = 1, c = readChar();
T x = 0;
if (c == '-')
s = -1, c = getChar();
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
}
/** Write */
static int write_pos = 0;
static char write_buf[buf_size];
inline void writeChar( int x ) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
template <class T>
inline void writeInt( T x, char end ) {
if (x < 0)
writeChar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n)
s[n++] = '0' + x % 10, x /= 10;
while (n--)
writeChar(s[n]);
if (end)
writeChar(end);
}
inline void writeWord( const char *s ) {
while (*s)
writeChar(*s++);
}
struct Flusher {
~Flusher() {
if (write_pos)
fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
} flusher;
int main() {
//scanf("%d", &N);
N = readInt();
//fastInput(N);
//for (int i = 1; i <= N; i ++) scanf("%d%d", &x[i], &y[i]);
for (int i = 1; i <= N; i ++) {
//fastInput(x[i]);
//fastInput(y[i]);
x[i] = readInt();
y[i] = readInt();
}
vector<int> values = vector<int> (x + 1, x + N + 1);
sort(values.begin(), values.end());
values.erase(unique(values.begin(), values.end()), values.end());
for (int i = 1; i <= N; i ++) {
x[i] = (int)(lower_bound(values.begin(), values.end(), x[i]) - values.begin()) + 1;
}
Nx = (int)values.size();
values = vector<int> (y + 1, y + N + 1);
sort(values.begin(), values.end());
values.erase(unique(values.begin(), values.end()), values.end());
for (int i = 1; i <= N; i ++) {
y[i] = (int)(lower_bound(values.begin(), values.end(), y[i]) - values.begin()) + 1;
}
Ny = (int)values.size();
int result = 0;
for (int i = 1; i <= N; i ++) {
dp[i] = outerGet(x[i] - 1, 1, y[i] - 1) + 1;
//cout << i << " --> " << x[i] << " " << y[i] << endl;
//cout << i << " " << dp[i] << endl;
if (dp[i] > result) result = dp[i];
outerUpdate(x[i], y[i], dp[i]);
}
cout << result << endl;
return 0;
}