Training 11

4012: What is the Rank?

The meaning of problems: a given number n, in order to add a set number, and outputs the number in the current set of several

#include<bits/stdc++.h>
using namespace std;
const int Max=45005;
struct node
{
     int x,y;
}e[Max];
int cmp(node a,node b)
{
    return a.y>b.y;
}
int cmp1(node a,node b)
{
    return a.x<b.x;
}
int f[Max];
void update(int x)
{
    for(int i=x;i<Max;i+=i&-i)
    {
        f[i]+=1;
    }
}
int query(int x)
{
    int sum=0;
    for(int i=x;i>0;i-=i&-i)
    {
        sum+=f[i];
    }
    return sum;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&e[i].y);
        e[i].x=i;
    }
    sort(e+1,e+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        e[i].y=i;
    }
    sort(e+1,e+n+1,cmp1);
    for(int i=1;i<=n;i++)
    {
        update(e[i].y);
        printf("%d\n",query(e[i].y));
    }
}

// hash + Fenwick tree

3656: Intersections

Meaning of the questions: Given a circle and a line, circle and line intersect seek

. Ideas: a line intersecting two points are not in the inner circle;

2. Two bit line segments in the circle, another point of the intersection of the outer circle (circle to intersect the bit);

3. When the outer circle are in the two o'clock, first find the center of a foot in a straight line, not a foot outside the circle intersect, when the pedal inside the circle (including a circle), continue to determine whether two points relatively perpendicular to the line segment foot in the same direction, the same direction as the disjoint reverse intersection;

To the point (x1, y1), (x2, y2) determined by the general formula: AX + BY + C = 0

A=y2-y1    B=x1-x2     C=x2*y1-x1*y2

Obtaining pedal (x3, y3) according to the ABC and the center (x, y)

x3=(b*b*x-a*b*y-a*c)/(a*a+b*b);y3=(a*a*y-a*b*x-b*c)/(a*a+b*b);

#include<bits/stdc++.h>
using namespace std;
void printY()
{
   printf("The line segment intersects the circle.\n");
}
void printN()
{
    printf("The line segment does not intersect the circle.\n");
}
int main()
{
    double x,y,r,x1,y1,x2,y2;
    while(~scanf("%lf%lf%lf",&x,&y,&r))
     {
         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
         double a=y2-y1;
         double b=x1-x2;
         double c=x2*y1-x1*y2;
         double x3=(b*b*x-a*b*y-a*c)/(a*a+b*b);
         double y3=(a*a*y-a*b*x-b*c)/(a*a+b*b);
         if((x-x1)*(x-x1)+(y-y1)*(y-y1)<r*r)
         {
             if((x-x2)*(x-x2)+(y-y2)*(y-y2)<r*r)
             printN();
             else
             printY();
         }
         else
         {
             if((x-x2)*(x-x2)+(y-y2)*(y-y2)<r*r)
               printY();
             else
             {
                 if((x-x3)*(x-x3)+(y-y3)*(y-y3)<=r*r)
                 {
                     if(x3==x2)
                     {
                         if((y3-y2)/abs(y3-y2)==(y3-y1)/abs(y3-y1))
                          printN();
                         else
                          printY();
                     }
                     else
                     {
                         if((x3-x2)/abs(x3-x2)==(x3-x1)/abs(x3-x1))
                          printN();
                         else
                          printY();
                     }
                 }
                 else
                 {
                     printN();
                 }
             }
         }
     }
}

//mathematics 

4613: Number of Battlefields

Meaning of the questions: Given a perimeter p, ask how many there are perimeter shape of p (not rectangular)

Matrix fast power demand Fibonacci number

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=987654321;
void mul(ll b[2][2],ll a[2][2])
{
    ll c[2][2]={0};
    for(int i=0;i<=1;i++)
    {
        for(int j=0;j<=1;j++)
        {
            c[i][j]=b[i][0]*a[0][j]%MOD+b[i][1]*a[1][j]%MOD;
        }
    }
    memcpy(b,c,sizeof(c));
}
void power(ll a[2][2],int p)
{
    ll b[2][2]={1,0,1,0};
    while(p)
    {
        if(p&1)
        {
            mul(b,a);
        }
        mul(a,a);
        p/=2;
    }
    memcpy(a,b,sizeof(b));
}
int main()
{
    int p;
    while(scanf("%d",&p),p)
    {
        if (p&1||p<8)
        {
            printf("0\n");
            continue;
        }
        p=(p-4)/2;
        ll a[2][2]={1,1,1,0};
        power(a,2*p);
        printf("%lld\n",(a[1][0]-p-1+MOD)%MOD);
    }
}

4505: Khoshahria

Meaning of the questions: There are n boxes, m kinds of toys. n boxes each containing certain different types of toys, now you select some boxes, so that these boxes contain all kinds of toys; asked how many choices can meet the conditions

#include<bits/stdc++.h>
using namespace std;
const int Max=(1<<20)+5;
const int Mod=1000000007;
int cnt[Max],num[Max],ans[Max];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    cnt[0]=1;
    int f=1<<m;
    for(int i=1;i<=n;i++)
    {
        cnt[i]=(cnt[i-1]*2)%Mod;
        int k,s=0;
        scanf("%d",&k);
        for(int j=1;j<=k;j++)
        {
            int x;
            scanf("%d",&x);
            s+=1<<x-1;
        }
        num[s]++;
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<f;j++)
        {
            if(j&(1<<i))num[j]+=num[j-(1<<i)];
        }
    }
    for(int i=0;i<f;i++)
    {
        ans[i]=cnt[num[i]];
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<f;j++)
        {
            if(j&(1<<i))ans[j]=(ans[j]-ans[j-(1<<i)]+Mod)%Mod;
        }
    }
    printf("%d\n",ans[f-1]%Mod);
}

@ + Form inclusion and exclusion pressure DP

6043: Tunnel Warfare

The meaning of problems: there is a straight line n villages, there are three operations, D operation, destruction of the village, Q operation, the query has several villages village direct connections or indirect connections (including itself), R latest operation will be destroyed the village reconstruction;

#include<bits/stdc++.h>
using namespace std;
const int Max=50005;
struct node{
   int llen,rlen,mlen;
}e[Max*4];
void Build(int l,int r,int i)
{
    e[i].llen=e[i].rlen=e[i].mlen=r-l+1;
    int mid=(l+r)/2;
    if(l==r)return ;
    Build(l,mid,i*2);
    Build(mid+1,r,i*2+1);
}
void Update(int l,int r,int i,int nod,int flag)
{
    if(l==r)
    {
        e[i].llen=e[i].rlen=e[i].mlen=flag;
        return ;
    }
    int mid=(l+r)/2;
    if(nod<=mid)Update(l,mid,i*2,nod,flag);
    else Update(mid+1,r,i*2+1,nod,flag);
    e[i].llen= e [ 2 * i] .llen; 
    e [i] .rlen = e [ 2 * i + 1 ] .rlen; 
    e [i] .mlen = max (max (e [i * 2 ] .mlen, e [i * 2 + 1 ] .mlen), e [i * 2 ] .rlen + e [i * 2 + 1 ]. llen);
    if (e [ 2 * i] .llen == mid-l + 1 ) 
        e [i] .llen + = e [ 2 * i + 1 ] .llen;
    if (e [i * 2 + 1 ] == .rlen r- mid) 
        e [i] .rlen + = e [ 2 *i].rlen;
}
int Query(int l,int r,int i,int x)
{
    if(l==r||e[i].mlen==0||e[i].mlen==r-l+1)
    {
        return e[i].mlen;
    }
    int mid=(l+r)/2;
    if(x<=mid)
    {
        if(x>=mid-e[i*2].rlen+1)
            return Query(l,mid,i*2,x)+Query(mid+1,r,i*2+1,mid+1);
         else
            return Query(l,mid,i*2,x);
    }
    else
    {
        if(x<=mid+1+e[i*2+1].llen-1)
            return Query(mid+1,r,i*2+1,x)+Query(l,mid,i*2,mid);
        else
            return Query(mid+1,r,i*2+1,x);
    }
}
stack<int> st;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    Build(1,n,1);
    for(int i=1;i<=m;i++)
    {
        getchar();
        char ch;
        scanf("%c",&ch);
        if(ch=='D')
        {
            int x;
            scanf("%d",&x);
            Update(1,n,1,x,0);
            st.push(x);
        }
        else if(ch=='Q')
        {   int x;
            scanf("%d",&x);
            printf("%d\n",Query(1,n,1,x));
        }
        else
        {
            if(!st.size())continue;
            int x=st.top();
            st.pop();
            Update(1,n,1,x,1);
        }
    }
}

 

 

 

Guess you like

Origin www.cnblogs.com/llhsbg/p/11864541.html