722C - Destroying Array - CodeForces Solution


data structures dsu *1600

Please click on ads to support us..

Python Code:

class DSU:
    def __init__(self,n,arr):
        self.rank = [0 for _ in range(n)]
        self.parent = [i for i in range(n)]
        self.sum = [arr[i] for i in range(n)]

    def max_sum(self,par2,par1):
        self.sum[par1]+=self.sum[par2]
        return self.sum[par1]

    def find(self,x):
        if self.parent[x]!=x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def union(self,x,y):
        par1 = self.find(x)
        par2 = self.find(y)

        if par1==par2:
            return
        if self.rank[par1]>self.rank[par2]:
            out = self.max_sum(par2,par1)
            self.parent[par2] = par1
        else:
            out = self.max_sum(par1, par2)
            self.parent[par1] = par2
            if self.rank[par1] ==self.rank[par2]:
                self.rank[par2]+=1
        return out


def destroy(N,L,index):
    dsu_obj = DSU(N,L)
    place_fill = [False for _ in range(N)]
    output = [0]
    max_sum = 0

    for val in index[:0:-1]:
        id = val-1
        comp_sum = L[id]
        place_fill[id] = True
        if id-1>=0 and place_fill[id-1]:
            comp_sum = dsu_obj.union(id-1,id)
        if id+1<N and place_fill[id+1]:
            comp_sum = dsu_obj.union(id,id+1)
        if comp_sum>max_sum:
            max_sum = comp_sum
        output.append(max_sum)
    for val in output[::-1]:
        print(val)




N = int(input())
L = list(map(int,input().split()))
index = list(map(int,input().split()))
destroy(N,L,index)

C++ Code:

#include <iostream>
using namespace std;
#include <bits/stdc++.h>
typedef long long ll ;
#define lup(i,a,b) for (int i=(a) ; i<(b) ; i++)
#define ld(i,a,b) for (int i=(a) ; i>=(b) ; i--)

ll mod=1e9+7 ;
ll po(ll n,ll m, ll mod  )
{
    if(!m)
        return 1 ;
    if(m%2==0)
    {
        ll c=po(n,m/2,mod) ;
        c%=mod ;
        return (c*c)%mod ;
    }
    else
    {
        ll y=n%mod ;
        return (y*(po(n,m-1,mod)%mod))%mod ;
    }
}
const int N=1e5+5, M=1e5+6 ;
int n, m, ne, head[N], nxt[M], to[M] ;

void initgraph()
{
    memset(head,-1,n*sizeof head[0]) ;
}

void adedge (int f, int t)
{
    to[ne]=t ;
    nxt[ne]=head[f] ;
    head[f]=ne++ ;
}

void adbiedge(int u, int v)
{
    adedge(u,v) ;
    adedge(v,u) ;
}

double eps=1e-7 ;
int dx[4]= {-1,0,1,0} ;
int dy[4]= {0,1,0,-1} ;
bool valid(int i,int j,int n,int m)
{
    return (i>=0 && i<=1+n && j>=0 && j<=1+m ) ;
}
ll p[300009] , sum[200000];
int findparent(int u)
{
    if(p[u]==u) return u ;
    return p[u]=findparent(p[u]) ;

}

int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    //freopen("input.txt","r",stdin) ;
    //freopen("output.txt","w",stdout) ;
    // cout << fixed << setprecision(3) ;

    int n , u ,ve ;
    cin >> n ;
    vector<ll>v(n+1) , a(n+1) , ans(n+1);
    lup(i,1,n+1) p[i]=i ;
    lup(i,1,n+1) { cin >> a[i] ; sum[i]=a[i] ; }
    lup(i,1,n+1) cin >> v[i] ;
    ll mx=0 ;
    int f[n+2]={0} ;
    ld(i,n,1)
    {
      ans[i]=mx ;
      int x=v[i] ;
      f[x]++ ;
      if(f[x-1] && !f[x+1])
      {
         u=findparent(x-1) ;
         sum[u]+=sum[x] ;
         p[x]=u ;
      }
      else if(f[x+1] && !f[x-1])
      {
         u=findparent(x+1) ;
         sum[u]+=sum[x] ;
         p[x]=u ;
      }
      else if(f[x-1] && f[x+1])
      {
         u=findparent(x+1) , ve=findparent(x-1) ;
         sum[u]+=(sum[x]+sum[ve]) ;
         p[ve]=u , p[x]=u ;
      }
      mx=max(mx,sum[findparent(x)]) ;

    }
    lup(i,1,n+1) cout << ans[i] << "\n" ;




    }




 	  		 	    		    	 	   			  	


Comments

Submit
0 Comments
More Questions

765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven