poj 3067 japan 樹狀數組

Poj3067 japan 這裏求的是高架橋交叉的個數,首先按照規則排序,然後同上處理

               這裏要注意的是 輸入輸出要用scanf 且要用sum要用__int64 

#include<iostream>
#include<algorithm>
#include<cstring>
#define maxn 2005
#define MAX 1000010
using namespace std;
struct node
{
  int east,west;
};
int tree[maxn/2];
node input[MAX];
int cmp(node x1,node x2)
{
    if(x1.east!=x2.east)
     return x1.east<x2.east;
    else
     return x1.west<x2.west;
}
void update(int x,int val)
{
  while(x<=1001)
  {
    tree[x]+=val;
    x+=(x&(-x));
  };
}
long long getsum(int x)
{
    long long sum=0;
    while(x>0)
    {
      sum+=tree[x];
      x-=(x&-x);
    }
    return sum;
}
int main()
{
    int nCase;
    int N,M,K;
    int T=1;
    cin>>nCase;
    while(nCase--)
    {
      //cin>>N>>M>>K;
      scanf("%d%d%d",&N,&M,&K);
      memset(tree,0,sizeof(tree));
      for(int i=1;i<=K;i++)
       //cin>>input[i].east>>input[i].west;
       scanf("%d%d",&input[i].east,&input[i].west);
      sort(input+1,input+K+1,cmp);
      long long sum=0;
      for(int i=1;i<=K;i++)
      {
          update(input[i].west,1);
          sum+=i-getsum(input[i].west);
      }
      //cout<<"Test case "<<T<<": "<<sum<<endl;
      printf("Test case %d: %I64d\n",T,sum);         //這裏居然必須要用__int64這種,也就是說必須要用這種格式輸出,太bt了
      T++;
    }
    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章