Japan (线段树--求逆序对^坑^)

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

Input

The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.

Output

For each test case write one line on the standard output: 
Test case (case number): (number of crossings)

Sample Input

1
3 4 4
1 4
2 3
3 2
3 1

Sample Output

Test case 1: 5

题意: 左边n个点,右边m个点,点都是从上到下,点都是从1,2.....n;1,2......m,然后又k条线连接左右点,不会出现3条及3条以上的点相交于一个点的情况,问有多少个交点

思路:  如果Li<Lj,Ri>Rj,那么肯定有交点,k个点对按照左边的k个点从小到大排好序(如果左边的点相等,右边的点),然后求右边k个点的逆序对就是交点数

需要注意的地方:用long;k没告诉你多大,尽量开到很大;对java写的程序来说,如果输入很大,做好自己写输入挂的准备

代码:



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
class InputReader3
{
    BufferedReader buf;
    StringTokenizer tok;
    InputReader3()
    {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext()
    {
        while(tok == null || !tok.hasMoreElements()) 
        {
            try
            {
                tok = new StringTokenizer(buf.readLine());
            } 
            catch(Exception e) 
            {
                return false;
            }
        }
        return true;
    }
    String next()
    {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt()
    {
        return Integer.parseInt(next());
    }
}
class Japan{
	int x;
	int y;
}
class jcmp implements Comparator<Japan>{
	  @Override
	public int compare(Japan o1, Japan o2) {
		 if(o1.x>o2.x)    return 1;
		 else if(o1.x==o2.x){
			  if(o1.y>=o2.y)  return 1;
			  else return -1;
		 }
		 else return -1;
	}
}
public class Main {
	static int n,m,k;
	static final int max=1005;
	static long tree[]=new long[max<<2];
	static Japan japan[]=new Japan[max*max];
	public static void update(int node,int l,int r,int a,int b){
		 if(l==a&&r==b){
			 tree[node]+=1;
			 return;
		 }
		 int mid=(l+r)>>1;
	     if(b<=mid)  update(node<<1,l,mid,a,b);
	     else if(a>mid) update(node<<1|1,mid+1,r,a,b);
	     else{
	    	   update(node<<1,l,mid,a,b);
	    	   update(node<<1|1,mid+1,r,a,b);
	     }
	     tree[node]=tree[node<<1]+tree[node<<1|1];
	}
	public static long query(int node,int l,int r,int a,int b){
		 if(l>=a&&r<=b){
			 return tree[node];
		 }
		 int mid=(l+r)>>1;
		 if(b<=mid) return query(node<<1,l,mid,a,b);
		 else if(a>mid) return query(node<<1|1,mid+1,r,a,b);
		 else return query(node<<1,l,mid,a,b)+query(node<<1|1,mid+1,r,a,b);
	}
     public static void main(String[] args) {
    	 InputReader3 scan=new InputReader3();
		 int t=scan.nextInt();
		 int c=0;
		 while(t!=0){
			 Arrays.fill(tree, 0);
			 n=scan.nextInt();
			 m=scan.nextInt();
             k=scan.nextInt();
             for(int i=1;i<=k;i++){
            	 japan[i]=new Japan();
            	 japan[i].x=scan.nextInt();
            	 japan[i].y=scan.nextInt();
             }
			Arrays.sort(japan,1,k+1,new jcmp());
			long ans=0;
			 for(int i=1;i<=k;i++){
				 int a=japan[i].y;
				 update(1,1,m,a,a);
 				 ans+=(i-query(1,1,m,1,a));
			 }
			 System.out.print("Test case "+(++c)+": ");
			 System.out.println(ans);
			 t--;
		 }
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ksuper&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值