Skip to content

Commit 6290851

Browse files
committed
Merge branch 'master' of https://github.com/phumipatc/Algorithm
2 parents dccedc3 + 1681254 commit 6290851

3 files changed

Lines changed: 74 additions & 85 deletions

File tree

Divide_and_Conquer/Merge_Sort.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +0,0 @@
1-
/*
2-
Task : Mod_of_Power
3-
Author : Phumipat C. [MAGCARI]
4-
School : RYW
5-
Language: C++
6-
Explanation Link : https://www.geeksforgeeks.org/multiply-two-polynomials-2/
7-
Created : 03 May 2021 [15:15]
8-
*/
9-
10-
11-
#include<bits/stdc++.h>
12-
#define all(x) begin(x),end(x)
13-
#define sz(x) (int)(x).size()
14-
#define MOD (LL )(1e9+7)
15-
using namespace std;
16-
using LL = long long;
17-
using PII = pair<int ,int >;
18-
using PLL = pair<long long ,long long >;
19-
const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}};
20-
const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}};
21-
22-
23-
24-
int *multiply(int A[], int B[], int m, int n)
25-
{
26-
int *prod = new int[m+n-1];
27-
28-
for (int i = 0; i<m+n-1; i++)
29-
prod[i] = 0;
30-
31-
for (int i=0; i<m; i++)
32-
{
33-
for (int j=0; j<n; j++)
34-
prod[i+j] += A[i]*B[j];
35-
}
36-
37-
return prod;
38-
}
39-
40-
void printPoly(int poly[], int n)
41-
{
42-
for (int i=0; i<n; i++)
43-
{
44-
cout << poly[i];
45-
if (i != 0)
46-
cout << "x^" << i ;
47-
if (i != n-1)
48-
cout << " + ";
49-
}
50-
}

MO's_alogorithm.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// put sum of subarrays of size sqrt(n) into block array
5+
void preprocess_blocks(vector<int>&arr, vector<int>& block)
6+
{
7+
int len=block.size();
8+
9+
for(int i=0;i<arr.size();i++)
10+
{
11+
block[i/len]+=arr[i];
12+
}
13+
14+
}
15+
16+
int main()
17+
{
18+
int n;
19+
//input size of array
20+
cin>>n;
21+
22+
vector<int> arr(n);
23+
24+
//input array
25+
for(int i=0;i<n;i++)
26+
cin>>arr[i];
27+
28+
//initialize size of blocks to be sqrt(n)+1
29+
int len= sqrt(n)+1;
30+
vector<int> block( len );
31+
32+
// preprocess blocks with sum
33+
preprocess_blocks(arr, block);
34+
35+
int querySize;
36+
//input total queries
37+
cin>>querySize;
38+
39+
int l,r;
40+
//input all the queries
41+
while(querySize--)
42+
{
43+
//1 indexed
44+
cin>>l>>r;
45+
l--;
46+
r--;
47+
48+
int sum=0;
49+
//traversing l to r efficiently by taking
50+
//already calculated sum from block array
51+
for(int i=l;i<=r;)
52+
{
53+
//if i is the starting index of a block and that block is in
54+
//the range l to r then take precalculated sum
55+
//else take arr[i] to sum
56+
if(i%len==0 && i+len-1<=r)
57+
{
58+
sum+= block[i/len];
59+
i+=len;
60+
}
61+
else
62+
{
63+
sum+=arr[i];
64+
i++;
65+
}
66+
}
67+
68+
//output sum
69+
cout<<sum<<endl;
70+
71+
}
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)