bzoj3262: 陌上花开

cdq分治+树状数组【oyzx神犇的博客转载，我不想打了。。。。。。】

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 100005
using namespace std;
int n,m,tot,cnt;
int ans[N];
struct data{
int a,b,c,s,ans;
}q[N],a[N];
{
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
bool cmp(data a,data b)
{
if (a.a==b.a && a.b==b.b) return a.c<b.c;
if (a.a==b.a) return a.b<b.b;
return a.a<b.a;
}
bool cmp2(data a,data b)
{
if (a.b==b.b) return a.c<b.c;
return a.b<b.b;
}
struct Tbit{
int t[N*2];
void modify(int x,int v){for (int p=x;p<=m;p+=p&-p) t[p]+=v;}
int query(int x){int res=0;for (int p=x;p;p-=p&-p) res+=t[p];return res;}
}T;
void solve(int l,int r)
{
if (l==r) return;
int mid=(l+r)>>1;
solve(l,mid); solve(mid+1,r);
sort(q+l,q+mid+1,cmp2);
sort(q+mid+1,q+r+1,cmp2);
int i=l,j=mid+1,last=0;
while (j<=r)
{
while (i<=mid && q[i].b<=q[j].b)
{
T.modify(q[i].c,q[i].s);
i++;
}
q[j].ans+=T.query(q[j].c);
j++;
}
for (int j=l; j<i; j++)  T.modify(q[j].c,-q[j].s);
}
int main()
{
sort(a+1,a+1+n,cmp);
cnt=0;
for (int i=1; i<=n; i++)
{
cnt++;
if (a[i].a!=a[i+1].a || a[i].b!=a[i+1].b || a[i].c!=a[i+1].c)
{
q[++tot]=a[i]; q[tot].s=cnt; cnt=0;
}
}
solve(1,tot);
for (int i=1; i<=tot; i++) ans[q[i].ans+q[i].s-1]+=q[i].s;
for (int i=0; i<n; i++) printf("%d\n",ans[i]);
}
View Code

posted @ 2016-05-29 20:40  ACist  阅读(130)  评论(0编辑  收藏  举报