[整体二分] Codechef January Challenge 2018 #MONSTER Killing Monsters

版权声明:本文为博主原创文章,未经博主允许随意转载。 https://blog.csdn.net/u014609452/article/details/79106152

整体二分,然后问题变成,子集加,单点查询,然后像CTSC吉夫特 可以用经典的二进制分高位低位的搞搞。调个参,大概是高5位低12位。
不知道在线怎么做。

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define pb push_back
using namespace std;
typedef long long ll;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()) if (c=='-') b=-1;
}
inline void read(ll &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()) if (c=='-') b=-1;
}

const int N=500005;

int n,m;
ll h[N];
int x[N],y[N];

int tim[N],cc[N];

const int S=12;
const int ss=(1<<S)-1;
ll a[N];
inline void Add(int x,int y){
  int x0=x>>S,x1=x&ss;
  for (int i=x0;;i=(i-1)&x0){
    a[(i<<S)|x1]+=y;
    if (i==0) break;
  }
}
inline void Dec(int x,int y){
  int x0=x>>S,x1=x&ss;
  for (int i=x0;;i=(i-1)&x0){
    a[(i<<S)|x1]-=y;
    if (i==0) break;
  }
}
inline bool Query(int x,ll y){
  int x0=x>>S,x1=x&ss;
  for (int i=x1^ss;;i=(i-1)&(x1^ss)){
    y-=a[x0<<S|(i^ss)];
    if (y<=0) return 0;
    if (i==0) break;
  }
  return 1;
}

inline void Solve(int l,int r,vector<int> &v){
  if (l==r){
    for (int i:v) tim[i]=l;
    return;
  }
  vector<int> lv,rv;
  int mid=(l+r)>>1;
  for (int i=l;i<=mid;i++)
    Add(x[i],y[i]);
  for (int i:v)
    if (Query(i,h[i]))
      rv.pb(i);
    else
      lv.pb(i);
  Solve(mid+1,r,rv);
  for (int i=l;i<=mid;i++)
    Dec(x[i],y[i]);
  Solve(l,mid,lv);
}

int main(){
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  read(n); for (int i=0;i<n;i++) read(h[i]);
  read(m); for (int i=1;i<=m;i++) read(x[i]),read(y[i]),x[i]&=(1<<17)-1;
  vector<int> v; for (int i=0;i<n;i++) v.pb(i);
  Solve(1,m+1,v);
  for (int i=0;i<n;i++) cc[tim[i]]++;
  int ret=n;
  for (int i=1;i<=m;i++)
    printf("%d\n",ret-=cc[i]);
  return 0;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页