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

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

#include
#include
#include
#include
#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<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<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<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<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;ifor (int i=1;i<=m;i++) read(x[i]),read(y[i]),x[i]&=(1<<17)-1;
  vector<int> v; for (int i=0;i1,m+1,v);
  for (int i=0;iint ret=n;
  for (int i=1;i<=m;i++)
    printf("%d\n",ret-=cc[i]);
  return 0;
}

你可能感兴趣的:(整体二分)