Swaps and Sum


Problem Statement :


You are given a sequence a1, a2, a3, . . . an. The task is to perform the following queries on it:

Type 1. Given two integers l and r(  1 <=  l  <=  r ; r - l +1 is even ) . Reorder the elements of the sequence in such a way (changed part of the sequence is in brackets):

That is swap the first two elements of segment [ l , r ] , the second two elements, and so on.
Type 2. Given two integers l and r, print the value of sum .

Input Format

The first line contains two integers n and q. The second line contains n integers a1, a2, a3 . . . , an, denoting initial sequence.

Each of the next q lines contains three integers tpi, li, ri, where  tpi  denotes the type of the query, and  li, ri are parameters of the query. It's guaranteed that for a first-type query ( r - l + 1 )  will be even.


Constraints

2  <=  n  <=   2 x 10^5
1  <=  q  <=  2 x 10^5
1  <=  ai  <=  10^6
1  <=  tpi  <= 2
1   <=  li  <=  ri  <=  n


Output Format

For each query of the second type print the required sum



Solution :



title-img


                            Solution in C :

In   C++  :







#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll N = 1<<18;
const ll INF = 1000000000;
const ll md =1000*1000*1000;
struct name
{
    ll x,y,l,r,val,mi;
};
ll n,m,tt0,x,t0,tt1,t1,type,l,r,l0,r0,l1,r1,t0f,t1f,t0l,t1l,t0m,t1m,t0r,t1r,tr,s,t;
vector <name> treap;
vector <ll> a;
void split(ll t,ll k,ll &t1,ll &t2){
     if (t==0)
     {
        t1=t2=0;
        return;
     }
     if (k>treap[treap[t].l].x){
        split(treap[t].r,k-treap[treap[t].l].x-1,treap[t].r,t2);
        t1=t;
     }
     else
     {
        split(treap[t].l,k,t1,treap[t].l);
        t2=t;
     }
     treap[t].x=treap[treap[t].l].x+treap[treap[t].r].x+1;
     treap[t].mi=treap[t].val+treap[treap[t].l].mi+treap[treap[t].r].mi;;
}
void merge (ll & t,ll t1, ll t2){
      if (!t1 || !t2) {
             if (!t1)
                t=t2;
             else
                t=t1;
             return ;
         }
         if (treap[t1].y>treap[t2].y)
         {
                merge(treap[t1].r,treap[t1].r,t2);
                t=t1;
         }
         else
         {
                merge(treap[t2].l,t1,treap[t2].l);
                t=t2;
         }
         treap[t].x=treap[treap[t].l].x+treap[treap[t].r].x+1;
         treap[t].mi=treap[t].val+treap[treap[t].l].mi+treap[treap[t].r].mi;
}
ll Pos(ll v)
{
    return v/2+v%2;
}
void Getlr()
{
     if (l%2==1)
               {
                   l0=Pos(l+1)+n/2+n%2;
                   l1=Pos(l);
               }
               else
               {
                   l1=Pos(l+1);
                   l0=Pos(l)+n/2+n%2;
               }
               if (r%2==1)
               {
                   r1=Pos(r);
                   r0=Pos(r-1)+n/2+n%2;
               }
               else
               {
                   r1=Pos(r-1);
                   r0=Pos(r)+n/2+n%2;
               }
}
int main()
{

s=0;
        scanf("%lld%lld",&n,&m);
        tr=t=0;
        treap.clear();
        a.clear();
        treap.resize(n+5);
        a.resize(n+5);
        for (int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for (int i=1;i<=n;i+=2)
        {
            tr++;
            treap[tr].x=1;
            treap[tr].y=rand()+1;
            treap[tr].val=treap[tr].mi=a[i];
            merge(t,t,tr);
        }
        for (int i=2;i<=n;i+=2)
        {
            tr++;
            treap[tr].x=1;
            treap[tr].y=rand()+1;
            treap[tr].val=treap[tr].mi=a[i];
            merge(t,t,tr);
        }
         while (m--)
         {
            scanf("%lld%lld%lld",&type,&l,&r);
            if (type==1)
            {
               Getlr();
            split(t,r0,t,t0r);
            split(t,l0-1,t,t0m);
            split(t,r1,t,t1r);
            split(t,l1-1,t,t1m);
            merge(t,t,t0m);
            merge(t,t,t1r);
            merge(t,t,t1m);
            merge(t,t,t0r);
            }
            else
            {
                Getlr();
            split(t,r0,t,t0r);
            split(t,l0-1,t,t0m);
            split(t,r1,t,t1r);
            split(t,l1-1,t,t1m);
            printf("%lld\n",treap[t0m].mi+treap[t1m].mi);
            merge(t,t,t1m);
            merge(t,t,t1r);
            merge(t,t,t0m);
            merge(t,t,t0r);
            }
         }
}









In   Java  :








import java.io.*;
import java.util.*;

public class Solution {
  private static Reader in;
  private static PrintWriter out;

  public static void main(String[] args) throws IOException {
    in = new Reader();
    out = new PrintWriter(System.out, true);
    
    int N = in.nextInt(), Q = in.nextInt();
    arr = new int[N+1];
    for (int i = 1; i <= N; i++) arr[i] = in.nextInt();
    rootEven = initRec(1, N/2, true);
    rootOdd = initRec(1, (N+1)/2, false);
    for (int i = 0; i < Q; i++) {
      int cmd = in.nextInt();
      if (cmd == 1) {
        flip(in.nextInt(), in.nextInt());
      } else {
        out.println(getSum(in.nextInt(), in.nextInt()));
      }
//      for (int j = 1; j <= N; j++) out.print(" "+getSum(j, j));
//      out.println();
    }
    out.close();
    System.exit(0);
  }
  public static int[] arr;
  static class Node {
    int size;
    Node left;
    Node right;
    Node parent;
    int val;
    long sum;

    public Node(int val) {
      this.val = val;
      this.sum = val;
      this.size = 1;
      left = null;
      right = null;
      parent = null;
    }

    public String toString() {
      return val + " " + size;
    }
  }

  // Whether x is a root of a splay tree
  static boolean isRoot(Node x) {
    return x.parent == null;
  }

  static void connect(Node ch, Node p, boolean leftChild) {
    if (leftChild)
      p.left = ch;
    else
      p.right = ch;
    join(p);
    if (ch != null) {
      ch.parent = p;
    }
  }

  // rotate edge (x, x.parent)
  static void rotate(Node x) {
    Node p = x.parent;
    Node g = p.parent;
    boolean isRootP = isRoot(p);
    boolean leftChildX = (x == p.left);

    Node next = leftChildX ? x.right : x.left;
    connect(next, p, leftChildX);
    connect(p, x, !leftChildX);

    if (!isRootP)
      connect(x, g, p == g.left);
    else
      x.parent = g;
  }

  static Node splay(Node x) {
    while (!isRoot(x)) {
      Node p = x.parent;
      Node g = p.parent;
      if (!isRoot(p))
        rotate((x == p.left) == (p == g.left) ? p : x);
      rotate(x);
    }
    return x;
  }

  static Node cutLeft(Node x) {
    Node ret = x.left;
    if (ret != null) {
      x.left.parent = null;
      x.left = null;
      join(x);
    }
    return ret;
  }

  static Node cutRight(Node x) {
    Node ret = x.right;
    if (ret != null) {
      x.right.parent = null;
      x.right = null;
      join(x);
    }
    return ret;
  }

  static void join(Node x) {
    x.size = (x.left == null ? 0 : x.left.size) + (x.right == null ? 0 : x.right.size) + 1;
    x.sum = (x.left == null ? 0 : x.left.sum) + (x.right == null ? 0 : x.right.sum) + x.val;
  }

  private static Node getElementAtPosition(boolean even, int a) {
    Node cur = even ? rootEven : rootOdd;
    while (a > 0) {
      int sz = (cur.left == null ? 0 : cur.left.size);
      if (a <= sz) {
        cur = cur.left;
        continue;
      }
      a -= sz + 1;
      if (a == 0)
        break;
      cur = cur.right;
    }
    Node ret = splay(cur);
    if (even) rootEven = ret; else rootOdd = ret;
    return cur;
  }
  
  private static long getSum(int a, int b) {
    if (a == b) {
      return getElementAtPosition(a % 2 == 0, (a+1)/2).val;
    }
    Node righte = getElementAtPosition(true, b/2);
    Node rae = cutRight(righte);
    Node lefte = getElementAtPosition(true, (a+1)/2);
    Node lae = cutLeft(lefte);
    
    Node righto = getElementAtPosition(false, (b+1)/2);
    Node rao = cutRight(righto);
    Node lefto = getElementAtPosition(false, a/2+1);
    Node lao = cutLeft(lefto);
      
    long res = lefte.sum + lefto.sum;

    rootEven = splay(righte);
    connect(rae, righte, false);
    rootEven = splay(lefte);
    connect(lae, lefte, true);
    
    rootOdd = splay(righto);
    connect(rao, righto, false);
    rootOdd = splay(lefto);
    connect(lao, lefto, true);
    
    return res;
  }

  private static void flip(int a, int b) {
    if (a == b)
      return;
    int off = (b-a+1) / 2;
    int se = (a+1)/2;
    Node righte = getElementAtPosition(true, se+off-1);
    Node rae = cutRight(righte);
    Node lefte = getElementAtPosition(true, se);
    Node lae = cutLeft(lefte);

    int so = a/2+1;
    Node righto = getElementAtPosition(false, so+off-1);
    Node rao = cutRight(righto);
    Node lefto = getElementAtPosition(false, so);
    Node lao = cutLeft(lefto);
      
      
    rootOdd = splay(righte);
    connect(rao, righte, false);
    rootOdd = splay(lefte);
    connect(lao, lefte, true);
    
    rootEven = splay(righto);
    connect(rae, righto, false);
    rootEven = splay(lefto);
    connect(lae, lefto, true);
  }
  
  private static Node initRec(int start, int end, boolean even) {
    if (start == end) {
      int idx = (start-1) * 2;
      if (!even) idx++; else idx += 2;
      return new Node(arr[idx]);
    }
    int mid = (start + end) >> 1;
    int idx = (mid-1) * 2;
    if (!even) idx++; else idx += 2;
    Node x = new Node(arr[idx]);
    if (start <= mid - 1)
      connect(initRec(start, mid - 1, even), x, true);
    if (mid + 1 <= end)
      connect(initRec(mid + 1, end, even), x, false);
    return x;
  }

  private static Node rootEven, rootOdd;
  static class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
      din = new DataInputStream(System.in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException {
      din = new DataInputStream(new FileInputStream(file_name));
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException {
      byte[] buf = new byte[1 << 20];
      int cnt = 0;
      byte c = read();
      while (c <= ' ')
        c = read();
      do {
        buf[cnt++] = c;
      } while ((c = read()) != '\n');
      return new String(buf, 0, cnt);
    }

    public String next() throws IOException {
      byte[] buf = new byte[1 << 20];
      int cnt = 0;
      byte c = read();
      while (c <= ' ')
        c = read();
      do {
        buf[cnt++] = c;
      } while ((c = read()) > ' ');
      return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException {
      int ret = 0;
      byte c = read();
      while (c <= ' ')
        c = read();
      boolean neg = (c == '-');
      if (neg)
        c = read();
      do {
        ret = ret * 10 + c - '0';
      } while ((c = read()) >= '0' && c <= '9');
      if (neg)
        return -ret;
      return ret;
    }

    public long nextLong() throws IOException {
      long ret = 0;
      byte c = read();
      while (c <= ' ')
        c = read();
      boolean neg = (c == '-');
      if (neg)
        c = read();
      do {
        ret = ret * 10 + c - '0';
      } while ((c = read()) >= '0' && c <= '9');
      if (neg)
        return -ret;
      return ret;
    }

    public double nextDouble() throws IOException {
      double ret = 0, div = 1;
      byte c = read();
      while (c <= ' ')
        c = read();
      boolean neg = (c == '-');
      if (neg)
        c = read();
      do {
        ret = ret * 10 + c - '0';
      } while ((c = read()) >= '0' && c <= '9');
      if (c == '.')
        while ((c = read()) >= '0' && c <= '9')
          ret += (c - '0') / (div *= 10);
      if (neg)
        return -ret;
      return ret;
    }

    private void fillBuffer() throws IOException {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1)
        buffer[0] = -1;
    }

    private byte read() throws IOException {
      if (bufferPointer == bytesRead)
        fillBuffer();
      return buffer[bufferPointer++];
    }

    public void close() throws IOException {
      if (din == null)
        return;
      din.close();
    }
  }


}









In   C   :










#include <stdio.h>
#include <stdlib.h>
typedef struct _ct_node{
  int size;
  int priority;
  int value;
  long long sum;
  struct _ct_node *left,*right;
} ct_node;
long long get_sum(int x,int y,ct_node *root);
void get_size(ct_node *root);
ct_node* merge(ct_node *L,ct_node *R);
int sizeOf(ct_node *root);
long long sumOf(ct_node *root);
void recalc(ct_node *root);
void split(int x,ct_node **L,ct_node **R,ct_node *root);
void reverse(int x,int y);
void computeTree(int x);
int N,P[200000],T[200000],st[200000];
ct_node poll[200000],*odd,*even;

int main(){
  int Q,x,y,i;
  scanf("%d%d",&N,&Q);
  for(i=0;i<N;i++){
    scanf("%d",&poll[i].value);
    poll[i].priority=P[i]=rand();
    poll[i].size=-1;
    poll[i].left=poll[i].right=NULL;
  }
  computeTree(0);
  computeTree(1);
  for(i=0;i<N;i++)
    if(T[i]==-1)
      if(i%2)
        odd=&poll[i];
      else
        even=&poll[i];
    else
      if(i<T[i])
        poll[T[i]].left=&poll[i];
      else
        poll[T[i]].right=&poll[i];
  get_size(odd);
  get_size(even);
  while(Q--){
    scanf("%d",&x);
    switch(x){
      case 1:
        scanf("%d%d",&x,&y);
        reverse(x,y);
        break;
      default:
        scanf("%d%d",&x,&y);
        if(x==y)
          if(x%2)
            printf("%lld\n",get_sum((x-1)/2,(x-1)/2,even));
          else
            printf("%lld\n",get_sum((x-1)/2,(x-1)/2,odd));
        else
          printf("%lld\n",get_sum((x-1)/2,(y-2)/2,odd)+get_sum(x/2,(y-1)/2,even));
    }
  }
  return 0;
}
long long get_sum(int x,int y,ct_node *root){
  if(!root || x>y || x>root->size-1 || y<0)
    return 0;
  if(x<=0 && y>=root->size-1)
    return root->sum;
  int curidx=sizeOf(root->left),t;
  long long ls,rs,ans=0;
  if(curidx>=x && curidx<=y)
    ans=root->value;
  if(y<curidx)
    ls=get_sum(x,y,root->left);
  else
    ls=get_sum(x,curidx-1,root->left);
  if(x>curidx)
    rs=get_sum(x-curidx-1,y-curidx-1,root->right);
  else
    rs=get_sum(0,y-curidx-1,root->right);
  return ans+ls+rs;
}
void get_size(ct_node *root){
  if(!root)
    return;
  int ls=0,rs=0;
  long long lsu=0,rsu=0;
  if(root->left){
    if(root->left->size==-1)
      get_size(root->left);
    ls=root->left->size;
    lsu=root->left->sum;
  }
  if(root->right){
    if(root->right->size==-1)
      get_size(root->right);
    rs=root->right->size;
    rsu=root->right->sum;
  }
  root->size=ls+rs+1;
  root->sum=lsu+rsu+root->value;
  return;
}
ct_node* merge(ct_node *L,ct_node *R){
  if(!L)
    return R;
  if(!R)
    return L;
  if(L->priority>R->priority){
    L->right=merge(L->right,R);
    recalc(L);
    return L;
  }
  R->left=merge(L,R->left);
  recalc(R);
  return R;
}
int sizeOf(ct_node *root){
  return (root)?root->size:0;
}
long long sumOf(ct_node *root){
  return (root)?root->sum:0;
}
void recalc(ct_node *root){
  root->size=sizeOf(root->left)+sizeOf(root->right)+1;
  root->sum=sumOf(root->left)+sumOf(root->right)+root->value;
  return;
}
void split(int x,ct_node **L,ct_node **R,ct_node *root){
  if(!root){
    *L=*R=NULL;
    return;
  }
  int curIndex=sizeOf(root->left);
  ct_node *t;
  if(curIndex<=x){
    split(x-curIndex-1,&t,R,root->right);
    root->right=t;
    recalc(root);
    *L=root;
  }
  else{
    split(x,L,&t,root->left);
    root->left=t;
    recalc(root);
    *R=root;
  }
  return;
}
void reverse(int x,int y){
  ct_node *ol,*om,*or,*el,*em,*er;
  int A,B;
  A=(x-1)/2;
  B=(y-2)/2;
  split(A-1,&ol,&or,odd);
  split(B-A,&om,&or,or);
  A=x/2;
  B=(y-1)/2;
  split(A-1,&el,&er,even);
  split(B-A,&em,&er,er);
  odd=merge(merge(ol,em),or);
  even=merge(merge(el,om),er);
  return;
}
void computeTree(int x){
  int i,k,top=-1;
  for(i=x;i<N;i+=2){
    k=top;
    while(k>=0 && P[st[k]]<P[i])
      k--;
    if(k!=-1)
      T[i]=st[k];
    if(k<top)
      T[st[k+1]]=i;
    st[++k]=i;
    top=k;
  }
  T[st[0]]=-1;
  return;
}









In   Python3  :







def read_numbers():
    return [int(i) for i in input().split(" ")]

def swap_numbers(l,r, numbers):
    sub = numbers[l:r]
    for i in range(0, r-l, 2):
        l1 = sub[i]
        r1 = sub[i+1]
        sub[i+1] = l1
        sub[i] = r1
    return sub
n,q = read_numbers()
numbers = read_numbers()
results = []
for x in range(q):
    t,l,r = read_numbers()
    l -= 1
    if t == 1:
        numbers[l:r] = swap_numbers(l,r,numbers)
    else:
        results.append(str(sum(numbers[l:r])))
print("\n".join(results))
                        








View More Similar Problems

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →