# Gray histogram

Question No.: | 202104-1 |

Test title: | Gray histogram |

time limit: | 1.0s |

Memory limit: | 512.0MB |

Problem Description: | ## Problem descriptionA gray image with length and width of N pixels and m pixels can be expressed as an n × Matrix A of size m. The gray statistical histogram of a gray image (hereinafter referred to as "histogram") can be expressed as an array h with length L, where h[x] (0 ≤ x < L) represents the number of pixels with gray value x in the image. Obviously, the sum of h[0] to h[L − 1] should be equal to the total number of pixels in the image n ⋅ m. Given the gray matrix A of an image, try to calculate its gray histogram h[0],h[1],..., h[L − 1]. ## Input formatEnter a total of n+1 lines. The first line of input contains three positive integers n, m, and L separated by spaces, as described above. Input matrix A on the second to n+1 lines. ## Output formatThe output is only one line, containing L integers h[0],h[1], * and h[L − 1], separated by spaces, representing the gray histogram of the input image. ## sample input4 4 16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Data
## sample output1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Data
## sample input7 11 8 0 7 0 0 0 7 0 0 7 7 0 7 0 7 0 7 0 7 0 7 0 7 7 0 0 0 7 0 0 0 7 0 7 7 0 0 0 0 7 0 0 7 7 0 7 0 0 0 0 0 7 0 7 0 0 7 0 7 0 7 0 7 0 7 0 0 0 7 0 0 0 7 0 0 7 0 0
Data
## sample output48 0 0 0 0 0 0 29
Data
## Scale and agreement of evaluation casesAll test data meet 0 < n, m ≤ 500 and 4 ≤ L ≤ 256. |

#include <iostream> using namespace std; const int N = 256; int a[N]; int n, m, l, t; int main() { cin >> n >> m >> l; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> t; a[t]++; } } for (int i = 0; i < l; ++i) { cout << a[i] << " "; } return 0; }

# Neighborhood mean

Question No.: | 202104-2 |

Test title: | Neighborhood mean |

time limit: | 1.0s |

Memory limit: | 512.0MB |

Problem Description: | ## Test backgroundAfter learning digital image processing, Dunton wants to denoise a gray-scale image on his opponent. However, the image only has a lot of noise in the dark area. If noise reduction is carried out rashly on the whole image, it will erase the noise and blur the original image. Therefore, Dunton calculates to use the neighborhood mean value to judge whether a pixel is in the dark area, and then only the image in the dark area The pixels in the dark area are denoised. ## Problem descriptionThe length and width of the gray image to be processed are n pixels, which can be expressed as an n × N-size matrix A, where each element is an integer in the range of [0,L), representing the gray value of the corresponding position pixel. Neighbor(i,j,r)={Axy|0≤x,y<n and |x−i|≤r and |y−j|≤r} Here, an additional parameter r is used to indicate the specific range of elements near Aij. According to the definition, it is easy to know that Neighbor(i,j,r) has at most (2r+1)2 elements. If the average value of all elements in the neighborhood of element Aij is less than or equal to a given threshold t, we consider that the pixel at the corresponding position of the element is in a darker area. Given the neighborhood parameter r and threshold t, try to count how many pixels in the input gray image are in the dark area. ## Input formatEnter a total of n+1 lines. The first line of input contains four positive integers n, L, r and t separated by spaces, with the meaning described above. Input matrix A on the second to n+1 lines. ## Output formatOutputs an integer representing the total number of pixels in the darker area of the input grayscale image. ## sample input4 16 1 6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Data
## sample output7
Data
## sample input11 8 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 7 0 0 7 7 0 7 0 7 0 7 0 7 0 7 0 7 7 0 0 0 7 0 0 0 7 0 7 7 0 0 0 0 7 0 0 7 7 0 7 0 0 0 0 0 7 0 7 0 0 7 0 7 0 7 0 7 0 7 0 0 0 7 0 0 0 7 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Data
## sample output83
Data
## Scale and agreement of evaluation cases70% of the test data meet n ≤ 100 and r ≤ 10. All test data meet 0 < n ≤ 600, 0 < R ≤ 100 and 2 ≤ T < L ≤ 256. |

//2D prefix and #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int N = 610; int n, m, t, l, r, T; int a[N][N]; int main() { scanf("%d%d%d%d", &n, &l, &r, &t); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { scanf("%d", &a[i][j]); a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } } // r--; int res = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { int aa = i <= r ? 1 : i - r; int bb = j <= r ? 1 : j - r; int cc = i + r >= n ? n : i + r; int dd = j + r >= n ? n : j + r; int tem = a[cc][dd] - a[cc][bb - 1] - a[aa - 1][dd] + a[aa - 1][bb - 1]; int square = (cc - aa + 1) * (dd - bb + 1); double temp = 1.0 * tem / square; if (temp <= t) { res++; } } } cout << res << endl; return 0; }

# Safety index of end of period forecast

Question No.: | 202012-1 |

Test title: | Safety index of end of period forecast |

time limit: | 1.0s |

Memory limit: | 512.0MB |

Problem Description: | ## Topic backgroundThe end of the term is coming. Xiaocai found his good friend Dunton, hoping to predict whether he will fail this semester. ## Title DescriptionFirstly, Dunton selected n indicators such as "classroom performance", "self-study duration", "homework score", "community activity participation" as the basis for prediction. Finally, the safety index y at the end of the term is defined as follows: Where ReLU(x)=max(0,x) is a common activation function. Given the importance of each index wi and the corresponding score score, let's calculate the safety index of Xiaocai students at the end of the term. ## Input formatRead in data from standard input. The first line of input contains a positive integer n, ensuring 2 ≤ n ≤ 105. Next, enter line n, where line I (1 ≤ I ≤ n) contains two integers wi and scorei separated by spaces, representing the importance of the i-th index and the score of the dishes. ## Output formatOutput to standard output. Output a non negative integer y, which represents the safety index of the end of the period. ## Example 1 input6 2 60 10 100 0 70 0 0 -10 50 10 60
Data
## Sample 1 output1220
Data
## Example 1 explanationy=ReLU(1220)=1220 ## Example 2 Input2 -10 100 -1 15
Data
## Example 2 output0
Data
## Example 2 explanationy=ReLU(−1015)=0 |

#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int n, m, T; int main() { scanf("%d", &n); int res = 0, a, b; for (int i = 0; i < n; ++i) { scanf("%d%d", &a, &b); res += a * b; } res = max(0, res); printf("%d", res); return 0; }

# Optimal threshold of period end forecast

Question No.: | 202012-2 |

Test title: | Optimal threshold of period end forecast |

time limit: | 1.0s |

Memory limit: | 512.0MB |

Problem Description: | ## Topic backgroundConsidering that the safety index is an integer in a large range and the side dishes are likely to be confused about whether they are really safe, Dunton decided to set a threshold θ， In order to convert the safety index y into a specific prediction result - "fail" or "fail". Because the higher the safety index, the lower the possibility of students failing the course, so when y ≥ θ Dunton will predict that the side dishes will be very safe and will not fail this semester; on the contrary, if y< θ， Dunton would admonish the side dish: "you're going to fail at the end of the term. Don't say you didn't say it beforehand." So how to set this threshold? Dunton is ready to find the answer from the past. ## Title DescriptionSpecifically, Dunton evaluated the safety index of M students last semester, in which the safety index of the i (1 ≤ i ≤ m) student is yi, which is an integer within the range of [0108]; At the same time, the student's failing in the last semester is recorded as resulti ∈ 0,1, where 0 means failing and 1 means not failing. Accordingly, Dunton uses predict θ (y) Indicates according to the threshold θ Convert the safety index y into specific prediction results. predictθ(y)={0(y<θ)1(y≥θ) Finally, Dunton designed the following formula to calculate the optimal threshold θ ∗: θ∗=maxargmaxθ∈yi∑j=1m(predictθ(yj)==resultj) The formula can also be expressed equivalently as the following rules: The best threshold is only selected in yi, that is, it is the same as the safety index of a classmate; According to the threshold value, the m students' failure in the last semester was predicted, and the prediction was correct the most times (i.e. the accuracy rate was the highest); When multiple thresholds can reach the highest accuracy, the largest one is selected.
## Input formatRead in data from standard input. The first line of input contains a positive integer m. Next, enter line m, where line i (1 ≤ i ≤ m) includes two integers yi and resulti separated by spaces, with the meaning as described above. ## Output formatOutput to standard output. Outputs an integer representing the optimal threshold θ ∗. ## Example 1 input6 0 0 1 0 1 1 3 1 5 1 7 1
Data
## Sample 1 output3
Data
## Example 1 explanationAccording to rule 1, the selection range of the optimal threshold is 0,1,3,5,7. θ= When 0, the number of correct predictions is 4; θ= 1, the number of correct predictions is 5; θ= 3, the number of correct predictions is 5; θ= At 5, the number of correct predictions is 4; θ= At 7, the number of correct predictions is 3. When the threshold is 1 or 3, the prediction accuracy is the highest; According to rule 3, θ ∗=max1,3=3. ## Example 2 Input8 5 1 5 0 5 0 2 1 3 0 4 0 100000000 1 1 0
Data
## Example 2 output100000000
Data
## Subtask70% of the test data ensure that m ≤ 200; All test data shall be 2 ≤ m ≤ 105. |

// Sorting + simulation, class prefix and judgment #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #define x first #define y second using namespace std; typedef pair<int, int> PII; const int N = 100010; int n, m, T; int s[N]; PII a[N]; bool cmp(PII q, PII u) { if (q.x == u.x)return q.y > u.y; return q.x < u.x; } int main() { scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d%d", &a[i].x, &a[i].y); } sort(a + 1, a + m + 1, cmp); for (int i = 1; i <= m; ++i) { s[i] = a[i].y + s[i - 1]; } int mmax = 0, m_idx = 0; for (int i = 1; i <= m; ++i) { int t = (s[m] - s[i - 1]) + i - (s[i - 1] - s[0]); if (mmax <= t) { mmax = t; m_idx = i; } } printf("%d", a[m_idx].x); return 0; }

# Scale checkpoint query

Question No.: | 202009-1 |

Test title: | Scale checkpoint query |

time limit: | 1.0s |

Memory limit: | 256.0MB |

Problem Description: | ## Topic backgroundIn June 8, 2020, the joint defense and joint control mechanism issued by the State Council issued the opinions on accelerating the implementation of COVID-19 nucleic acid detection. It proposed that the eight categories of "close contacts" and other "key groups" should be inspected as far as possible. ## Problem descriptionThere are n nucleic acid detection points in a city, numbered from 1 to N, in which the position of detection point i can be expressed as a plane integer coordinate (xi,yi). In order to make an appointment for nucleic acid detection, please query the three nearest detection points according to the location of citizens (X,Y). ## Input formatEnter a total of n+1 lines. The first line contains three integers n, X and Y separated by spaces, indicating the total number of detection points and the location of citizens. Enter the coordinates of N detection points from the second line to the n+1 line. Line i+1 (1 ≤ i ≤ n) contains two integers xi and yi separated by spaces, indicating the location of test point i. ## Output formatThere are three lines of output, and the numbers of the three detection points closest to the citizen are output in turn according to the distance from near to far. ## Sample input 13 2 2 2 2 2 3 2 4
Data
## Sample output 11 2 3
Data
## Sample input 25 0 1 -1 0 0 0 1 0 0 2 -1 2
Data
## Sample output 22 4 1
Data
## Example 2 explanation## Scale and agreement of evaluation casesAll test points meet, 3 ≤ n ≤ 200, all coordinates are integers and the absolute value does not exceed 1000. ## TipsThe distance Di from the citizen to test point i can be calculated by the following formula: Di2=(X−xi)2+(Y−yi)2 |

// Simulation + sorting #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; struct PII { int idx; int x, y; }; const int N = 210; int n, m, T; PII cur; PII a[N]; int getD2(PII c) { return (c.x - cur.x) * (c.x - cur.x) + (c.y - cur.y) * (c.y - cur.y); } bool cmp(PII c, PII d) { if (getD2(c) == getD2(d)) return c.idx < d.idx; return getD2(c) < getD2(d); } int main() { cin >> n >> cur.x >> cur.y; for (int i = 1; i <= n; ++i) { a[i].idx = i; cin >> a[i].x >> a[i].y; } sort(a + 1, a + 1 + n, cmp); for (int i = 1; i <= 3; ++i) { cout << a[i].idx << endl; } return 0; }

# Screening of risk population

Question No.: | 202009-2 |

Test title: | Screening of risk population |

time limit: | 1.0s |

Memory limit: | 256.0MB |

Problem Description: | ## Topic backgroundAfter the outbreak of an epidemic in a certain place, we want to inform all residents who have recently passed through the high-risk area to participate in nucleic acid testing in accordance with the principle of "testing as much as possible". ## Problem descriptionIn order to find out the residents passing through high-risk areas, analyzing location records is a simple and effective method. Specifically, the location record of a resident contains t plane coordinates (x1,y1),(x2,y2),... (xt,yt), where (xi,yi) represents the location of the resident at time i. Consider the location record of a resident. If one of the coordinates is in a rectangle (including boundary), it means that the resident passes through the high-risk area; Further, if k or more consecutive coordinates are located in the rectangle (including the boundary), the resident is considered to have stayed in the high-risk area. It should be noted that when determining passing and staying, we only care about t coordinates in the location record, without considering where the resident is located between i and i+1. Given the scope of the high-risk area and the location records of n residents in the past t times, try to count the number of people passing through the high-risk area and the number of people who have stayed in the high-risk area. ## Input formatEnter a total of n+1 lines. The first line contains seven integers n, k, t, xl, yd, xr, and yu separated by spaces, meaning as described above. The next n lines contain 2t integers separated by spaces, which represent the location records (x1,y1),(x2,y2), (xt,yt) of a resident in the past t times in order. ## Output formatThere are two lines of output, one integer in each line, representing the number of people passing through the high-risk area and the number of people who have stayed in the high-risk area. ## Sample input 15 2 6 20 40 100 80 100 80 100 80 100 80 100 80 100 80 100 80 60 50 60 46 60 42 60 38 60 34 60 30 10 60 14 62 18 66 22 74 26 86 30 100 90 31 94 35 98 39 102 43 106 47 110 51 0 20 4 20 8 20 12 20 16 20 20 20
Data
## Sample output 13 2
Data
## Example 1 DescriptionAs shown by the red mark in the figure below, the first three position records pass through the high-risk area; ## Sample input 21 3 8 0 0 10 10 -1 -1 0 0 0 0 -1 -1 0 0 -1 -1 0 0 0 0
Data
## Sample output 21 0
Data
## Example 2 DescriptionThe location records that it has passed through the high-risk area, but it is located in it for at most two consecutive times, which does not meet the conditions of stay. ## Scale and agreement of evaluation casesAll test points meet 1 ≤ n ≤ 20, 1 ≤ k ≤ t ≤ 103, all coordinates are integers and the absolute value does not exceed 106. |

// Simulation with pits #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int n, m, k, T, t; int xx1, yy1, xx2, yy2; int cur_x, cur_y; int res1, res2; int main() { scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xx1, &yy1, &xx2, &yy2); while (n--) { bool flag1 = false; int f = 0; bool flag2 = false; for (int i = 0; i < t; ++i) { scanf("%d%d", &cur_x, &cur_y); if (cur_y >= yy1 && cur_y <= yy2 && cur_x >= xx1 && cur_x <= xx2) { flag1 = true; f++; if (f >= k) flag2 = true; } else { f = 0; } } if (flag1) res1++; if (flag2) { res2++; } } printf("%d\n%d\n", res1, res2); return 0; }

# Linear classifier

A simple binary classification problem is considered: points on a two-dimensional plane are divided into AA and BB.

The training data contains nn points, in which the second point (1 ≤ i ≤ n1 ≤ i ≤ n) can be expressed as a triple (Xi, Yi, type i) (Xi, Yi, type i), that is, the abscissa, ordinate and category of the point.

On a two-dimensional plane, any line can be represented as θ 0+ θ 1x+ θ 2y=0 θ 0+ θ 1x+ θ 2Y = 0, i.e. by θ 0 θ one θ 0 θ 1 and θ two θ 2 three parameters determine the straight line and meet θ 1, θ two θ 1, θ If 2 is different, it is 00.

Based on these nn known categories of points, we want to find a line on the plane as a linear classifier.

Specifically, this line should perfectly separate the A, BA and B points in the training data, that is, there are only AA points on one side and BB points on the other side.

In this way, for any point of unknown category, we can predict its category according to which side of the line it is located.

In this question, we only need to process mm queries as follows:

Given a straight line, judge whether it can perfectly separate the A, BA and B points in the training data.

#### Input format

Enter n+m+1 lines in total.

The first line contains two positive integers n and m separated by spaces, representing the number of points and queries, respectively.

Input the information of n points from the second line to the n+1 line. Line i+1i+1 (1 ≤ i ≤ n) contains three items xi, yi and typei separated by spaces, representing the abscissa, ordinate and category of the ith point respectively, where the coordinate is an integer and the category is a capital English letter A or B.

Enter m queries from line n+2 to line n+m+1. Line j+n+1 (1 ≤ J ≤ m) contains three integers separated by spaces θ 0 θ 1 and θ 2. Represents the three parameters of the given line in the j-th query.

#### Output format

Output a total of m lines, each line outputs a string.

The string output in line j (1 ≤ J ≤ m) corresponds to the result of the jj query: if A given straight line can perfectly separate two types of points A and B, output Yes, otherwise output No.

#### Data range

The input data ensures that there is no point just falling on a given line;

0 < n ≤ 103, 0 < m ≤ 20, and the number of type A and B points is not 0;

The coordinates of all points and the three parameters of the given line are integers, and the absolute value is ≤ 106;

The coordinates of any two points are not exactly the same.

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-hM6ZDvs9-1631786605219)(CCF CSP.assets/19_6a4a705577-QQ screenshot 20210225130038.png)]

#### Input example:

9 3 1 1 A 1 0 A 1 -1 A 2 2 B 2 3 B 0 1 A 3 1 B 1 3 B 2 0 A 0 2 -3 -3 0 2 -3 1 1

#### Output example:

No No Yes

#### Example explanation

Only the straight line given in the 33rd query can perfectly separate the points of A, BA and B.

[the transfer of external chain pictures fails. The source station may have an anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-llYlPmGO-1631786605222)(CCF CSP.assets/19_e177e4d377-QQ screenshot 20210225130354.png)]

code:

// Mathematics, simulation #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int N = 1010; int n, m, T; struct pp { long long x, y; char t; } a[N]; int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> a[i].x >> a[i].y >> a[i].t; } int s1, s2, s0; while (m--) { cin >> s0 >> s1 >> s2; long long tem = s1 * a[0].x + s2 * a[0].y + s0; bool flagA; bool flag = false; if (a[0].t == 'A')flag = true; if (tem > 0) { if (flag) flagA = true; else flagA = false; }else{ if (flag) flagA = false; else flagA = true; } flag = true; for (int i = 1; i < n; ++i) { tem = s1 * a[i].x + s2 * a[i].y + s0; if (tem > 0) { if ((flagA && a[i].t == 'A') || (!flagA && a[i].t == 'B')) continue; else { flag = false; break; } } else { if ((!flagA && a[i].t == 'A') || (flagA && a[i].t == 'B')) continue; else { flag = false; break; } } } if (flag) puts("Yes"); else puts("No"); } return 0; }

# Sparse vector

For an n-dimensional integer vector v ∈ Zn, its value in the index dimension is recorded as vindex.

Here, we agree that the value of index starts from 1, that is, v=(v1,v2,..., vn).

The following describes a sparse representation of vectors.

If the value of v in only a few dimensions is not 0, it is called sparse vector.

For example, when n=10, v=(0,0,0,5,0,0, − 3,0,0,1) is a sparse vector.

Because there are few non-zero values of sparse vectors, we can save space by storing only non-zero values.

Specifically, each non-zero value can be represented by an (index,value) pair, that is, the value vindex=value ≠ 0 of the vector in the indexindex dimension.

In the above example, v can be expressed as [(4,5), (7, − 3), (10,1)] [(4,5), (7, − 3), (10,1)].

Next, the definition of this sparse representation generalization is given.

- For any n-dimensional integer vector v ∈ Zn, if its value is not 0 in and only in a dimension, it can be uniquely expressed as: [(index1,value1),(index2,value2),..., (indexa,valuea)][(index1,value1),(index2,value2),..., (indexa,valuea)].
- All indexindex es are integers and meet the following requirements: 1 ≤ index1 < index2 <... < indexa ≤ n.
- valuei represents the non-zero value of the vector vv on the corresponding dimension indexi.

The sparse representations of two n-dimensional integer vectors u,v ∈ Zn are given, and their inner products are calculated.

u⋅v=∑i=1nui⋅vi

#### Input format

The first line contains three positive integers n, an, a and bb separated by spaces, where nn represents the dimension of vectors u, vu and v, and aa and bb represent the number of non-zero values contained in the two vectors respectively.

The sparse representation of the input vector uu from the second line to the a+1a+1 line. Line i+1i+1 (1 ≤ i ≤ a1 ≤ i ≤ a) contains two integers indexiindexi and valueivaluei separated by spaces, indicating vindexi=valuei ≠ 0vindexi=valuei ≠ 0.

The sparse representation of the input vector vv from lines a+2a+2 to a+b+1a+b+1. Line j+a+1j+a+1 (1 ≤ J ≤ b1 ≤ J ≤ b) contains two integers indexjindexj and valuejvaluej separated by spaces, indicating vindexj=valuej ≠ 0 vindexj=valuej ≠ 0.

#### Output format

Output an integer representing the inner product u ⋅ vu ⋅ v of vectors uu and vv.

#### Data range

Input data shall ensure 0 < A, B < N0 < A, B < n;

The absolute values | ui|,|vi | ≤ 106 (1 ≤ i ≤ n)|ui|,|vi | ≤ 106 (1 ≤ i ≤ n) of vectors uu and vv in each dimension.

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-EpkNQtun-1631786605226)(CCF CSP.assets/19_409ebaf877-QQ screenshot 20210225163412.png)]

#### Input example:

10 3 44 57 -310 11 104 205 307 40

#### Output example:

-20

#### Example explanation

u=(0,0,0,5,0,0,−3,0,0,1)

v=(10,0,0,20,30,0,40,0,0,0)

u⋅v=5×20+(−3)×40=−20

code:

// Double pointer #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> using namespace std; int n, m, a1, b1, t; vector<pair<int, int>> a; vector<pair<int, int>> b; int main() { cin >> n >> a1 >> b1; for (int i = 0; i < a1; ++i) { cin >> m >> t; a.push_back(make_pair(m, t)); } for (int i = 0; i < b1; ++i) { cin >> m >> t; b.push_back(make_pair(m, t)); } long long res = 0; for (int i = 0, j = 0; i < a1 && j < b1;) { if (a[i].first == b[j].first) { res += (long long)a[i].second * b[j].second; i++, j++; } else { if (a[i].first > b[j].first) j++; else i++; } } cout << res << endl; return 0; }

# Prefix and

## One dimensional prefix sum

S[i] = a[1] + a[2] + ... a[i] a[l] + ... + a[r] = S[r] - S[l - 1]

##### 1230. K-fold interval

Given a sequence of length N, A1,A2,... AN, if the sum of a continuous subsequence Ai,Ai+1,... Aj is a multiple of K, we call this interval [I, J] a k-fold interval.

Can you find out how many K-times intervals there are in the sequence?

Input format:

The first line contains two integers N and K.

Each of the following N lines contains an integer Ai.

Output format:

Output an integer representing the number of K-times intervals.

Data range:

1 ≤ N , K ≤ 100000

1 ≤ Ai ≤ 100000

Input example:

5 2 1 2 3 4 5

Output example:

6

code:

#include<iostream> using namespace std; const int N = 100010; long long int n, k, x, ans = 0; long long int s[N]; long long int res[N]; int main() { cin >> n >> k; res[0] = 1; //s[0]%k=0, so there is already one number with remainder of 0 for (long long int i = 1; i <= n; i++) { cin >> x; s[i] = s[i - 1] + x; ans += res[s[i] % k]; res[s[i] % k]++; } cout << ans << endl; return 0; }

## 2D prefix and

S[i, j] = The first i that 's ok j The sum of all elements in the upper left part of the column grid with(x1, y1)Is the upper left corner,(x2, y2)The sum of the submatrix in the lower right corner is: S[x2, y2] - S[x1 - 1, y2] - S[x2, y1 - 1] + S[x1 - 1, y1 - 1]

# Dichotomy

## Integer dichotomy

bool check(int x) {/* ... */} // Check whether x satisfies a certain property // When the interval [l, r] is divided into [l, mid] and [mid + 1, r], use: int bsearch_1(int l, int r) { while (l < r) { int mid = l + r >> 1; if (check(mid)) r = mid; // check() determines whether the mid satisfies the property else l = mid + 1; } return l; } // The interval [l, r] is divided into [l, mid - 1] and [mid, r]: int bsearch_2(int l, int r) { while (l < r) { int mid = l + r + 1 >> 1; if (check(mid)) l = mid; else r = mid - 1; } return l; }

##### 789. Range of numbers

Given an array of integers of length n in ascending order and q queries.

For each query, the start and end positions of an element k are returned (the positions are counted from 0).

If the element does not exist in the array, "- 1" - 1 "is returned.

Input format:

The first line contains integers n and q, indicating the length of the array and the number of queries.

The second line contains n integers (all in the range of 1 ~ 10000), representing the complete array.

The next q lines, each containing an integer k, represent a query element.

Output format:

There are q rows in total, and each row contains two integers, indicating the starting position and ending position of the element.

If the element does not exist in the array, "- 1" - 1 "is returned.

Data range:

1≤n≤100000,

1≤q≤10000,

1≤k≤10000

Input example:

6 31 2 2 3 3 4345

Output example:

3 45 5-1 -1

code:

#include<iostream> using namespace std; const int N = 100001; int a[N]; int main() { int n, k, x; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < k; i++) { cin >> x; //Find the left endpoint first int l = 0, r = n - 1; while (l < r) { int mid = (l + r) >> 1; if (a[mid] >= x) r = mid; else l = mid + 1; } if (a[r] == x) { cout << r << " "; //Find the right endpoint after finding the left endpoint r = n - 1; while (l < r) { int mid = (l + r + 1) >> 1; if (a[mid] <= x) l = mid; else r = mid - 1; } cout << l << endl; } else//Left endpoint direct output - 1 - 1 not found cout << "-1 -1" << endl; } return 0; }

## Floating point binary

bool check(double x) {/* ... */} // Check whether x satisfies a certain property double bsearch_3(double l, double r) { const double eps = 1e-6; // eps indicates accuracy, which depends on the accuracy requirements of the subject while (r - l > eps) { double mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } return l; }

##### 790. The third root of a number

Given a floating-point number n, find its cubic root.

Input format:

A row containing a floating-point number n.

Output format:

There is one line, including a floating point number, which represents the solution of the problem.

Note that the result retains 6 decimal places.

Data range:

−10000 ≤ n ≤ 10000

Input example:

1000.00

Output example:

10.000000

code:

#include<iostream> #include<cstdio> using namespace std; int main() { double n; bool fu = false; cin >> n; if (n < 0) { fu = true; n *= (-1); } double l = 0, r = 10001; double mid; while (r - l > 1e-8) { mid = (l + r) / 2; if (mid * mid * mid > n) r = mid; else l = mid; } if (fu) mid *= (-1); printf("%.6f", mid); return 0; }

# Double pointer

for (int i = 0, j = 0; i < n; i ++ ) { while (j < i && check(i, j)) j ++ ; // Logic of specific problems } Classification of frequently asked questions: (1) For a sequence, maintain an interval with two pointers (2) For two sequences, maintain a certain order, such as merging two ordered sequences in merge sort