Cod sursa(job #3319913)

Utilizator amaliasasuAmalia Sasu amaliasasu Data 3 noiembrie 2025 19:13:34
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
 
ifstream fin("cutii.in");
ofstream fout("cutii.out");
 
struct Cutie {
    int x, y, z;
    bool operator < (const Cutie &c) const 
    {
        return z < c.z;
    }
};
 
const int MAX_N = 3500;
int aib[MAX_N + 10][MAX_N + 10];  
                                  
 
void Update(int cx, int cy, int val)  
{                                     
    for (int x = cx; x <= MAX_N; x += x & -x) 
		for (int y = cy; y <= MAX_N; y += y & -y) 
            aib[x][y] = max(aib[x][y], val);
}
 
int Query(int cx, int cy)   // Query(x, y) - nr max de cutii cu _x < x si _y < y 
{                           // care pot fi incuibate una in alta (nr max de puncte in scara)
    int ans = 0;
    for (int x = cx; x > 0; x -= x & -x) 
        for (int y = cy; y > 0; y -= y & -y) 
            ans = max(ans, aib[x][y]);
    return ans;
}
 
void Reset(int cx, int cy) 
{
    for (int x = cx; x <= MAX_N; x += x & -x) 
        for (int y = cy; y <= MAX_N; y += y & -y) 
            aib[x][y] = 0;
}
 
int Solve(int n) // O(n * log^2(n))
{
    vector<Cutie> C;
    int x, y, z;
 
    for (int i = 1; i <= n; i++) 
    {
        fin >> x >> y >> z;
        C.emplace_back(x, y, z);
    }
    sort(C.begin(), C.end()); // O(n * log(n)) sortare dupa z
     
    int ans = 0;
    for (const auto& c : C)  // O(n * log(n) * log(n))
    {
        int d = Query(c.x - 1, c.y - 1) + 1;
        Update(c.x, c.y, d);
        ans = max(ans, d);
    }
     
    // foarte important!
    for (const auto& c : C)  // O(n * log2(n) * log2(n))
        Reset(c.x, c.y);
 
    return ans;
}
 
int main() 
{
    int n, t;
    fin >> n >> t; 
     
    for(int i = 1; i <= t; i++) 
        fout << Solve(n) << '\n';

    return 0;
}