Cod sursa(job #2792905)

Utilizator DragosC1Dragos DragosC1 Data 2 noiembrie 2021 14:27:45
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;

ifstream f;
ofstream g;

int n, T;

int aib[3501][3501];

struct tripla {
    int x, y, z;
};

tripla a[3502];
int dp[3501];

int range(int x, int y) {
    int i, j;
    int Max = 0;
    for (i = x; i > 0; i -= i & -i)
        for (j = y; j > 0; j -= j & -j) 
            Max = max(Max, aib[i][j]);
    return Max;
}

void upd(int x, int y, int val) {
    int i, j;
    for (i = x; i <= n; i += i & -i)
        for (j = y; j <= n; j += j & -j) 
            aib[i][j] = max(aib[i][j], val);
}

void upd2(int x, int y, int val) {
    int i, j;
    for (i = x; i <= n; i += i & -i)
        for (j = y; j <= n; j += j & -j) 
            aib[i][j] = 0;
}

bool csort(tripla a, tripla b) {
    if (a.x < b.x)
        return 1;
    else if (a.x == b.x && a.y < b.y)
        return 1;
    else if (a.x == b.x && a.y == b.y && a.z < b.z)
        return 1;
    return 0;
}

void solve() {
    int i, x, j, Max;
    for (i = 1; i <= n; i++)
        f >> a[i].x >> a[i].y >> a[i].z;
    sort(a + 1, a + n + 1, csort);
    for (i = 1; i <= n; i++)
        dp[i] = 0;
    for (i = 1, x = 0; i <= n; i++) {
        dp[i] = 1 + range(a[i].y - 1, a[i].z - 1);
        if (a[i].x == a[i + 1].x)
            x++;
        else {
            for (j = i - x; j <= i; j++) 
                upd(a[j].y, a[j].z, dp[j]);
            x = 0;
        }
    }
    for (i = 1; i <= n; i++)
        upd2(a[i].y, a[i].z, 0);
    Max = 0;
    for (i = 1; i <= n; i++)
        Max = max(Max, dp[i]);
    g << Max << '\n';
}   

void read() {
    f.open("cutii.in");
    g.open("cutii.out");
    f >> n >> T;
    while (T--)
        solve();
    f.close();
    g.close();
}

int main() {
    read();
    return 0;
}