Cod sursa(job #1428070)

Utilizator danstefanDamian Dan Stefan danstefan Data 3 mai 2015 17:23:55
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<fstream>
#include<algorithm>
#include<cstring>
using namespace std;
int n, t, i, ma;
int d[3502], a[3502][3502];
struct cutie{
int x;
int y;
int z;};
cutie v[3502];
int query(int x, int y){
    int ma=0;
    for(int i = x; i >= 1; i -= (i & -i))
            for(int j = y; j >= 1; j -= (j & -j))
                    ma = max(ma, a[i][j]);
return ma;}
void update(int x, int y, int val){
    for(int i = x; i <= n; i += (i & -i))
            for(int j = y; j <= n; j += (j & -j))
                a[i][j] = max(a[i][j], val);}
void cc_cleaner(int x, int y){
    for(int i = x; i <= n; i += (i & -i)){
            for(int j = y; j <= n; j += (j & -j)){
    a[i][j] = 0;}}}
int cmp(cutie a, cutie b){
    return a.z < b.z;
}
int main(){
    freopen("cutii.in","r",stdin);
    ofstream g ("cutii.out");
   scanf("%d%d",&n,&t);
    for(; t; t--){
            for(i = 1; i <= n; i++)scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z);
sort(v + 1, v + n + 1, cmp);
d[1] = 1;
ma= 1;
update(v[1].x, v[1].y, 1);
for(i = 2; i <= n; i++){
d[i] = query(v[i].x - 1, v[i].y - 1) + 1;
update(v[i].x, v[i].y, d[i]);
ma= max(ma, d[i]);}
g<< ma <<'\n';
for(i = 1; i <= n; i++)
cc_cleaner(v[i].x,v[i].y);}
return 0;
}