Problem solving report (difference template)

Posted by makeITfunctional on Tue, 18 Jan 2022 13:15:10 +0100

Differential algorithm:

Given an array of N length, there are m requests. Each request adds the number c in the [l,r] (L < = n; R < = n) interval, and finally outputs the array.

If the violence method is used to solve, m requests need to be cycled, and each request needs to traverse the array (r-l) times ((r-l) < = n). The time complexity is O(mn) (the data range is about 10e3). Using the difference algorithm, the time complexity can be reduced to linear, i.e. O(m+n); (the data range can be about 10e7)

Next, we will explain the difference algorithm in detail

     

 

It can be seen that difference is actually the inverse of prefix sum operation. When it is necessary to add c to the elements in the [l,r] interval, the operation to be done is b [l] + = c. Since array A is the prefix and sum of array b, the elements of array a after l will be automatically added with c. Because it is in a closed interval, it is greater than r subscript

The element of needs to subtract the corresponding c to offset the previously added c. The operation at this time is B [R + 1] - = c; The time complexity of this step is O(m);

After calculating the difference fraction group, (when initializing the b array, the value is 0), there are two ideas on how to get the operated array a:

Personal: for b array, calculate the sum of each prefix in b array, that is, traverse b array, b[i]+=b[i-1]; This is equivalent to obtaining the specific value of operation c for each subscript. If array a wants to obtain the array after operation, it only needs to be added to the subscript of array b after calculating the prefix and

y total: the elements in array a can be regarded as [l,l] interval. Add element a [l] to array b and initialize it first. Then do m more request operations. Finally, calculate the sum of each prefix of the b array.

No matter which method, the time complexity of the implementation is O(n), that is, the length of the array, and the final time complexity is O(n+m), which is linear

Note: when the values of l and r are too large and most elements after the difference are not traversed, the discretization method needs to be used.

 

Title:

  Acwing 797. Difference (template question)

 

 

//Personal method
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=1e6;
int a[MAXN];
int b[MAXN];
int main()
{
    int n,m;
    cin>>n>>m;
    memset(b,0,sizeof(b));//Initialize differential array
    for(int i=1;i<=n;i++) cin>>a[i];//Read in original array
    for(int i=0;i<m;i++)
    {
        int ai,bi,c;
        cin>>ai>>bi>>c;
        b[ai]+=c;
        b[bi+1]-=c;
    }
    for(int i=1;i<=n;i++) b[i]+=b[i-1];//Calculate the prefix and of the differential array so that the value of the corresponding subscript in the differential array is the total value after the corresponding operation
    for(int i=1;i<=n;i++) a[i]+=b[i];//about a Each subscript of the array is read into the corresponding operation
    for(int i=1;i<=n;i++) cout<<a[i]<<" ";
    return 0;
}

 

//y total
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=1e6;
int a[MAXN];
int b[MAXN];

void insert(int l,int r,int c)
{
    b[l]+=c;
    b[r+1]-=c;
    return ;
}

int main()
{
    int n,m;
    cin>>n>>m;
    memset(b,0,sizeof(b));//Initialize differential array
    for(int i=1;i<=n;i++) 
    {
        cin>>a[i];//Read in original array
        insert(i,i,a[i]);//Treat the original array as in an interval[i,i]Add elements to a[i]
    }
    for(int i=0;i<m;i++)
    {
        int ai,bi,c;
        cin>>ai>>bi>>c;
        insert(ai,bi,c);
    }
    for(int i=1;i<=n;i++) b[i]+=b[i-1];//Calculate the prefix and of the differential array so that the value of the corresponding subscript in the differential array is the total value after the corresponding operation
for(int i=1;i<=n;i++) cout<<b[i]<<" "; return 0; }

 

Acwing 2041. Haystack

 

In fact, this question is also a template question, but all elements of the original array in the question are 0 by default. You only need to get the prefix and sum of the difference score group. Because you need to sort from small to large and output the intermediate value, you also need to sort after the operation. And the middle value is output, that is, the subscript is (n/2+1)

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN=1e6+5;
int st[MAXN],d[MAXN];
int main()
{
  int n,k;
  cin>>n>>k;
  //memset(st,0,sizeof(st));
  memset(d,0,sizeof(d));
  for(int i=1;i<=k;i++)
  {
    int l,r;
    cin>>l>>r;
    d[l]++; d[r+1]--;//Add haystacks for sections
  }
  for(int i=1;i<=n;i++) d[i]+=d[i-1];//Calculate prefix and
  sort(d+1,d+n+1);
  cout<<d[n/2+1];//Calculate median
}