Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : LIS2 - Another Longest Increasing Subsequence Problem
Source : Spoj
Category : LIS
Algorithm : solving Insert-Query Problems Offline, LIS
Verdict : Accepted
- #include "bits/stdc++.h"
-
- using namespace std;
-
- #define FI freopen("in.txt", "r", stdin)
- #define FO freopen("out.txt", "w", stdout)
- #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
-
- #define FOR(i, n) for (int i = 1; i <= n; i++)
- #define For(i, n) for (int i = 0; i < n; i++)
- #define ROF(i, n) for (int i = n; i >= 1; i--)
- #define Rof(i, n) for (int i = n-1; i >= 0; i--)
- #define FORI(i, n) for (auto i : n)
- #define REP(i, a, b) for (int i = a; i <= b; i++)
-
- #define ll long long
- #define ull unsigned long long
- #define vi vector <int>
- #define vl vector <ll>
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define mk make_pair
- #define ff first
- #define ss second
- #define eb emplace_back
- #define em emplace
- #define pb push_back
- #define ppb pop_back
- #define All(a) a.begin(), a.end()
- #define memo(a, b) memset(a, b, sizeof a)
- #define Sort(a) sort(All(a))
- #define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
- #define rev(a) reverse(All(a))
- #define sz(a) (int)a.size()
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- #define maxAll(a) *max_element(All(a))
- #define minAll(a) *min_element(All(a))
- #define allUpper(a) transform(All(a), a.begin(), :: toupper)
- #define allLower(a) transform(All(a), a.begin(), :: tolower)
- #define endl '\n'
- #define nl puts("")
- #define ub upper_bound
- #define lb lower_bound
- #define Exp exp(1.0)
- #define PIE 2*acos(0.0)
- #define Sin(a) sin(((a)*PIE)/180.0)
- #define EPS 1e-9
-
- #include "ext/pb_ds/assoc_container.hpp"
- #include "ext/pb_ds/tree_policy.hpp"
- using namespace __gnu_pbds;
-
- template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
-
- #include "ext/rope"
- using namespace __gnu_cxx;
-
-
-
-
-
-
-
-
-
-
- #define trace1(x) cerr << #x << ": " << x << endl;
- #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
- #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
- #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
- #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
- #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
-
- inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
- inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
- inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
- inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
-
- #define popcount(mask) __builtin_popcount(mask) // count set bit
- #define popcountLL(mask) __builtin_popcountll(mask) // for long long
-
- inline int read() { int a; scanf("%d", &a); return a; }
- inline ll readLL() { ll a; scanf("%lld", &a); return a; }
- inline double readDD() { double a; scanf("%lf", &a); return a; }
-
- template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
- int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
- ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
-
- #define inf 1e17
- #define mod 1000000007
-
- static const int maxn = 2e5 + 5;
- static const int logn = 18;
-
- struct node
- {
- int x, y, id;
- int dp;
- node(int x = 0, int y = 0, int id = 0, int dp = 0) :
- x(x), y(y), id(id), dp(dp) {}
-
- inline void read()
- {
- scanf("%d %d", &x, &y);
- }
- inline friend bool operator < (const node &A, const node &B)
- {
- if (A.x == B.x) return A.y < B.y;
- return A.x < B.x;
- }
- } p[maxn];
-
- int n;
- int Tree[maxn];
-
- inline void update(int pos, int val)
- {
- while (pos <= n)
- {
- Tree[pos] = max(Tree[pos], val);
- pos += (pos & -pos);
- }
- }
-
- inline int query(int pos)
- {
- int ret = 0;
- while (pos > 0)
- {
- ret = max(ret, Tree[pos]);
- pos -= (pos & -pos);
- }
- return ret;
- }
-
- inline void recover(int pos)
- {
- while (pos <= n)
- {
- Tree[pos] = 0;
- pos += (pos & -pos);
- }
- }
-
- inline void divideConquer(int l, int r)
- {
- if (l >= r) return;
- int mid = (l + r) >> 1;
- divideConquer(l, mid);
- sort(p+l, p+mid+1);
- sort(p+mid+1, p+r+1);
- int i = l;
- int j = mid+1;
- int prefixMax = 0;
- while (j <= r)
- {
- while (i <= mid && p[i].x < p[j].x)
- {
- update(p[i].y, p[i].dp);
- ++i;
- }
- p[j].dp = max(p[j].dp, query(p[j].y - 1) + 1);
- ++j;
-
- }
- REP(i, l, mid) recover(p[i].y);
- sort(p+mid+1, p+r+1, [](node a, node b)
- {
- return a.id < b.id;
- });
- divideConquer(mid+1, r);
-
- }
-
- map <int, int> Map;
- int arr_x[maxn], arr_y[maxn];
-
- int main()
- {
- FI;
- n = read();
- FOR(i, n)
- {
- p[i].read();
- p[i].id = i;
- p[i].dp = 1;
- arr_x[i] = p[i].x;
- arr_y[i] = p[i].y;
- }
- sort(arr_x+1, arr_x+n+1);
- int ptr = 0;
- FOR(i, n) if (Map.find(arr_x[i]) == Map.end()) Map[ arr_x[i] ] = ++ptr;
- FOR(i, n) p[i].x = Map[ p[i].x ];
- sort(arr_y+1, arr_y+n+1);
- ptr = 0;
- Map.clear();
- FOR(i, n) if (Map.find(arr_y[i]) == Map.end()) Map[ arr_y[i] ] = ++ptr;
- FOR(i, n) p[i].y = Map[ p[i].y ];
-
- divideConquer(1, n);
-
- int ans = 0;
- FOR(i, n) ans = max(ans, p[i].dp);
- printf("%d\n", ans);
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.