Best Data structure to support range Sum and consecutive elements swap operation?

Suresh Chatti

We have an array of n positive integers and m quires. Each query can be one of the following two types.

  1. Find the sum of all elements between some range [i, j]
  2. Reorder the elements given in a range [i, j] such that array elements will be like this

(a[i+1],a[i],a[i+3],a[i+2],..............,a[j],a[j-1]) given that this range length will be even. where a[i] is array element at i index.

Following are the limits

2 ≤ n,m ≤ 2×10^5 

1 ≤ a[i] ≤ 10^6 

1≤ i ≤ j ≤n

I tried with segment trees but these are taking more than 2 secs. Can anybody suggest best data structure Or any best approach?

Here is my code

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int sgtree[524288];
int data[200005];
int treeind[200005];
int constructSgTree(int start,int end,int index){
    if(start==end){
        sgtree[index]=data[start];
        treeind[start]=index;
        return data[start];
    }
    else{
        int mid=start+(end-start)/2;
        sgtree[index]=constructSgTree(start,mid,index*2+1)+constructSgTree(mid+1,end,index*2+2);
        return sgtree[index];
    }
}
int sumSgTree(int start,int end,int i,int j,int index){
    if(i<=start&&j>=end){
        return sgtree[index];
    }
    else if(i>end||j<start){
        return 0;
    }
    else{
            int mid=start+(end-start)/2;
            return sumSgTree(start,mid,i,j,2*index+1)+sumSgTree(mid+1,end,i,j,2*index+2);
    }
}
void updateSgTree(int start,int end,int i,int val,int index){

    if(i<start||i>end){
        return ;
    }
    else{
        sgtree[index]+=val;
        if(start!=end)
        {
        int mid=start+(end-start)/2;

        updateSgTree(start,mid,i,val,2*index+1);

        updateSgTree(mid+1,end,i,val,2*index+2);
            }

    }




}

int main() {
    int n,i,q,op,l,r,temp,j,temp1,temp2,temp3;
    cin>>n>>q;
    float sum=0;
    for(i=0;i<n;i++){
        cin>>data[i];
        //dataind[i]=i;
    }
    constructSgTree(0,n-1,0);
    /*
    for(i=0;i<n;i++){
        cout<<sgtree[treeind[i]]<<" ";
    }
    */
    //cout<<endl;
    for(i=0;i<q;i++){
        cin>>op>>l>>r;
        l--;
        r--;
        if(op==2){
            j=l;
            /*
            sum=0.0;
            while(j<=r){
                /*
                temp=data[j]-sgtree[treeind[j]];
                if(temp!=0){
                    updateSgTree(0,n-1,j,temp,0);
                }

                sum+=data[j];
                j++;

            }
            cout<<sum<<endl;
         */
            cout<<sumSgTree(0,n-1,l,r,0)<<endl;
        }
        else{
            while(l<=r){

                //temp=data[l+1]-data[l];
                if(l!=r){
                    temp=sgtree[treeind[l]];
                sgtree[treeind[l]]=sgtree[treeind[l+1]];
                sgtree[treeind[l+1]]=temp;
                temp1=(treeind[l]-1)/2;
                temp2=(treeind[l+1]-1)/2;



                    while(temp1!=temp2){
                        if(temp1<temp2){
                            sgtree[temp2]=sgtree[temp2]+data[l]-data[l+1];
                            temp2=(temp2-1)/2;
                        }
                        else{
                            sgtree[temp1]=sgtree[temp1]-data[l]+data[l+1];
                            temp1=(temp1-1)/2;
                        }
                    }
                    //updateSgTree(0,n-1,l,temp,0);
                    //updateSgTree(0,n-1,l+1,-temp,0);

                /*
                temp=data[l];
                data[l]=data[l+1];
                data[l+1]=temp;
                */
                temp=data[l];
                data[l]=data[l+1];
                data[l+1]=temp; 
                }

                l+=2;   
            }
        }
    }
    return 0;
}
kraskevich

The time complexity of your solution is at least O(n)(maybe it is O(n log n)) per query, so it is obviously too slow.

Here is a solution that requires O(log n) time per query:

  1. Let's build to treaps with implicit keys: the first one will contain all elements that have an even index and the second one will contain all elements that have an odd index. Both trees should keep elements sorted according to their index in the given array.

  2. A sum query is pretty straightforward: we know the smallest and the largest odd and even index within the range, so we can run this query on the first and the second tree and return the sum.

  3. A swap query can be processed in the following way: let's split the first tree into L1, M1, R1 parts and the second one into L2, M2, R2(where M1 and M2 is the part that lies inside the query range). Then we should swap M1 and M2 and merge trees back, that is, the first tree is the result of merging L1, M2, R1 and the second one is L2, M1, R2.

Each query requires constant number of merge and split operations, so the total time complexity is O((n + m) * log n) or O(n + m * log n)(it depends on the way we construct these treaps before answering queries).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Best Data structure to support range Sum and consecutive elements swap operation?

From Dev

Array consecutive elements sum

From Dev

How to build a data-structure to support some range queries

From Dev

best complexity swap elements in stack

From Dev

Sum consecutive groups of elements of an array

From Dev

Sum consecutive pairs of elements in a list

From Dev

A data structure which keeps elements in sorted order, supports fast insertion and calculation of maximum difference between consecutive elements

From Dev

A data structure which keeps elements in sorted order, supports fast insertion and calculation of maximum difference between consecutive elements

From Dev

maximum sum of n consecutive elements of array

From Dev

Data Structure for faster contains() operation?

From Dev

Insert operation on Queap Data Structure

From Dev

What is the best data structure to use when adding elements to list based on comparisons c#

From Dev

What data structure to find number of elements in a given range in O(log n) time?

From Dev

Best way to build this data structure

From Dev

Is dictionary the best data structure for this case?

From Dev

Data structure similar to Dictionary, but with range?

From Dev

Best way to keep the elements of a structure up to date

From Dev

Two sum data structure problems

From Dev

swap elements by comparing data attribute in jquery

From Dev

Minimum sum such that one of every three consecutive elements is taken

From Dev

Best data structure to store peculiarly structured data

From Dev

Data Structure of the Periodic Table of Elements

From Dev

Accessing elements of a ruby data structure

From Dev

R, Operation on data frame across consecutive rows with repeated entries

From Dev

best data structure to fit constant deletion in graph

From Dev

Best big set data structure in Java

From Dev

Scala - best data structure to store a trie

From Dev

Which data structure is best for an ordered list of strings?

From Dev

Best Data Structure to store marks and ranks of students

Related Related

  1. 1

    Best Data structure to support range Sum and consecutive elements swap operation?

  2. 2

    Array consecutive elements sum

  3. 3

    How to build a data-structure to support some range queries

  4. 4

    best complexity swap elements in stack

  5. 5

    Sum consecutive groups of elements of an array

  6. 6

    Sum consecutive pairs of elements in a list

  7. 7

    A data structure which keeps elements in sorted order, supports fast insertion and calculation of maximum difference between consecutive elements

  8. 8

    A data structure which keeps elements in sorted order, supports fast insertion and calculation of maximum difference between consecutive elements

  9. 9

    maximum sum of n consecutive elements of array

  10. 10

    Data Structure for faster contains() operation?

  11. 11

    Insert operation on Queap Data Structure

  12. 12

    What is the best data structure to use when adding elements to list based on comparisons c#

  13. 13

    What data structure to find number of elements in a given range in O(log n) time?

  14. 14

    Best way to build this data structure

  15. 15

    Is dictionary the best data structure for this case?

  16. 16

    Data structure similar to Dictionary, but with range?

  17. 17

    Best way to keep the elements of a structure up to date

  18. 18

    Two sum data structure problems

  19. 19

    swap elements by comparing data attribute in jquery

  20. 20

    Minimum sum such that one of every three consecutive elements is taken

  21. 21

    Best data structure to store peculiarly structured data

  22. 22

    Data Structure of the Periodic Table of Elements

  23. 23

    Accessing elements of a ruby data structure

  24. 24

    R, Operation on data frame across consecutive rows with repeated entries

  25. 25

    best data structure to fit constant deletion in graph

  26. 26

    Best big set data structure in Java

  27. 27

    Scala - best data structure to store a trie

  28. 28

    Which data structure is best for an ordered list of strings?

  29. 29

    Best Data Structure to store marks and ranks of students

HotTag

Archive