Cod sursa(job #257480)

Utilizator hadesgamesTache Alexandru hadesgames Data 13 februarie 2009 13:37:15
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.13 kb
/*
http://www.youtube.com/watch?v=PCiyb2xvAh8
*/
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <algorithm>
#include <utility>
#include <string>
#include <functional>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
#define FOR(i,a,b) for (i=a;i<=b;i++)
#define fori(it,v) for (it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fs first
#define ss second.first
#define sss second.second
#define all(c) c.begin(),c.end()
#define pf push_front
#define popb populack
#define popf pop_front
FILE *in,*out;
int arb[3*3505][3*3505],n,b[4000];;
pair<int,pair<int,int> > a[4000];
int query(int nr,int nod,int a,int b,int x,int y)
{
	if (x<=a&&b<=y)
		return arb[nr][nod];
	int m=(a+b)/2;
	int maxv=-1;
	if (x<=m)
		maxv=max(maxv,query(nr,nod*2,a,m,x,y));
	if (y>m)
		maxv=max(maxv,query(nr,nod*2+1,m+1,b,x,y));
	return maxv;
}
int search(int nod,int a,int b,int x,int y,int z,int t)
{
//	printf("%d %d %d %d\n",a,b,x,y);
	if (x<=a&&b<=y)
		return query(nod,1,0,n,z,t);
	int m=(a+b)/2;
	int maxv=-1;
	if (x<=m)
		maxv=max(maxv,search(nod*2,a,m,x,y,z,t));
	if (y>m)
		maxv=max(maxv,search(nod*2+1,m+1,b,x,y,z,t));
	return maxv;
}
void update(int nr,int nod,int a,int b,int x,int v)
{
	if (a==b&&b==x)
		arb[nr][nod]=v;
	int m=(a+b)/2;
	if (x<=m)
		update(nr,nod*2,a,m,x,v);
	else
		update(nr,nod*2+1,m+1,b,x,v);
	arb[nr][nod]=max(arb[nr][nod*2],arb[nr][nod*2+1]);
}
void push(int nod,int a,int b,int x,int p2,int v)
{
	if (a==b&&b==x)
		update(nod,1,0,n,p2,v);
	int m=(a+b)/2;
	if (x<=m)
		push(nod*2,a,m,x,p2,v);
	else
		push(nod*2+1,m+1,b,x,p2,v);
	update(nod,1,0,n,p2,v);
}
int main ()
{
	int i,t,rez;
	in=fopen("cutii.in","r");
	out=fopen("cutii.out","w");
	fscanf(in,"%d%d",&n,&t);
	rez=-1;
	for(;t;t--)
	{
		FOR(i,1,n)
		{
			fscanf(in,"%d%d%d",&a[i].fs,&a[i].ss,&a[i].sss);
		}
		sort(a+1,a+n+1);
		FOR(i,1,n)
		{
			b[i]=1+search(1,0,n,1,a[i].ss-1,0,a[i].sss-1);
			rez=max(rez,b[i]);
			push(1,1,n,a[i].ss,a[i].sss,b[i]);
		}	
		fprintf(out,"%d\n",rez);
	}
}