ccpc 2019网络选拔赛
先存一下几个较难题的AC代码:
1002:
#include<bits/stdc++.h> using namespace std; #define fi first #define se second const int maxn=15000000; const int N=110000; int T; int n,m; int sum[maxn],pos,a[N]; struct node { int ll,rr; }tr[maxn]; pair<int,int> b[N]; map<int,int> idx; set<int> del_nums; set<int>::iterator it; int f[N]; inline int lowbit(int x) { return x&-x; } int root1[N],root2[N],pp[N]; int copy(int x) { ++pos; sum[pos]=sum[x]; tr[pos].ll=tr[x].ll; tr[pos].rr=tr[x].rr; return pos; } void add(int k,int l,int r,int x) { sum[k]++; if (l==r) return; int mid=(l+r)/2; if (x<=mid) { tr[k].ll=copy(tr[k].ll); add(tr[k].ll,l,mid,x); } else { tr[k].rr=copy(tr[k].rr); add(tr[k].rr,mid+1,r,x); } } int ask(int k,int l,int r,int x) { if (sum[k]==0) return n+1; if (l==r) return l; int mid=(l+r)/2; if (l>=x) { if (sum[tr[k].ll]) return ask(tr[k].ll,l,mid,x); else return ask(tr[k].rr,mid+1,r,x); } else { int res; if (mid<x) res=n+1; else res=ask(tr[k].ll,l,mid,x); if (res==n+1) return ask(tr[k].rr,mid+1,r,x); else return res; } } int main() { scanf("%d",&T); while (T--) { scanf("%d %d",&n,&m); for (int i=1;i<=n;i++) scanf("%d",&a[i]),b[i].fi=a[i],b[i].se=i,pp[i]=0; sort(b+1,b+1+n); reverse(b+1,b+1+n); idx.clear(); for (int i=1;i<=n;i++) idx[a[i]]=i; for (int i=1;i<=n;i++) { if (i>1&&b[i].fi+1==b[i-1].fi) f[b[i].se]=f[b[i-1].se]; else f[b[i].se]=b[i].fi; } pos=0; root2[n+1]=copy(0); for (int i=n;i>=1;i--) { root2[i]=copy(root2[i+1]); add(root2[i],1,n,a[i]); } int lastans=0; del_nums.clear(); del_nums.insert(n+1); while (m--) { int op,x,r,k; scanf("%d",&op); if (op==1) { scanf("%d",&x); x=x^lastans; if (!pp[x]) { int tmp=x; pp[x]=1; del_nums.insert(a[x]); idx.erase(a[x]); } } else { scanf("%d %d",&r,&k); r=r^lastans; k=k^lastans; if (!idx.count(k)||idx[k]>r) printf("%d\n",k),lastans=k; else { int pos=idx[k]; int x=f[pos]; int y; if (r==n) y=n+1; else y=ask(root2[r+1],1,n,k); it=del_nums.lower_bound(k); int z=*it; lastans=min(x,min(y-1,z-1))+1; printf("%d\n",lastans); } } } } return 0; }
1003:
#include<bits/stdc++.h> #define ll long long #define endl '\n' #pragma comment(linker, "/STACK:1024000000,1024000000") #define ull unsigned long long #define fi first #define se second #define mp make_pair #define pii pair<ll,ll> #define all(x) x.begin(),x.end() #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) #define per(ii,a,b) for(int ii=b;ii>=a;--ii) using namespace std;//head const int maxn=1e6+10,maxm=3e5+10; const ll INF=0x3f3f3f3f,mod=1e9+7; int casn,n,m,k; const int csize=128; char s[maxn]; namespace suffix{ int sa[maxn],h[maxn],rank[maxn]; int x[maxn],y[maxn],c[maxn]; void geth(int n){ int j,k=0; rep(i,1,n)rank[sa[i]]=i; rep(i,1,n){ if(k)k--; j=sa[rank[i]-1]; while(s[i+k]==s[j+k])k++; h[rank[i]]=k; } } void getsa(int n,int m){ rep(i,1,m)c[i]=0; rep(i,1,n)c[x[i]=s[i]]++; rep(i,1,m)c[i]+=c[i-1]; per(i,1,n)sa[c[x[i]]--]=i; for(int k=1;k<=n;k<<=1){ int p=1; rep(i,n-k+1,n)y[p++]=i; rep(i,1,n) if(sa[i]>=k+1)y[p++]=sa[i]-k; rep(i,1,m)c[i]=0; rep(i,1,n)c[x[y[i]]]++; rep(i,1,m)c[i]+=c[i-1]; per(i,1,n)sa[c[x[y[i]]]--]=y[i]; swap(x,y); p=1;x[sa[1]]=1; rep(i,2,n) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p:++p; if(p>=n)break; m=p; } geth(n); } } const int maxp=20; class stable{public: int logn[maxn],dp[maxp][maxn]; void init(int n=maxn-1){ logn[2]=1; rep(i,3,n) logn[i]=logn[i>>1]+1; } void cal(int *a,int n){//init(n) rep(i,1,n) dp[0][i]=a[i]; rep(j,1,maxp) for(int i=1;i+(1<<j)-1<=n;++i) dp[j][i]=min(dp[j-1][i],dp[j-1][i+(1<<(j-1))]); } inline int query(int l,int r){ int lg=logn[r-l+1]; return min(dp[lg][l],dp[lg][r-(1<<lg)+1]); } }st; namespace tree{ #define nd seg[now] #define ndp seg[pre] #define mid ((s+t)>>1) int rt[maxn],size; struct node{int l,r,sum;}seg[maxn*30]; void init(){ size=0;fill_n(rt,n+1,0); } void maketree(int s=1,int t=n,int &now=rt[0]){ now=++size;nd={s,t,0}; if(s==t) return ; maketree(s,mid,nd.l);maketree(mid+1,t,nd.r); } void update(int &now,int pre,int k,int s=1,int t=n){ now=++size;nd=ndp,nd.sum++; if(s==t) return ; if(k<=mid)update(nd.l,ndp.l,k,s,mid); else update(nd.r,ndp.r,k,mid+1,t); } int query(int now,int pre,int k,int s=1,int t=n){ if(s==t) return s; int sum=seg[ndp.l].sum-seg[nd.l].sum; if(k<=sum) return query(nd.l,ndp.l,k,s,mid); else return query(nd.r,ndp.r,k-sum,mid+1,t); } inline int kth(int l,int r,int k){ return query(rt[l-1],rt[r],k); } inline void up(int a,int x){ update(rt[a],rt[a-1],x); } #undef mid }; int query(int s,int t,int k){ int pos=suffix::rank[s]; int l=1,r=pos-1,len=t-s+1; int s0=pos,t0=pos; while(l<=r){ int mid=(l+r)>>1; if(st.query(mid+1,pos)>=len) s0=mid,r=mid-1; else l=mid+1; } l=pos+1,r=n; while(l<=r){ int mid=(l+r)>>1; if(st.query(pos+1,mid)>=len) t0=mid,l=mid+1; else r=mid-1; } if(t0-s0+1<k) return -1; else return tree::kth(s0,t0,k); } int main() {IO; cin>>casn; st.init(); while(casn--){ cin>>n>>m>>(s+1); suffix::getsa(n,128); st.cal(suffix::h,n); tree::init(); tree::maketree(1,n); rep(i,1,n) tree::up(i,suffix::sa[i]); while(m--) { int l,r,k;cin>>l>>r>>k; cout<<query(l,r,k)<<endl; } } }
1005:
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn = 1e6+10; const ll mod = 1e9+7; const ll inv2 = 5e8+4; const ll inv6 = 166666668; int prime[maxn], phi[maxn]; ll sum[maxn]; bool vis[maxn]; int cnt = 0; void init() { phi[1] = 1; for(int i = 2; i < maxn; i++) { if(!vis[i]) prime[++cnt] = i, phi[i] = i-1;; for(int j = 1; j <= cnt && i*prime[j] < maxn; j++) { vis[i*prime[j]] = true; if(i%prime[j] == 0) { phi[i*prime[j]] = phi[i]*prime[j]; break; } else phi[i*prime[j]] = phi[i]*(prime[j]-1); } } sum[0] = 0; for(int i = 1; i < maxn; i++) { sum[i] = (sum[i-1]+1ll*i*phi[i])%mod; } } unordered_map<ll, ll> mp; ll one(ll n) { n %= mod; return n*(n+1)%mod*inv2%mod; } ll squ(ll n) { n %= mod; return n*(n+1)%mod*((2*n+1)%mod)%mod*inv6%mod; } ll F(ll n) { if(n < maxn) return sum[n]; if(mp.count(n)) return mp[n]; ll res = squ(n); for(ll l = 2, r; l <= n; l = r+1) { r = n/(n/l); ll tmp = (one(r)-one(l-1)+mod)%mod; res -= tmp*F(n/l)%mod; if(res < 0) res += mod; } return mp[n] = res; } int main() { init(); int T; scanf("%d", &T); while(T--) { ll n, a, b; scanf("%lld%lld%lld", &n, &a, &b); ll tmp = F(n); ll ans = (tmp+1)*inv2%mod; ans = (ans-1+mod)%mod; printf("%lld\n", (ans%mod+mod)%mod); } return 0; }
1008:
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define fi first #define se second #define mp make_pair #define pii pair<ll,ll> #define all(x) x.begin(),x.end() #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) #define per(ii,a,b) for(int ii=b;ii>=a;--ii) #define forn(i,x,g,e) for(int i=g[x];i;i=e[i].next) #define show(x) cout<<#x<<"="<<x<<endl #define showa(a,b) cout<<#a<<'['<<b<<"]="<<a[b]<<endl #define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl #define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl #define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl #define show5(v,w,x,y,z) cout<<#v<<"="<<v<<" "<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl #define showmm(x,a,b) rep(i,0,a) rep(j,0,b) cout<<#x<<'['<<i<<']'<<'['<<j<<"]="<<x[i][j]<<(" \n"[j==b]) #define showm(x,a,b) rep(i,0,a) rep(j,0,b) cout<<x[i][j]<<(" \n"[j==b]) #define showa1(x,a,b) rep(i,a,b) showa(x,i);cout<<endl #define showa2(x,a,b) rep(i,a,b) cout<<x[i]<<' ';cout<<endl using namespace std; const int maxn=1e5+10,maxm=3e5+10; const ll INF=0x3f3f3f3f,mod=1e9+7; int casn,n,m,k,cnt = 1; priority_queue<int> que; int main() { IO; cin>>casn; while (casn--) { cin >>n>>k; cnt=1; ll sum=k; while(!que.empty()) que.pop(); rep(i,1,n){ ll tmp=0; cin>>tmp; cnt+=tmp/k; sum+=tmp; if (tmp%k)que.push(tmp%k); } rep(i,cnt,n-1) { sum+=k-que.top(); que.pop(); } cout<<sum<<endl; } return 0; }