C++ Reference

## 余下的就是例题了

### [BZOJ3687]简单题

$f[i][j]=f[i-1][j]+f[i-1][j-a[i]]$

$Bitset=Bitset\ or\ (Bitset<<a[i])$

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <bitset>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;

int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
}

const int MAXN=1005;
const int MAXNUM=2000005;
int n,a[MAXN];
std::bitset<MAXNUM> bs;

int main(){
bs[0]=1;
int x;
rin(i,1,n){
bs^=(bs<<x);
}
int ans=0;
rin(i,0,2000000) if(bs[i])
ans^=i;
printf("%d\n",ans);
return 0;
}


### Shift-And&Shift-Or字符串匹配算法

Wikipedia

std::bitset<MAXN> now,sta[30];


Shift-And

rin(i,1,m) sta[t[i]-'a'].set(i-1);
rin(i,1,n){
now=((now<<1).set(0)&sta[s[i]-'a']);
if(now[m-1]) printf("%d\n",i-m+1);
}


Shift-Or

now.set();
memset(sta,0xff,sizeof sta);
rin(i,1,m) sta[t[i]-'a'].reset(i-1);
rin(i,1,n){
now=((now<<1)|sta[s[i]-'a']);
if(!now[m-1]) printf("%d\n",i-m+1);
}


### [BZOJ4939][Ynoi2016]掉进兔子洞

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <bitset>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;

int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
}

const int MAXN=100005;
const int SIZE=340;
const int T=25000;
int n,m,a[MAXN],b[MAXN];
int cnt,ans[T+5],has[MAXN];
std::bitset<MAXN> bs[T+5],now;
struct Qu{
int l,r,lk,id;
inline friend bool operator < (Qu x,Qu y){
if(x.lk!=y.lk) return x.lk<y.lk;
if(x.lk&1) return x.r<y.r;
return x.r>y.r;
}
}q[T*3+5];

inline void mo(){
memset(has,0,sizeof has);
memset(bs,0xff,sizeof bs);
std::sort(q+1,q+cnt+1);
int nowl=1,nowr=0;
now.reset();
rin(i,1,cnt){
while(nowl>q[i].l){
nowl--;
now.set(a[nowl]+has[a[nowl]]);
has[a[nowl]]++;
}
while(nowr<q[i].r){
nowr++;
now.set(a[nowr]+has[a[nowr]]);
has[a[nowr]]++;
}
while(nowl<q[i].l){
has[a[nowl]]--;
now.reset(a[nowl]+has[a[nowl]]);
nowl++;
}
while(nowr>q[i].r){
has[a[nowr]]--;
now.reset(a[nowr]+has[a[nowr]]);
nowr--;
}
bs[q[i].id]&=now;
}
cnt/=3;
rin(i,1,cnt){
printf("%d\n",ans[i]-bs[i].count()*3);
}
}

int main(){
rin(i,1,n){
}
std::sort(b+1,b+n+1);
rin(i,1,n){
a[i]=std::lower_bound(b+1,b+n+1,a[i])-b;
}
rin(i,1,m){
q[cnt].lk=(q[cnt].l-1)/SIZE+1;
q[cnt].id=(i-1)%T+1;
ans[(i-1)%T+1]+=q[cnt].r-q[cnt].l+1;
q[cnt].lk=(q[cnt].l-1)/SIZE+1;
q[cnt].id=(i-1)%T+1;
ans[(i-1)%T+1]+=q[cnt].r-q[cnt].l+1;
q[cnt].lk=(q[cnt].l-1)/SIZE+1;
q[cnt].id=(i-1)%T+1;
ans[(i-1)%T+1]+=q[cnt].r-q[cnt].l+1;
if(cnt==T*3){
mo();
cnt=0;
memset(ans,0,sizeof ans);
}
}
if(cnt) mo();
return 0;
}


### [BZOJ4810][Ynoi2017]由乃的玉米田

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <bitset>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;

int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
}

const int MAXN=100005;
const int SIZE=340;
int n,m,a[MAXN],cnt[MAXN];
bool ans[MAXN];
std::bitset<MAXN> f,g;
struct Qu{
int opt,l,r,x,lk,id;
inline friend bool operator < (Qu x,Qu y){
if(x.lk!=y.lk) return x.lk<y.lk;
if(x.lk&1) return x.r<y.r;
return x.r>y.r;
}
}q[MAXN];

int main(){
rin(i,1,n){
}
rin(i,1,m){
q[i].lk=(q[i].l-1)/SIZE+1;
q[i].id=i;
}
std::sort(q+1,q+n+1);
int l=1,r=0;
rin(i,1,m){
while(l>q[i].l){
l--;
cnt[a[l]]++;
f.set(a[l]);
g.set(MAXN-5-a[l]);
}
while(r<q[i].r){
r++;
cnt[a[r]]++;
f.set(a[r]);
g.set(MAXN-5-a[r]);
}
while(l<q[i].l){
cnt[a[l]]--;
if(!cnt[a[l]]){
f.reset(a[l]);
g.reset(MAXN-5-a[l]);
}
l++;
}
while(r>q[i].r){
cnt[a[r]]--;
if(!cnt[a[r]]){
f.reset(a[r]);
g.reset(MAXN-5-a[r]);
}
r--;
}
if(q[i].opt==1){
if((f&(f>>q[i].x)).any()) ans[q[i].id]=1;
}
else if(q[i].opt==2){
if((f&(g>>(MAXN-5-q[i].x))).any()) ans[q[i].id]=1;
}
else{
int lim=sqrt(q[i].x);
rin(j,1,lim){
if(q[i].x%j) continue;
if(f[j]&&f[q[i].x/j]){
ans[q[i].id]=1;
break;
}
}
}
}
rin(i,1,m){
if(ans[i]) printf("yuno\n");
else printf("yumi\n");
}
return 0;
}


### Extra. 三维偏序

Update on 2018/12/7：好吧，这里为了卡空间要套个分块，但是在洛咕上会TLE，极限数据$2.5s+$。感谢UOJ群的各位和yyb，放个yyb博客的链接在这：链接，yyb orz。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <bitset>
#define rin(i,a,b) for(register int i=(a);i<=(b);i++)
#define rec(i,a,b) for(register int i=(a);i>=(b);i--)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;

int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}

const int MAXN=100005;
const int SIZE=335;
const int BLOCK=(MAXN>>5)+5;
int n,k,dat[3][MAXN],blg[MAXN],rez[MAXN];
int Table[65536];

inline void init(){
rin(i,0,65535) Table[i]=Table[i>>1]+(i&1);
}

inline int query(unsigned int x){
return Table[x>>16]+Table[x&65535];
}

struct Bitset{
unsigned int Element[BLOCK],siz;
Bitset(){
siz=0;
}
inline friend void operator &= (Bitset &x,Bitset y){
x.siz=0;
rin(i,0,BLOCK-1) x.Element[i]&=y.Element[i],x.siz+=query(x.Element[i]);
}
inline void set(){
siz=n;
memset(Element,0xff,sizeof Element);
}
inline void set(int x){
int nth=(x>>5),pos=x-(nth<<5);
Element[nth]|=(1u<<pos);
siz++;
}
};
Bitset now,ret,bs[3][MAXN/SIZE+5];

struct Pair{
int x,id;
inline friend bool operator < (Pair x,Pair y){
return x.x<y.x;
}
}a[3][MAXN];

inline int L(int x){
return (x-1)*SIZE+1;
}

inline int R(int x){
return x*SIZE;
}

inline int find(int d,int x){
int l=1,r=n,retint;
while(l<=r){
int mid=((l+r)>>1);
if(a[d][mid].x<=x) retint=mid,l=mid+1;
else r=mid-1;
}
return retint;
}

inline void getset(int d,int x){
int pos=find(d,x);
ret=bs[d][blg[pos]-1];
rin(i,L(blg[pos]),pos) ret.set(a[d][i].id);
}

int main(){
init();
rin(i,1,n){
blg[i]=(i-1)/SIZE+1;
}
rin(d,0,2){
std::sort(a[d]+1,a[d]+n+1);
rin(i,1,blg[n]){
bs[d][i]=bs[d][i-1];
rin(j,L(i),R(i)) bs[d][i].set(a[d][j].id);
}
}
rin(i,1,n){
now.set();
rin(d,0,2) now&=(getset(d,dat[d][i]),ret);
rez[now.siz-1]++;
}
rin(i,0,n-1) printf("%d\n",rez[i]);
return 0;
}


posted on 2018-12-06 15:56  ErkkiErkko  阅读(123)  评论(0编辑  收藏

• 随笔 - 91
• 文章 - 0
• 评论 - 20