A Sequence of Numbers

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case %d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef unsigned long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
#define MAXN (100000+10)
ll a[MAXN],p2[30];
int t[16];
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
int n;
int kc=1;
p2[0]=1;
For(i,20) p2[i]=p2[i-1]*2;
while(scanf("%d",&n)) {
if (n==-1) return 0;
For(i,n) scanf("%d",&a[i]);
memset(t,-1,sizeof(t));
char s[2]; long long ans=0;
int p=0,c=0;
while(scanf("%s",s)) {
if (s[0]=='Q') {
if (c) {
For(i,n) a[i]+=c;
c=0;
memset(t,-1,sizeof(t));
}
scanf("%d",&p);
if (t[p]==-1) {
t[p]=0;
For(i,n) t[p]+=(a[i]>>p)&1;
}
ans+=t[p];

} else if (s[0]=='C'){
int c2;
scanf("%d",&c2);
c+=c2;
} else break;
}
Pr(kc++,ans);
}


return 0;
}

David Shopping

手写堆

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int n,m;
int h[1<<20];
struct node {
int L,i,id;
node(int _L,int _i,int _id):L(_L),i(_i),id(_id){}
node(){}
friend bool operator<(node a,node b) {
if (a.L^b.L) return a.L>b.L;
return a.i<b.i;
}
friend bool operator>(node a,node b) {
return !(a<b);
}
};
#define MAXN (300000+10)
struct Heap{
node a[MAXN];
int siz;
void mem(){
siz=0;
}
void Swap(int i,int j) {
swap(h[a[i].id],h[a[j].id]);
swap(a[i],a[j]);
}
void add(node x) {
a[++siz]=x; h[x.id]=siz;
int p=siz;
while(p>1) {
if (a[p/2]>a[p]) {
Swap(p/2,p);
p/=2;
} else break;
}
}
void del(int p) {
Swap(p,siz);
h[a[siz].id]=0;
a[siz--]=node(0,0,0);
while(p*2<=siz) {
int c=p;
if (a[c]>a[p*2]) c=p*2;
if (p*2+1<=siz&&a[c]>a[p*2+1]) c=p*2+1;
if (c==p) break;
Swap(c,p);
p=c;
}

while(p>1) {
if (a[p/2]>a[p]) {
Swap(p/2,p);
p/=2;
} else break;
}
}
}S;
void work() {
S.mem();
int ans=0;
For(i,m) {
int p=read();
node now;
if (h[p]) {
now=S.a[h[p]];
now.L++;
S.del(h[p]);
S.add(now);
} else {
now=node(1,i,p);
if (S.siz==n) S.del(1),++ans;
S.add(now);
}
}
printf("%d\n",ans);
}
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
int kcase=1;
while(cin>>n>>m) {
if (!n&&!m) return 0;
MEM(h)
printf("Case %d: ",kcase++);
work();
}


return 0;
}