Luogu3374
已知一个数列,你需要进行下面两种操作:
1.将某一个数加上x
2.求出某区间每一个数的和
#define _FILE_ ""
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<vector>
#include<stdlib.h>
#define fr(i,a,b) for(int i=a,_end_=b;i<=_end_;i++)
#define fd(i,a,b) for(int i=a,_end_=b;i>=_end_;i--)
#define frei(s) freopen(s,"r",stdin)
#define freo(s) freopen(s,"w",stdout)
#define ll long long
using namespace std;
#define rt return
#define inf 0x3f3f3f3f
#define pc(x) putchar(x)
#define spc putchar(' ')
#define gc getchar()
#define ln pc('\n')
#define writeint(x) printf("%d",x)
#define lowbit(x) (x&(-x))
int readuint(){
int s=0;
char c=getchar();
while(c<'0'||c>'9')c=gc;
while(c>=48&&c<='9'){
s=s*10+c-48;
c=gc;
}
rt s;
}
int readint(){
int s=0,k=1;
char c=getchar();
while((c<'0'||c>'9')&&c!='-')c=gc;
if(c=='-'){
k=-1;
c=gc;
}
while(c>=48&&c<='9'){
s=s*10+c-48;
c=gc;
}
rt s*k;
}
void OPENFILE(){
char FILENAME[50];
if(strlen(_FILE_)==0)rt;
sprintf(FILENAME,"%s.in",_FILE_);
frei(FILENAME);
sprintf(FILENAME,"%s.out",_FILE_);
freo(FILENAME);
}
int n,m,d,b,a[10000010],c[1000010];
int main(){
OPENFILE();
n=readuint();
m=readuint();
fr(i,1,n)
{
a[i]=readint();
d=i;
while(d<=n*2)
{
c[d]+=a[i];
d+=lowbit(d);
}
}
while(m--)
if(readuint()==1)
{
d=readuint(),b=readint();
while(d<=n*2)
{
c[d]+=b;
d+=lowbit(d);
}
}
else
{
b=0;
d=readuint()-1;
while(d)
{
b-=c[d];
d-=lowbit(d);
}
d=readuint();
while(d)
{
b+=c[d];
d-=lowbit(d);
}
writeint(b);
ln;
}
rt 0;
}
Luogu3368
已知一个数列,你需要进行下面两种操作:
1.将某区间每一个数数加上x
2.求出某一个数的和
#define _FILE_ ""
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<vector>
#include<stdlib.h>
#define fr(i,a,b) for(int i=a,_end_=b;i<=_end_;i++)
#define fd(i,a,b) for(int i=a,_end_=b;i>=_end_;i--)
#define frei(s) freopen(s,"r",stdin)
#define freo(s) freopen(s,"w",stdout)
#define ll long long
using namespace std;
#define rt return
#define inf 0x3f3f3f3f
#define pc(x) putchar(x)
#define spc putchar(' ')
#define gc getchar()
#define ln pc('\n')
#define writeint(x) printf("%d",x)
#define lowbit(x) (x&(-x))
int readuint(){
int s=0;
char c=getchar();
while(c<'0'||c>'9')c=gc;
while(c>=48&&c<='9'){
s=s*10+c-48;
c=gc;
}
rt s;
}
int readint(){
int s=0,k=1;
char c=getchar();
while((c<'0'||c>'9')&&c!='-')c=gc;
if(c=='-'){
k=-1;
c=gc;
}
while(c>=48&&c<='9'){
s=s*10+c-48;
c=gc;
}
rt s*k;
}
void OPENFILE(){
char FILENAME[50];
if(strlen(_FILE_)==0)rt;
sprintf(FILENAME,"%s.in",_FILE_);
frei(FILENAME);
sprintf(FILENAME,"%s.out",_FILE_);
freo(FILENAME);
}
int c[10000010],a[10000010];
int n,m;
void add(int x,int y)
{
while(x)
{
c[x]+=y;
x-=lowbit(x);
}
}
int query(int x)
{
int _=0;
while(x<=n*2)
{
_+=c[x];
x+=lowbit(x);
}
rt _;
}
int main(){
OPENFILE();
n=readuint();
m=readuint();
fr(i,1,n)
a[i]=readint();
while(m--)
if(readuint()==1)
{
int _=readuint(),__=readuint(),___=readint();
add(_-1,-___);
add(__,___);
}
else
{
int _=readuint();
writeint(query(_)+a[_]);
ln;
}
rt 0;
}