您的位置:首页 > 其它

【bzoj3289】Mato的文件管理 莫队+树状数组

2016-11-17 14:41 246 查看
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3289

【题解】

用树状数组维护逆序对,然后对于询问用莫队就行了,这题还得离散化来搞。

/*************
bzoj 3289
by chty
2016.11.17
*************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define FILE "read"
#define MAXN 50010
#define up(i,j,n) for(ll i=j;i<=n;i++)
struct node{ll x,y,id;}q[MAXN];
ll n,m,now,tot,block,a[MAXN],ans[MAXN];
pair<ll,ll>b[MAXN];
namespace Init{
char buf[1<<15],*fs,*ft;
inline char getc() {return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;}
inline ll read(){
ll x=0,f=1; char ch=getc();
while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getc();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getc();}
return x*f;
}
}
namespace Tree_Array{
ll c[MAXN];
ll lowbit(ll x) {return x&(-x);}
void updata(ll x,ll v){for(ll i=x;i<=tot;i+=lowbit(i)) c[i]+=v;}
ll query(ll x){ll sum=0;for(ll i=x;i;i-=lowbit(i)) sum+=c[i]; return sum;}
}
namespace Mo_Team{
bool cmp(node a,node b) {return (a.x/block==b.x/block)?a.y<b.y:(a.x/block)<(b.x/block);}
void work(){
block=(ll)sqrt(n*1.0);
sort(q+1,q+m+1,cmp);
ll l(1),r(0);
up(i,1,m){
while(q[i].y>r) now+=Tree_Array::query(tot)-Tree_Array::query(a[++r]),Tree_Array::updata(a[r],1);
while(q[i].y<r) Tree_Array::updata(a[r],-1),now-=Tree_Array::query(tot)-Tree_Array::query(a[r--]);
while(q[i].x>l) Tree_Array::updata(a[l],-1),now-=Tree_Array::query(a[l++]-1);
while(q[i].x<l) now+=Tree_Array::query(a[--l]-1),Tree_Array::updata(a[l],1);
ans[q[i].id]=now;
}
up(i,1,m) printf("%lld\n",ans[i]);
}
}
void init(){
n=Init::read();
up(i,1,n) b[i].first=Init::read(),b[i].second=i;
m=Init::read();
up(i,1,m) q[i].x=Init::read(),q[i].y=Init::read(),q[i].id=i;
sort(b+1,b+n+1);
up(i,1,n){
if(i==1||b[i].first!=b[i-1].first) tot++;
a[b[i].second]=tot;
}
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
init();
Mo_Team::work();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: