大大的四个字:数据结构!!!
Description
给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,对于每一对i∈[1,n],j∈[1,n]且i!=j,求满足ai<aj<bi<bj的对数.
Solution
这道题我们可以转换一下题意:如果比赛时可以想出来这个我一定能AC 有n条线段,给出每条线段的左端点和右端点,求有多少对线段重合又不是包含被包含的关系。
这样就可以想出来一个很好的思路。
首先,我们给左端点拍个序。然后我们扫一遍每一条线段,把右端点插入树状数组里,然后查找有多少个右端点在这条线段的这个区间内并加入答案。
Code
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
int n,ans=0,tree[100001];
struct node {
int a,b;
}a[50001];
bool cmp(node x,node y) {
return x.a<y.a;
}
int lowbit(int k) {
return k&(-k);
}
int query(int x) {
int sum=0;
while(x>0) {
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
void update(int x) {
while(x<=n*2) {
tree[x]++;
x+=lowbit(x);
}
}
int main() {
scanf("%d",&n);
for(int i=1;i<=(n<<1);i++) {
int x;
scanf("%d",&x);
if(!a[x].a)a[x].a=i;
else a[x].b=i;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++) {
ans+=query(a[i].b-1)-query(a[i].a);
update(a[i].b);
}
printf("%d",ans);
}