Advertisement
trungore10

Untitled

Oct 9th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void Read(int &val) {
  5.     val = 0; char c;
  6.     do { c = getchar(); } while (!isdigit(c));
  7.     while (isdigit(c)) { val = val * 10 + c - '0'; c = getchar(); }
  8. }
  9.  
  10. struct data {
  11.     int X, Y, color;
  12.     data() {}; data(int X, int Y, int color) : X(X), Y(Y), color(color) {};
  13.     bool operator < (const data &a) const {
  14.         return Y < a.Y || (Y == a.Y && X < a.X);
  15.     }
  16. };
  17.  
  18. const int N = 1e5 + 4, oo = 1e9 + 7;
  19. int T, n, k;
  20. data a[N];
  21.  
  22. vector<int> rar;
  23.  
  24. void compress() {
  25.     rar.clear();
  26.     for (int i = 1; i <= n; ++i) rar.push_back(a[i].X);
  27.     sort(rar.begin(), rar.end());
  28.     rar.resize(unique(rar.begin(), rar.end()) - rar.begin());
  29.     for (int i = 1; i <= n; ++i) a[i].X = lower_bound(rar.begin(), rar.end(), a[i].X) - rar.begin() + 1;
  30.  
  31.     rar.clear();
  32.     for (int i = 1; i <= n; ++i) rar.push_back(a[i].Y);
  33.     sort(rar.begin(), rar.end());
  34.     rar.resize(unique(rar.begin(), rar.end()) - rar.begin());
  35.     for (int i = 1; i <= n; ++i) a[i].Y = lower_bound(rar.begin(), rar.end(), a[i].Y) - rar.begin() + 1;
  36. }
  37.  
  38. set<int> Set[N];
  39.  
  40. struct Binary_Indexed_Tree {
  41.     int node[N];
  42.  
  43.     void clear(int n) { for (int i = 0; i <= n; ++i) node[i] = 0; }
  44.  
  45.     int get(int pos) {
  46.         int ans = 0;
  47.         for (int i = pos; i > 0; i -= i & (-i)) ans += node[i];
  48.         return ans;
  49.     }
  50.  
  51.     void update(int pos, int val, int n) {
  52.         for (int i = pos; i <= n && i > 0; i += i & (-i)) node[i] += val;
  53.     }
  54. } BIT;
  55.  
  56. int res;
  57.  
  58. void process() {
  59.     sort(a+1, a+n+1); res = 0;
  60.  
  61.     for (int i = 1; i <= k; ++i) {
  62.         Set[i].clear();
  63.         Set[i].insert(0); Set[i].insert(n+1);
  64.     }
  65.     BIT.clear(n);
  66.  
  67.     for (int l = 1; l <= n; ++l) {
  68.         int r = l;
  69.         while (r <= n && a[r].Y == a[l].Y) ++r; --r;
  70.  
  71.         for (int i = l; i <= r; ++i) {
  72.             int col = a[i].color;
  73.  
  74.             auto itR = Set[col].lower_bound(a[i].X);
  75.             auto itL = Set[col].upper_bound(a[i].Y); itL--;
  76.  
  77.             int L = (*itL), R = (*itR);
  78.             if (L == a[i].X || R == a[i].X) continue;
  79.             res = max( res, BIT.get(R-1) - BIT.get(L) );
  80.         }
  81.  
  82.         for (int i = l; i <= r; ++i) {
  83.             BIT.update(a[i].X, 1, n);
  84.             Set[a[i].color].insert(a[i].X);
  85.         }
  86.  
  87.         l = r;
  88.     }
  89. }
  90.  
  91. bool cmp(data a, data b) { return a.X < b.X || (a.X == b.X && a.Y < b.Y); }
  92.  
  93. int save[N];
  94.  
  95. void Special() {
  96.     sort(a+1, a+n+1, cmp);
  97.  
  98.     for (int i = 1; i <= k; ++i) save[i] = 0;
  99.  
  100.     for (int i = 1; i <= n; ++i) { 
  101.         int Prev = save[a[i].color];
  102.  
  103.         int tmp = BIT.get(a[i].X-1) - BIT.get(a[Prev].X);
  104.         res = max(res, tmp);
  105.  
  106.         save[a[i].color] = i;
  107.     }
  108.  
  109.     for (int i = 1; i <= k; ++i) {
  110.         int Prev = save[i];
  111.  
  112.         int tmp = BIT.get(n) - BIT.get(a[Prev].X);
  113.         res = max(res, tmp);
  114.     }
  115. }
  116.  
  117. bool vis[N];
  118.  
  119. void sol() {
  120.     compress();
  121.  
  122.         for (int i = 1; i <= k; ++i) vis[i] = false;
  123.         for (int i = 1; i <= n; ++i) vis[a[i].color] = true;
  124.         for (int i = 1; i <= k; ++i) if (!vis[i]) { cout << n << '\n'; return; }
  125.  
  126.     process();
  127.  
  128.     Special();
  129.  
  130.     cout << res << '\n';
  131. }
  132.  
  133. int main() {
  134.     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  135.     freopen("heist.inp", "r", stdin);
  136.     freopen("heist.out", "w", stdout);
  137.  
  138.     Read(T);
  139.     while (T--) {
  140.         Read(n); Read(k);
  141.         for (int i = 1; i <= n; ++i) {
  142.             Read(a[i].X); Read(a[i].Y); Read(a[i].color);
  143.         }
  144.  
  145.         sol();
  146.     }
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement