1 solutions
-
0
#include <bits/stdc++.h> using namespace std; const int MAXNUMBER=6e5+1,ADD=1,QUERY=2; int ARRAYLENGTH,OPERATIONCOUNT; int Array[MAXNUMBER],Array1[MAXNUMBER]; int lowbit(int Parameter){ return Parameter&-Parameter; } void init(){ for(int i=1;i<=ARRAYLENGTH;i++){ Array[i]=Array1[i]-Array1[i-lowbit(i)]; } } void readIn(int ReadCount){ int Temporary; scanf("%d",&Array1[1]); for(int i=2;i<=ReadCount;i++){ scanf("%d",&Temporary); Array1[i]=Array1[i-1]+Temporary; } } void add(int Slot,int Parameter){ for(int i=Slot;i<=ARRAYLENGTH;i+=lowbit(i)){ Array[i]+=Parameter; } } int sum(int Slot){ int Sum=0; for(int i=Slot;i>0;i-=lowbit(i)){ Sum+=Array[i]; } return Sum; } int query(int Left,int Right){ return sum(Right)-sum(Left-1); } void run(int RunCount){ int Operation,Parameter1,Parameter2; for(int i=1;i<=RunCount;i++){ cin>>Operation>>Parameter1>>Parameter2; if(Operation==ADD){ add(Parameter1,Parameter2); } if(Operation==QUERY){ printf("%d\n",query(Parameter1,Parameter2)); } } } int main(){ scanf("%d",&ARRAYLENGTH); scanf("%d",&OPERATIONCOUNT); readIn(ARRAYLENGTH); init(); run(OPERATIONCOUNT); return 0; }
- 1
Information
- ID
- 1831
- Time
- 1000ms
- Memory
- 512MiB
- Difficulty
- 3
- Tags
- # Submissions
- 38
- Accepted
- 17
- Uploaded By