码迷,mamicode.com
首页 > 其他好文 > 详细

ccpc 2019网络选拔赛

时间:2019-08-24 09:23:12      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:cpp   unsigned   next   网络   return   namespace   const   tmp   pop   

先存一下几个较难题的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;
}

 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;
}

 

ccpc 2019网络选拔赛

标签:cpp   unsigned   next   网络   return   namespace   const   tmp   pop   

原文地址:https://www.cnblogs.com/nervendnig/p/11403247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!