Lexical Sign Sequence

icpc old title, and recently transferred out of the school oj brought in to do a training game, when the game with his teammates discussed the two approaches, one is for circulation plus Fenwick tree (analysis of the complexity of the analysis time, although it will feel t , but it should be the kind of data does not exist intentionally card, direct pay, did not think about it too, so there will be no second approach practice), but did not expect the game came to an end on the backhand seniors pay such a group hack out a data card, QWQ.

//#pragma GCC optimize(4)
#include <bits/stdc++.h>
#define rint register int
typedef long long ll;
using namespace std;
const int N=100000+5;
int a[N],b[N],c[N],vis[N],n,m;
struct node
{
    int x,y,z;
}ar[N];
void add(int x,int val)
{
    for(int i=x;i<=n;i+=i&-i)c[i]+=val;
}
int ask(int x)
{
    int ans=0;
    for(int i=x;i;i-=i&-i) ans+=c[i];
    return ans;
}
bool cmp(node s,node t)
{
    return s.y<t.y;
}
int main()
{
 
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(a[i]!=0)
        {
            vis[i]=1;
            add(i,a[i]);
        }
        else
        {
            add(i,-1);
            a[i]=-1;
        }
    }
    for(int i=1;i<=m;i++)
    {
        cin>>ar[i].x>>ar[i].y>>ar[i].z;
    }
    sort(ar+1,ar+1+m,cmp);
    int flag=0;
    for(int i=1;i<=m;i++)
    {
        int l=ar[i].x,r=ar[i].y;
        int sum=ask(r)-ask(l-1);
        for(int j=r;j>=l&&sum<ar[i].z;j--)
        {
            if(!vis[j])
            {
                sum+=2;
                add(j,2);
                vis[j]=1;
                a[j]=1;
            }
        }
        if(sum<ar[i].z)
        {
            flag=1;break;
        }
    }
    if(flag) printf("Impossible\n");
    else
    {
        for(int i=1;i<n;i++)
        {
            printf("%d ",a[i]);
        }
        printf("%d\n",a[n]);
    }
    return 0;
}
 
/*************************************************** *********** 
    Problem: 11753 
    the User: Qianwan063 
    Language: C ++ 
    the Result: time overrun 
************************* ************************************** * /

Another approach is by means of a disjoint-set, f [i] point to the first position may become 1, other implementations principle above

#pragma GCC optimize(4)
#include <bits/stdc++.h>
#define rint register int
typedef long long ll;
using namespace std;
const int N=100000+5;
int a[N],b[N],c[N],n,m,f[N];
int getf(int x)
{
    if(f[x]==x) return x;
    return f[x]=getf(f[x]);
   // return f[x]==x? x:f[x]=getf(f[x]);
}
struct node
{
    int x,y,z;
}ar[N];
void add(int x,int val)
{
    for(int i=x;i<=n;i+=i&-i)c[i]+=val;
}
int ask(int x)
{
    int ans=0;
    for(int i=x;i;i-=i&-i) ans+=c[i];
    return ans;
}
bool cmp(node s,node t)
{
    return s.y<t.y;
}
int main()
{
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(a[i]!=0)
        {
            int x=getf(i);
            int y=getf(i-1);
            f[x]=y;
            add(i,a[i]);
        }
        else
        {
            add(i,-1);
            a[i]=-1;
        }
    }
    for(int i=1;i<=m;i++)
    {
        cin>>ar[i].x>>ar[i].y>>ar[i].z;
    }
    sort(ar+1,ar+1+m,cmp);
    int flag=0;
    for(int i=1;i<=m;i++)
    {
        int l=ar[i].x,r=ar[i].y;
        int sum=ask(r)-ask(l-1);
        int pos=r;
        while(sum<ar[i].z&&(pos=getf(pos))>=l)
        {
            sum+=2;
            add(pos,2);
            a[pos]=1;
            f[pos]=getf(pos-1);
        }
        if(sum<ar[i].z)
        {
            flag=1;break;
        }
    }
    if(flag) printf("Impossible\n");
    else
    {
        for(int i=1;i<n;i++)
        {
            printf("%d ",a[i]);
        }
        printf("%d\n",a[n]);
    }
    return 0;
}
 
/**************************************************************
    Problem: 11753
    User: Qianwan063
    Language: C++
    Result: 正确
    Time:53 ms
    Memory:6628 kb
****************************************************************/

 

Guess you like

Origin www.cnblogs.com/Suiyue-Li/p/11415200.html