soldier Kills (i)Time limit: Ms | Memory limit: 65535 KB Difficulty: 3
Describe
South Generals have n soldiers, numbered 1 to N, each of these soldiers is known for their kills.
The boy is the South General's military advisers, the South General now want to know the number M to No. N soldiers total kills, please help the handyman to answer the South General.
Note that the South General may ask many questions.
Input
Only one set of test data
The first line is two integer n,m, where N represents the number of soldiers (1<n<1000000), and M indicates the number of times the South General inquired (1<M<100000)
The next line is n integers, and AI represents the number of soldier kills. (0<=ai<=100)
The subsequent M-line has two integer m,n per line, indicating that the general South wants to know the total number of kills (1<=m,n<=n) of soldiers from number m to nth.
Output
For each query, the total number of kills is output
One row per output
Sample input
5 2
1 2 3) 4 5
1 3
2 4
Sample output
6
9
To give you an interval [1,n], each point has a value, and then the M query.
The number of each query interval [x, y].
Idea: Although recursion directly to the first N and, but still want to use a tree-like array method.
Note: query (), ask for the top N and
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;int c[1000010],lowbit[1000010],num[1000010],n,m;int query (int p) { int sum = 0; while (P > 0) { sum + = c[p]; P-= lowbit[p]; } return sum;} int main () { scanf ("%d%d", &n,&m); for (int i = 1; I <= N; i++) lowbit[i] = i& (i^ (i-1)); for (int i = 1; I <= N; i++) { scanf ("%d", &num[i]); Num[i] + = num[i-1]; C[i] = Num[i]-num[i-lowbit[i]; } while (m--) { int a, b; scanf ("%d%d", &a,&b); printf ("%d\n", query (b)-query (A-1)); } return 0;}
NYOJ108 soldier Kills (a) "Tree array"