Title Description
Title, you need to perform the following two operations, known as a sequence:
1. Add a number to the X
2. Find out the amount of each number in a range and
Input/output format
Input format:
The first line contains two integers n, M, respectively, indicating the number of numbers and the total number of operations.
The second line contains n space-delimited integers, where the I number represents the initial value of item I of a sequence.
The next M line contains 3 or 4 integers, representing an operation, as follows:
Action 1: Format: 1 x k Meaning: Add x number plus K
Action 2: Format: 2 x y meaning: the and of each number in the output interval [x, y]
Output format:
The output contains several line integers, which is the result of all Operation 2.
Input and Output Sample input example # #:
5 51 5 4 2 31 1 32 2 51 3-11 4 22 1 4
Sample # # of output:
1416
Description
Time limit: 1000ms,128m
Data size:
For 30% data: n<=8,m<=10
For 70% data: n<=10000,m<=10000
For 100% data: n<=500000,m<=500000
Sample Description:
Therefore, the output results 14, 16
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 intn,m,a[500010],tarr[500010];6 voidAddintPosintdel)7 {8 9 while(pos<=N)Ten { Onetarr[pos]+=del; Apos+= (pos^ (pos-1)) &Pos; - } - the } - intQry (intPOS) - { - intsum=0; + while(POS) - { +sum+=Tarr[pos]; Apos-= (pos^ (pos-1)) &Pos; at } - returnsum; - } - intMain () - { -Cin>>n>>m; in for(intI=1; i<=n;i++) -scanf"%d",&a[i]); to for(intI=1; i<=n;i++) + Add (I,a[i]); - intflag,x,y; the for(intI=1; i<=m;i++) * { $scanf"%d%d%d",&flag,&x,&y);Panax Notoginseng if(flag==1) Add (x, y); - Elseprintf"%d\n", Qry (y)-qry (X-1)); the } + A return 0; the}
Idea: Tree-like array back version ~ ~ ~
Rokua P3374 "template" Tree array 1