您的位置:首页 > 其它

4418: [Shoi2013]扇形面积并|二分答案|树状数组

2016-03-23 11:08 441 查看
为何感觉SHOI的题好水。。。又是一道SB题

从左到右枚举每一个区间,遇到一个扇形的左区间就+1,遇到右区间就-1,然后再树状数组上2分答案,还是不会码log的。。SHOI2013似乎还有一道题发牌也是类似的维护方法。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define mod 999911659 //2 3 4679 35617
#define N 500001
using namespace std;
int sc()
{
int i=0,f=1; char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar();
return i*f;
}
struct W{int p,r,x;}a
;
long long ans;
int n,tot,mx,k,m,tr
;
bool cmp(W a,W b)
{
return a.p<b.p;
}
void change(int x,int f)
{
for(;x<=mx;x+=x&-x)tr[x]+=f;
}
ll ask(int x)
{
ll ans=0;
for(;x;x-=x&-x)ans+=tr[x];
return ans;
}
int query()
{
int l=1,r=mx,ans=0;
while(l<=r)
{
int mid=l+r>>1;
if(ask(mid)>=k)ans=mid,l=mid+1;
else r=mid-1;
}
return ans;
}
int main()
{
n=sc();m=sc(),k=sc();
for(int i=1;i<=n;i++)
{
int x=sc(),l=sc(),r=sc();mx=max(mx,x);
a[++tot].p=l,a[tot].r=x,a[tot].x=1;
a[++tot].p=r,a[tot].r=x,a[tot].x=-1;
if(l>r)
a[++tot].p=-m,a[tot].r=x,a[tot].x=1;
}
sort(a+1,a+tot+1,cmp);
int now=1;
for(int i=-m;i<m;i++)
{
while(now<=tot&&a[now].p<=i)
change(1,a[now].x),change(a[now].r+1,-a[now].x),now++;
ll res=query();
ans+=res*res;
}
cout<<ans;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息