Nanyang OJ Soldier kills (ii) title NYOJ data structure

Source: Internet
Author: User



/* Soldier kills (ii)
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 5
Describe
South Generals have n soldiers, numbered 1 to N, each of these soldiers is known for their kills.

The staff is the South General's military advisers, the South General often want to know the number m to nth soldier's total kills, please help the handyman to answer the South General.

The South General's inquiry after the soldier I may also kill the Q person, after the South General asked again, need to consider the new kill number.

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 represents the number of instructions. (1<m<100000)
The next line is n integers, and AI represents the number of soldier kills. (0<=ai<=100)
The subsequent M-line is an instruction that contains a string and two integers, first a string, and if the string query indicates that the South General has a query operation, followed by the two integer m,n, indicating the start and end of the query soldier number; If the string add is followed by the two integer I , A (1<=i<=n,1<=a<=100), which indicates that the number of new kills for the first soldier is a.

Output
For each query, output an integer r that represents the total number of kills of soldier number m to nth soldier, one row per set of outputs
Sample input
5 6 1 2 3 4 5 Query 1 3 Add 1 2 Query 1 3 Add 2 3 Query 1 2 query 1 5
Sample output
6 8 8 20
Code: */
Normal timeout code
#include <stdio.h>
#include <stdlib.h>
#define N 1000001
int s[n],m,n,k,l;
void Ji ()
{
for (int i=k;i<=n;i++)
S[i]+=l;
}
void Chu ()
{printf ("%d\n", S[l]-s[k-1]);}
int main ()
{
scanf ("%d%d", &n,&m);
s[0]=0;
for (int i=1;i<=n;i++)
{
scanf ("%d", &s[i]);
S[I]+=S[I-1];
}
Char h[100];
for (int j=0;j<m;j++)
{
scanf ("%s%d%d", h,&k,&l);
if (h[0]== ' Q ') printf ("%d\n", s[l]-s[k-1]);
if (h[0]== ' A ') Ji ();
}
return 0;
}
Use a tree array to solve the problem of changing arrays dynamically, because a soldier in front changes the number of kills, followed by changes
#include <stdio.h>
int a[1000010],n;
int lowbit (int pat)
{
Return pat& (-PAT);//The return value is a K-power of 2, and K is the number of the Pat converted to 2 binary from right to left 0
}
void pus (int pat,int numb)
{
while (pat<=n)
{
A[pat]+=numb;
Pat+=lowbit (PAT);
}
}
int getsum (int pat)
{
int sum=0;
while (pat>=1)
{
SUM+=A[PAT];
Pat-=lowbit (PAT);
}
return sum;
}
int main ()
{
int m,i,from,to,num;
Char str[10];
scanf ("%d%d", &n,&m);
for (I=1;i<=n;++i)
{
scanf ("%d", &num);
Pus (i,num);
}
while (m--)
{
scanf ("%s%d%d", str,&from,&to);
if (str[0]== ' Q ')
printf ("%d\n", Getsum (To)-getsum (from-1));
Else
{
Pus (from,to);
}
}
return 0;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nanyang OJ Soldier kills (ii) title NYOJ data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.