第一届&第二届“幻想杯”NOIP模拟赛 标程

来源:互联网 发布:家庭媒体中心软件 编辑:程序博客网 时间:2024/05/16 01:40

实在懂不了的话直接来1601找chenyixiao,或者是在lfyzoj上发私信,或者是在luogu上发私信。

灵梦的宴会

#include <algorithm>#include <iostream>#include <cstdio>using namespace std;struct Odge{    int too, fro, val;}odge[1000005];struct Edge{    int too, nxt, val;}edge[200005];int n, m, k, uu, vv, ecnt=0, hea[100005]={0}, r1, r2, fa[100005];int depth[100005]={0};int grand[100005][18]={0};void rn(int &x){    char ch=getchar();    x = 0;    while(ch<'0' || ch>'9')   ch = getchar();    while(ch>='0' && ch<='9'){        x = x * 10 + ch - '0';        ch = getchar();    }}inline void add_edge(int fro, int too, int val){    edge[++ecnt].nxt = hea[fro];    edge[ecnt].too = too;    edge[ecnt].val = val;    hea[fro] = ecnt;}int myfind(int x){    return fa[x]==x?x:fa[x]=myfind(fa[x]);}void kruskal(){    int cnt=0;    for(int i=1; i<=k; i++){        r1 = myfind(odge[i].fro);        r2 = myfind(odge[i].too);        if(r1!=r2){            cnt++;            add_edge(odge[i].fro, odge[i].too, odge[i].val);            add_edge(odge[i].too, odge[i].fro, odge[i].val);            fa[r2] = r1;        }        if(cnt==n-1)    break ;    }}inline bool cmp(Odge x, Odge y){    return x.val>y.val;}void bt(int u){    for(int i=hea[u]; i; i=edge[i].nxt){        int t=edge[i].too;        if(!depth[t]){            depth[t] = depth[u] + 1;            grand[t][0] = u;            bt(t);        }    }}int query(int x, int y){    if(depth[x]<depth[y]){        x ^= y;        y ^= x;        x ^= y;    }    for(int i=17; i>=0; i--)        if(depth[grand[x][i]]>=depth[y])            x = grand[x][i];    if(x==y)    return y;    for(int i=17; i>=0; i--)        if(grand[x][i]!=grand[y][i]){            x = grand[x][i];            y = grand[y][i];        }    return grand[x][0];}int main(){    rn(n);rn(m);rn(k);    for(int i=1; i<=n; i++)        fa[i] = i;    for(int i=1; i<=k; i++){        rn(odge[i].fro);rn(odge[i].too);rn(odge[i].val);    }    sort(odge+1, odge+1+k, cmp);    kruskal();    depth[1] = 1;    bt(1);    for(int i=1; i<=17; i++)        for(int j=1; j<=n; j++)            grand[j][i] = grand[grand[j][i-1]][i-1];    for(int i=1; i<=m; i++){        rn(uu);rn(vv);         printf("%d\n", depth[uu]+depth[vv]-2*depth[query(uu, vv)]);    }    return 0;}

烧烤摊

#include <iostream>#include <cstdio>using namespace std;int n, m, t, u, v, w;typedef long long ll;struct SGT{    ll lzt[400005];    ll sum[400005];    void build(int o, int l, int r){        lzt[o] = 0;        if(l==r)    scanf("%lld", &sum[o]);        else{            int mid=(l+r)>>1;            int lson=o<<1;            int rson=lson|1;            build(lson, l, mid);            build(rson, mid+1, r);            sum[o] = sum[lson] + sum[rson];        }    }    void push_down(int o, int l, int r, int mid, int lson, int rson){        lzt[lson] += lzt[o];        lzt[rson] += lzt[o];        sum[lson] += (mid-l+1)*lzt[o];        sum[rson] += (r-mid)*lzt[o];        lzt[o] = 0;    }    void update(int o, int l, int r, int x, int y, int k){        if(l>=x && r<=y){            lzt[o] += k;            sum[o] += (r-l+1)*k;        }        else{            int mid=(l+r)>>1;            int lson=o<<1;            int rson=lson|1;            if(lzt[o])  push_down(o, l, r, mid, lson, rson);            if(x<=mid)   update(lson, l, mid, x, y, k);            if(y>mid)    update(rson, mid+1, r, x, y, k);            sum[o] = sum[lson] + sum[rson];        }    }    long long query(int o, int l, int r, int x, int y){        if(l>=x && r<=y)  return sum[o];        else{            int mid=(l+r)>>1;            int lson=o<<1;            int rson=lson|1;            ll ans=0;            if(lzt[o])  push_down(o, l, r, mid, lson, rson);            if(x<=mid)   ans += query(lson, l, mid, x, y);            if(y>mid)    ans += query(rson, mid+1, r, x, y);            return ans;        }    }}sgt;void rn(int &x){    char ch=getchar();    x = 0;    while(ch<'0' || ch>'9')   ch = getchar();    while(ch>='0' && ch<='9'){        x = x * 10 + ch - '0';        ch = getchar();    }}ll exgcd(ll a, ll b, ll &x, ll &y){    if(!b){        x = 1;        y = 0;        return a;    }    ll re=exgcd(b, a%b, x, y);    ll t=x;    x = y;    y = t - a / b * y;    return re;}int main(){    rn(n);rn(m);    sgt.build(1, 1, n);    for(int i=1; i<=m; i++){        rn(t);rn(u);rn(v);rn(w);        if(t==1)    sgt.update(1, 1, n, u, v, w);        else{            ll x, y, gcd, b=v-u+1, a=sgt.query(1, 1, n, u, v);            gcd=exgcd(a, w, x, y);            if(b%gcd)   printf("-1\n");            else{                w /= gcd;                printf("%lld\n", ((x%w*b/gcd)%w+w)%w);            }        }    }    return 0;}

古老的元神

水题。

#include <iostream>#include <cstring>#include <cstdio>using namespace std;int n, u, a[3500005], dp[3500005], ans;void rn(int &x){    char ch=getchar();    x = 0;    while(ch<'0' || ch>'9')   ch = getchar();    while(ch>='0' && ch<='9'){        x = x * 10 + ch - '0';        ch = getchar();    }}int main(){    cin>>n;    for(int i=1; i<=n; i++){        rn(u);        dp[u] = i;    }    for(int i=1; i<=n; i++){        rn(u);        a[i] = dp[u];    }    memset(dp, 0x3f, sizeof(dp));    dp[0] = 0;    for(int i=1; i<=n; i++){        if(dp[ans]<a[i])    dp[++ans] = a[i];        else{            int l=1, r=ans, mid;            while(l<r){                mid = (l + r) >> 1;                if(dp[mid]<a[i])    l = mid + 1;                else                r = mid;            }            dp[l] = min(dp[l], a[i]);        }    }    cout<<ans;    return 0;}

Sakura Collection

#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;int dis[200005][3], cnt, hea[200005], n, m, p, e, b, u, v, ans=0x3f3f3f3f;queue<int> d;bool ins[200005];struct Edge{    int too, nxt;}edge[2000005];void add_edge(int fro, int too){    edge[++cnt].nxt = hea[fro];    edge[cnt].too = too;    hea[fro] = cnt;}void spfa(int nl, int r){    d.push(nl);    ins[nl] = true;    dis[nl][r] = 0;    while(!d.empty()){        int x=d.front();        d.pop();        ins[x] = false;        for(int i=hea[x]; i; i=edge[i].nxt){            int t=edge[i].too;            if(dis[t][r]>dis[x][r]+1){                dis[t][r] = dis[x][r] + 1;                if(!ins[t]){                    ins[t] = true;                    d.push(t);                }            }        }    }}int main(){    scanf("%d %d %d %d %d", &b, &e, &p, &n, &m);    for(int i=1; i<=m; i++){        scanf("%d %d", &u, &v);        add_edge(u, v);        add_edge(v, u);    }    memset(dis, 0x3f, sizeof(dis));    spfa(1, 0);    spfa(2, 1);    spfa(n, 2);    for(int i=1; i<=n; i++){        if(dis[i][0]!=0x3f3f3f3f&&dis[i][1]!=0x3f3f3f3f&&dis[i][2]!=0x3f3f3f3f)            ans = min(ans, b*dis[i][0]+e*dis[i][1]+p*dis[i][2]);    }    cout<<ans;    return 0;}

喷气式摩托

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <cmath>using namespace std;int n, m, thea[5005], tcnt, hea[10005], cnt, sx[5005], w[5005], s[5005];int uu, vv, ww, dis[10005];bool inq[10005];queue<int> d;struct Te{    int too, nxt, val;}te[30005];struct Edge{    int too, nxt, val;}edge[120005];void add_tedge(int fro, int too, int val){    te[++tcnt].nxt = thea[fro];    te[tcnt].too = too;    te[tcnt].val = val;    thea[fro] = tcnt;}void add_edge(int fro, int too, int val){    edge[++cnt].nxt = hea[fro];    edge[cnt].too = too;    edge[cnt].val = val;    hea[fro] = cnt;}void bui(int u){    add_edge(u, u+n, s[u]);    add_edge(u+n, u, 0);    for(int i=thea[u]; i; i=te[i].nxt){        int t=te[i].too;        int delta=abs(w[t]-w[u]);        if(sx[u]!=sx[t]){            add_edge(u, t, te[i].val+delta);            add_edge(u+n, t+n, max(0, te[i].val-delta));        }        else{            add_edge(u, t+n, te[i].val);            add_edge(u+n, t, te[i].val);        }    }}void spfa(){    memset(dis, 0x3f, sizeof(dis));    if(sx[1]){        dis[1] = 0;        d.push(1);        inq[1] = true;    }    else{        dis[1+n] = 0;        d.push(1+n);        inq[1+n] = true;    }    while(!d.empty()){        int x=d.front();        d.pop();        inq[x] = false;        for(int i=hea[x]; i; i=edge[i].nxt){            int t=edge[i].too;            if(dis[t]>dis[x]+edge[i].val){                dis[t] = dis[x] + edge[i].val;                if(!inq[t]){                    inq[t] = true;                    d.push(t);                }            }        }    }}int main(){    cin>>n>>m;    for(int i=1; i<=n; i++)        scanf("%d", &sx[i]);    for(int i=1; i<=n; i++)        scanf("%d", &w[i]);    for(int i=1; i<=n; i++)        scanf("%d", &s[i]);    for(int i=1; i<=m; i++){        scanf("%d %d %d", &uu, &vv, &ww);        add_tedge(uu, vv, ww);    }    for(int i=1; i<=n; i++)        bui(i);    spfa();    printf("%d", min(dis[n], dis[n+n]));    return 0;}

稼欲灵招来

水题。

#include <algorithm>#include <iostream>#include <cstdio>using namespace std;struct Node{    int num, ran;}a[100005], b[100005];int n, tot, c[100005], d[100005], sum=0;const int mod=99999997;bool cmp(Node x, Node y){    return x.num<y.num;}inline int lb(int x){    return x & -x;}void add(int pos, int num){    while(pos<=n){        d[pos] += num;        pos += lb(pos);    }}int query(int pos){    int re=0;    while(pos){        re += d[pos];        pos -= lb(pos);    }    return re;}int main(){    cin>>n;    for(int i=1; i<=n; i++)  scanf("%d", &a[i].num), a[i].ran=i;    for(int i=1; i<=n; i++)  scanf("%d", &b[i].num), b[i].ran=i;    sort(a+1, a+1+n, cmp);    sort(b+1, b+1+n, cmp);    for(int i=1; i<=n; i++)        c[a[i].ran] = b[i].ran;    for(int i=1; i<=n; i++){        add(c[i], 1);        sum = (sum + i - query(c[i])) % mod;    }    cout<<sum;    return 0;}
原创粉丝点击