唔~~~咋一看之下还以为要开二维数组,不过倒是看得出这题要么用树状数组要么用线段树(居然可以用表情!!!)咳咳。。。区间问题嘛~后来想了想,似乎不是这么回事,在脑子里想象一下,发现这题可以转换成一维树状数组求逆序数~
先对n从小到大排序,如果相等,则对m进行从小到大排序,唔,对道路们排序不影响结果这是必然的,然后对m求逆序数(画出来就知道,其实这样就能很清晰直观的用焦点个数来判断了~(j>i)mj<mi的话这两条道路必有交点,以此类推)~~就TLE了!!!!~~~~哎~~不知道有木有大神有更快的方法~~懒得想,用了快速io~~wa~~~~~~~~才发现最后的结果是long型的好烦,总是不会去考虑数据类型,等着wa了才会去找~~~眼拙~~在题目里没找着k的范围~~~!
AC代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;
class way{int x,y;}
public class Main
{
static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt()throws IOException {in.nextToken();return (int)in.nval;}
private static int lowbit(int x) {return x&(-x);}
private static void upDate(int[] c,int n,int x,int add)
{
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=add;
}
private static int sum(int[] c,int x)
{
int sum=0;
for(int i=x;i>0;i-=lowbit(i))
sum+=c[i];
return sum;
}
public static void main(String[] args)throws IOException
{
int t=nextInt();
for(int i=1;i<=t;i++)
{
int n=nextInt();
int m=nextInt();
int k=nextInt();
way xy[]=new way[k+1];
for(int j=1;j<=k;j++)
{
xy[j]=new way();
xy[j].x=nextInt();
xy[j].y=nextInt();
}
/*if(n==0 && m==0)
{
out.println("Test case "+i+": "+0);
continue;
}*/
Arrays.sort(xy,1,k+1,new Comparator<way>() {
@Override
public int compare(way a, way b)
{
if(a.x!=b.x)
return (a.x>b.x)?1:-1;
return (a.y>b.y)?1:-1;
}
});
int max=n>=m?n:m;
int c[]=new int[max+1];
long sum=0;
for(int j=1;j<=k;j++)
{
sum+=j-1-sum(c,xy[j].y);
upDate(c,max,xy[j].y,1);
}
out.println("Test case "+i+": "+sum);
}
out.flush();
}
}