-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path2D Compressed Fenwick.cpp
113 lines (100 loc) · 3.35 KB
/
2D Compressed Fenwick.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
//
// main.cpp
// practice
//
// Created by Mahmud on 04/20/18.
// Copyright © 2018 Mahmud. All rights reserved.
//
// 2D Longest increasing sequence problem
// Solution with 2D-Fenwick trees with grid compression
// O(N*log^2(N)) solution
#pragma GCC optimize("-0g")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int MAX = 100005;
int N, Nx, Ny;
int x[MAX], y[MAX];
int dp[MAX];
vector<int> gridValues[MAX];
vector<int> compressedFenwick[MAX];
void update(vector<int> &fenwick, int position, int treeSize, int value) {
for ( ; position <= treeSize; position += (position & -position)) {
fenwick[position] = max(fenwick[position], value);
}
}
int get(vector<int> &fenwick, int position) {
int s = 0;
for ( ; position > 0; position -= (position & -position)) {
s = max(s, fenwick[position]);
}
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;
}
int main() {
//scanf("%d", &N);
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]);
}
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();
for (int i = 1; i <= N; i ++) {
for (int j = x[i] - 1; j > 0; j -= (j & -j)) {
gridValues[j].push_back(y[i]);
}
for (int j = x[i]; j <= Nx; j += (j & -j)) {
gridValues[j].push_back(y[i]);
}
}
for (int i = 1; i <= Nx; i ++) {
gridValues[i].push_back(N);
sort(gridValues[i].begin(), gridValues[i].end());
gridValues[i].erase(unique(gridValues[i].begin(), gridValues[i].end()), gridValues[i].end());
compressedFenwick[i].resize((int)gridValues[i].size() + 1, 0);
}
int result = 0;
for (int i = 1; i <= N; i ++) {
dp[i] = 1;
for (int j = x[i] - 1; j > 0; j -= (j & -j)) {
int currentY = (int)(lower_bound(gridValues[j].begin(), gridValues[j].end(), y[i]) - gridValues[j].begin()) + 1;
dp[i] = max(dp[i], get(compressedFenwick[j], currentY - 1) + 1);
}
for (int j = x[i]; j <= Nx; j += (j & -j)) {
int currentY = (int)(lower_bound(gridValues[j].begin(), gridValues[j].end(), y[i]) - gridValues[j].begin()) + 1;
update(compressedFenwick[j], currentY, (int)compressedFenwick[j].size(), dp[i]);
}
result = max(result, dp[i]);
}
cout << result << endl;
return 0;
}