2019.9.29 csp-s simulation test 55 to reflect summary

Gugu Gu is not a virtue [fog]

The first time realized that burst liver written explanations? ? ?

 

We missed the exam, each person is their own time later Qiazhao exam. My last score to get familiar with a problem ... 152 AC two questions explosion.

The topic is strongly Tucao people named take heart

 

T1 Union:

We found each addition operation of a section, only the section l r + 1, or there might be an answer. Then consider these two points can not be used on behalf of a whole range, maintains global leftmost 0 somewhere.

The operation of each of l and r + 1 are kept down, discrete, building a segment tree. Each line segment tree operations for a [l] -a [r + 1] -1 This section (A [x] x is discretized after sorting, i.e. the position of the segment tree).

Segment tree maintenance intervals and each time the query is to find the range and the leftmost place is not equal to the length of the interval. For the reverse operation, can play a downstream label, and so that existing interval into intervals and interval length minus the original.

Pay attention to the beginning of the answer is 1, save all l and r + 1 would also like to add a extra 1.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int m,cnt=1;
long long b[300010];
struct node{
    int opt;
    long long l,r;
}a[300010];
struct tree{
    int l,r,sum,tag1,tag2;
}t[1000010];
void build(int p,int l,int r){
    t[p].l=l,t[p].r=r;
    t[p].tag1=-1;
    if(l==r)return;
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
}
void pushdown(int p){
    if(t[p].tag1!=-1){
        t[p*2].tag1=t[p].tag1;
        t[p*2].sum=(t[p*2].r-t[p*2].l+1)*t[p].tag1;
        t[p*2].tag2=0;
        t[p*2+1].tag1=t[p].tag1;
        t[p*2+1].sum=(t[p*2+1].r-t[p*2+1].l+1)*t[p].tag1;
        t[p*2+1].tag2=0;
        t[p].tag1=-1;
    }
    if(t[p].tag2){
        t[p*2].tag2^=1;
        t[p*2].sum=t[p*2].r-t[p*2].l+1-t[p*2].sum;
        t[p*2+1].tag2^=1;
        t[p*2+1].sum=t[p*2+1].r-t[p*2+1].l+1-t[p*2+1].sum;
        t[p].tag2=0;
    }
}
void change(int p,int l,int r,int val){
    if(l<=t[p].l&&t[p].r<=r){
        t[p].sum=(t[p].r-t[p].l+1)*val;
        t[p].tag1=val;
        t[p].tag2=0;
        return;
    }
    pushdown(p);
    int mid=(t[p].l+t[p].r)/2;
    if(l<=mid)change(p*2,l,r,val);
    if(r>mid)change(p*2+1,l,r,val);
    t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void change1(int p,int l,int r){
    if(l<=t[p].l&&t[p].r<=r){
        t[p].tag2^=1;
        t[p].sum=t[p].r-t[p].l+1-t[p].sum;
        return;
    }
    pushdown(p);
    int mid=(t[p].l+t[p].r)/2;
    if(l<=mid)change1(p*2,l,r);
    if(r>mid)change1(p*2+1,l,r);
    t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
long long query(int p){
    if(t[p].l==t[p].r){
        return b[t[p].l];
    }
    pushdown(p);
    int mid=(t[p].l+t[p].r)/2;
    long long val;
    if(t[p*2].sum!=t[p*2].r-t[p*2].l+1)val=query(p*2);
    else if(t[p*2+1].sum!=t[p*2+1].r-t[p*2+1].l+1)val=query(p*2+1);
    t[p].sum=t[p*2].sum+t[p*2+1].sum;
    return val;
}
int main()
{
    scanf("%d",&m);
    b[1]=1;
    for(int i=1;i<=m;i++){
        scanf("%d%lld%lld",&a[i].opt,&a[i].l,&a[i].r);
        a[i].r++;
        b[++cnt]=a[i].l;
        b[++cnt]=a[i].r;
    }
    sort(b+1,b+cnt+1);
    int n=unique(b+1,b+cnt+1)-b-1;
    build(1,1,n);
    for(int i=1;i<=m;i++){
        a[i].l=lower_bound(b+1,b+n+1,a[i].l)-b;
        a[i].r=lower_bound(b+1,b+n+1,a[i].r)-b;
        if(a[i].opt==1)change(1,a[i].l,a[i].r-1,1);
        else if(a[i].opt==2)change(1,a[i].l,a[i].r-1,0);
        else change1(1,a[i].l,a[i].r-1);
        printf("%lld\n",query(1));
    }
    return 0;
}
View Code

 

 

T2 Race:

When the examination error greedy fool 40 points ...

Correct answer is to enumerate the number of x meet two people like the article, then kx for everyone supplemented on one of his favorite items, lack of m would no longer complement the rest of the items from inside. Maintenance with the rest of the items to support the tree line before the inquiry and how many minimum.

Note that the border issue.

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,k,A,B,cnt,k1,k2;
int v[200010],v1[200010],va[200010],vb[200010],val[200010];
int a[200010],b[200010],ab[200010],d[200010];
long long sum,ans=1e18,suma[200010],sumb[200010],sumab;
struct node{
    int l,r,cnt;
    long long sum;
}t[1000010];
void build(int p,int l,int r){
    t[p].l=l,t[p].r=r;
    if(l==r)return;
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
}
void change(int p,int l,int r,int y){
    if(l<=t[p].l&&t[p].r<=r){
        t[p].cnt+=y;
        t[p].sum+=y*(val[l]);
        return;
    }
    int mid=(t[p].l+t[p].r)/2;
    if(l<=mid)change(p*2,l,r,y);
    if(r>mid)change(p*2+1,l,r,y);
    t[p].cnt=t[p*2].cnt+t[p*2+1].cnt;
    t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
long long query(int p,int y){
    if(t[p].l==t[p].r){
        return t[p].sum/t[p].cnt*y;
    }
//    int mid=(t[p].l+t[p].r)/2;
    if(t[p*2].cnt>y)return query(p*2,y);
    else if(t[p*2].cnt==y)return t[p*2].sum;
    else{
        long long z=0;
        z+=t[p*2].sum;
        return z+query(p*2+1,y-t[p*2].cnt);
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&v[i]);
        val[++cnt]=v[i];
    }
    sort(val+1,val+cnt+1);
    int num=unique(val+1,val+cnt+1)-val-1;
    scanf("%d",&A);
    for(int i=1,x;i<=A;i++){
        scanf("%d",&x);
        va[x]=1;
    }
    scanf("%d",&B);
    for(int i=1,x;i<=B;i++){
        scanf("%d",&x);
        vb[x]=1;
    }
    if(A<k||B<k||k>m||n<m){
        printf("-1");
        return 0;
    }
    for(int i=1;i<=n;i++){
        v1[i]=lower_bound(val+1,val+num+1,v[i])-val;
        if(va[i]&&vb[i]){
            ab[++ab[0]]=v1[i];
        }
        else if(va[i]){
            a[++a[0]]=v1[i];
        }
        else if(vb[i]){
            b[++b[0]]=v1[i];
        }
        else d[++d[0]]=v1[i];
    }
    if(k*2-ab[0]>m){
        printf("-1");
        return 0;
    }
    build(1,1,num);
    sort(ab+1,ab+ab[0]+1);
    sort(a+1,a+a[0]+1);
    sort(b+1,b+b[0]+1);
    sort(d+1,d+d[0]+1);
    for(int i=1;i<=a[0];i++){
        suma[i]=suma[i-1]+val[a[i]];
        if(i>k)change(1,a[i],a[i],1);
    }
    for(int i=1;i<=b[0];i++){
        sumb[i]=sumb[i-1]+val[b[i]];
        if(i>k)change(1,b[i],b[i],1);
    }
//    for(int i=1;i<=ab[0];i++){
//        change(1,ab[i],ab[i],1);
//    }
    for(int i=1;i<=d[0];i++){
        change(1,d[i],d[i],1);
    }
    for(int i=0;i<=min(ab[0],k);i++){
        if(2*k-m>i||k-i>a[0]||k-i>b[0]){
            if(i>0){
//                change(1,ab[i],ab[i],-1);
                sumab+=val[ab[i]];
                if(a[k-i+1])change(1,a[k-i+1],a[k-i+1],1);
                if(b[k-i+1])change(1,b[k-i+1],b[k-i+1],1);
            }
        }
        else if(i==0){
            sum=suma[k]+sumb[k];
            sum+=query(1,m-2*k);
            ans=min(sum,ans);
        }
        else{
//            change(1,ab[i],ab[i],-1);
            sumab+=val[ab[i]];
            if(a[k-i+1])change(1,a[k-i+1],a[k-i+1],1);
            if(b[k-i+1])change(1,b[k-i+1],b[k-i+1],1);
            sum=sumab+suma[k-i]+sumb[k-i];
            sum+=query(1,m-i-2*(k-i));
            ans=min(ans,sum);
        }
    }
    printf("%lld\n",ans);
    return 0;
}
View Code

 

 

T3 questions:

For each apple, consider it if you want to survive what conditions need to meet, find out if it is to survive needs to ban Apple's collection. Statistics last answer, enumerated two apples, if they have a chance of survival and the survival of the collection need not ban out of the intersection, then the answer ++.

So assuming that an apple a start last alive from the back against the introduction of a set of needs to be met. Backward meaning is, after To have survived to this existing collection, you need to make to meet the set in front of what it was like.

If the operation of a two apples belong to an existing collection, then survived to the final target Apple apparently mortal. If an operator has an apple belongs to an existing collection, then in order to make the Apple survive to meet behind the operation, another Apple certainly belongs to the set before this operation.

Exam time thought the collection also thought Converse, Leng Shimo out out how to write, there is uncertainty about the meaning of a place.

#include<iostream>
#include<cstdio>
#include<bitset>
#include<cstring>
using namespace std;
int n,m,vis[410],flag,ans,x[50010],y[50010],num;
bitset<410>a[410];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x[i],&y[i]);
    }
    for(int i=1;i<=n;i++){
        a[i][i]=1;
        for(int j=m;j>=1;j--){
            int u=x[j],v=y[j];
            if(a[i][u]&&a[i][v]){
                vis[i]=1;
                break;
            }
            else if(a[i][u]||a[i][v]){
                a[i][u]=a[i][v]=1;
            }
        }
    }    
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(!vis[i]&&!vis[j]&&(a[i]&a[j]).count()==0)ans++;
        }
    }
    printf("%d",ans);
    return 0;
}
View Code

 

 

Several test problems exposed more and more, a little bit not cope.

Taking advantage of the National Day quickly chased training schedule ... otherwise the fear is really a complete end after more than a month.

 

Guess you like

Origin www.cnblogs.com/chloris/p/11614792.html