Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:12:20+00:00 2026-06-18T09:12:20+00:00

I don’t have a scenario, but here goes the problem. This is one is

  • 0

I don’t have a scenario, but here goes the problem. This is one is just driving me crazy. There is a nxn boolean matrix initially all elements are 0, n <= 10^6 and given as input.
Next there will be up to 10^5 queries. Each query can be either set all elements of column c to 0 or 1, or set all elements of row r to 0 or 1. There can be another type of query, printing the total number of 1’s in column c or row r.

I have no idea how to solve this and any help would be appreciated. Obviously a O(n) solution per query is not feasible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:12:22+00:00Added an answer on June 18, 2026 at 9:12 am

    The idea of using a number to order the modifications is taken from Dukeling’s post.

    We will need 2 maps and 4 binary indexed tree (BIT, a.k.a. Fenwick Tree): 1 map and 2 BITs for rows, and 1 map and 2 BITs for columns. Let us call them m_row, f_row[0], and f_row[1]; m_col, f_col[0] and f_col[1] respectively.

    Map may be implemented with array, or tree like structure, or hashing. The 2 maps are used to store the last modification to a row/column. Since there can be at most 105 modification, you may use that fact to save space from simple array implementation.

    BIT has 2 operations:

    • adjust(value, delta_freq), which adjusts the frequency of the value by delta_freq amount.
    • rsq(from_value, to_value), (rsq stands for range sum query) which finds the sum of the all the frequencies from from_value to to_value inclusive.

    Let us declare global variable: version

    Let us define numRow to be the number of rows in the 2D boolean matrix, and numCol to be the number of columns in the 2D boolean matrix.

    The BITs should have size of at least MAX_QUERY + 1, since it is used to count the number of changes to the rows and columns, which can be as many as the number of queries.

    Initialization:

    version = 1
    # Map should return <0, 0> for rows or cols not yet
    # directly updated by query
    m_row = m_col = empty map
    f_row[0] = f_row[1] = f_col[0] = f_col[1] = empty BIT
    

    Update algorithm:

    update(isRow, value, idx):
        if (isRow):
            # Since setting a row/column to a new value will reset
            # everything done to it, we need to erase earlier
            # modification to it.
            # For example, turn on/off on a row a few times, then
            # query some column
            <prevValue, prevVersion> = m_row.get(idx)
            if ( prevVersion > 0 ):
                f_row[prevValue].adjust( prevVersion, -1 )
    
            m_row.map( idx, <value, version> )
            f_row[value].adjust( version, 1 )
        else:
            <prevValue, prevVersion> = m_col.get(idx)
            if ( prevVersion > 0 ):
                f_col[prevValue].adjust( prevVersion, -1 )
    
            m_col.map( idx, <value, version> )
            f_col[value].adjust( version, 1 )
    
        version = version + 1
    

    Count algorithm:

    count(isRow, idx):
        if (isRow):
            # If this is row, we want to find number of reverse modifications
            # done by updating the columns
            <value, row_version> = m_row.get(idx)
            count = f_col[1 - value].rsq(row_version + 1, version)
        else:
            # If this is column, we want to find number of reverse modifications
            # done by updating the rows
            <value, col_version> = m_col.get(idx)
            count = f_row[1 - value].rsq(col_version + 1, version)
    
        if (isRow):
           if (value == 1):
               return numRow - count
           else:
               return count
        else:
           if (value == 1):
               return numCol - count
           else:
               return count
    

    The complexity is logarithmic in worst case for both update and count.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't overthink this - there's a very commonly used term and I ... have
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if this is possible, but I have some code like this: val
Don't know why but I can't find a solution to this. I have 3
Don't ask me how but I'm in a situation where I have DCPs published
Don't be scared of the extensive code. The problem is general. I just provided
Don't know how to google for such, but is there a way to query
I don't know why, but this code worked for me a month ago... maybe
Don't know what happened, but it just hangs on Type Space loading (4%) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.