因为时间不够了,所以——写一下模板
int lowbit(int x) {
return x & (-x);
}
void Update(int x,int y) {
for(;x <= n;x += lowbit(x))
Bit[x] += y;
}
int Ask(int x) {
int ans = 0;
for(;x;x -= lowbit(x))
ans += Bit[x];
return ans;
}
#include
#include
#include
using namespace std;
const int MAXN = 1e6 + 5;
int a[MAXN],Bit[MAXN],l,r,x,n,q;
int lowbit(int x) {
return x & (-x);
}
void Update(int x,int y) {
for(;x <= n;x += lowbit(x))
Bit[x] += y;
}
int Ask(int x) {
int ans = 0;
for(;x;x -= lowbit(x))
ans += Bit[x];
return ans;
}
int main() {
scanf("%d %d",&n,&q);
for(int i = 1;i <= n;i ++) {
scanf("%d",&a[i]);
Update(i,a[i]);
}
for(int i = 1;i <= q;i ++) {
int type;
scanf("%d",&type);
if(type == 1) {
int l,x;
scanf("%d %d",&l,&x);
Update(l,x);
}
else {
int l,r;
scanf("%d %d",&l,&r);
int ans = Ask(r) - Ask(l - 1);
printf("%d\n",ans);
}
}
return 0;
}
#include
#include
#include
#define ll long long
using namespace std;
const ll MAXN = 1e6 + 5;
ll Bit[MAXN],cf[MAXN],n,q,a[MAXN];
ll lowbit(ll x) {
return x & (-x);
}
void Update(ll x,ll y) {
for(;x <= n;x += lowbit(x))
Bit[x] += y;
}
ll ask(ll x) {
ll ans = 0;
for(;x;x -= lowbit(x))
ans += Bit[x];
return ans;
}
int main() {
scanf("%lld %lld",&n,&q);
for(ll i = 1;i <= n;i ++) {
scanf("%lld",&a[i]);
}
for(ll i = 1;i <= q;i ++) {
ll type;
scanf("%lld",&type);
if(type == 1) {
ll l,r,x;
scanf("%lld %lld %lld",&l,&r,&x);
Update(l,x);
Update(r + 1,-x);
}
else {
ll l;
scanf("%lld",&l);
printf("%lld\n",ask(l) + a[l]);
}
}
return 0;
}
#include
#include
#include
using namespace std;
const int MAXN = 1e6 + 5;
int Bit[5][MAXN],sum[MAXN],n,q;
int lowbit(int x) {
return x & (-x);
}
void Update(int type,int x,int y) {
for(;x <= n;x += lowbit(x))
Bit[type][x] += y;
}
int ask(int type,int x) {
int ans = 0;
for(;x;x -= lowbit(x))
ans += Bit[type][x];
return ans;
}
int main() {
scanf("%d %d",&n,&q);
for(int i = 1;i <= n;i ++) {
int x;
scanf("%d",&x);
sum[i] = sum[i - 1] + x;
}
for(int i = 1;i <= q;i ++) {
int type;
scanf("%d",&type);
if(type == 1) {
int l,r,x;
scanf("%d %d %d",&l,&r,&x);
Update(0,l,x);
Update(0,r + 1,-x);
Update(1,l,l * x);
Update(1,r + 1,-(r + 1) * x);
}
else {
int l,r;
scanf("%d %d",&l,&r);
int num1 = sum[r] + (r + 1) * ask(0,r) - ask(1,r);
int num2 = sum[l - 1] + l * ask(0,l - 1) - ask(1,l - 1);
printf("%d\n",num1 - num2);
}
}
return 0;
}
#include
#include
#include
#define ll long long
using namespace std;
const ll MAXN = 1e6 + 5;
ll a[MAXN],b[MAXN],c[MAXN],Bit[MAXN],n,ans,m;
ll lowbit(ll x) {
return x & (-x);
}
void Update(ll x,ll y) {
for(;x <= n;x += lowbit(x))
Bit[x] += y;
}
ll Sum(ll x) {
ll ans = 0;
for(;x;x -= lowbit(x))
ans += Bit[x];
return ans;
}
int main() {
scanf("%lld",&n);
for(ll i = 1;i <= n;i ++)
scanf("%lld",&a[i]),b[i] = a[i];
sort(b + 1,b + n + 1);
int m = unique(b + 1,b + n + 1) - b - 1;
for(int i = 1;i <= n;i ++) {
c[i] = lower_bound(b + 1,b + m + 1,a[i]) - b;
}
for(ll i = n;i >= 1;i --) {
ans += Sum(c[i] - 1);
Update(c[i],1);
}
printf("%lld",ans);
return 0;
}
#include
#include
#include
using namespace std;
const long long MAXN = 1e4 + 5;
long long a[MAXN][MAXN],Bit[MAXN][MAXN],n,m;
long long lowbit(long long x) {
return x & (-x);
}
void Update(long long x,long long y,long long z) {
for(;x <= n;x += lowbit(x)) {
for(long long j = y;j <= m;j += lowbit(j)) {
Bit[x][j] += z;
}
}
}
long long Sum(long long x,long long y) {
long long ans = 0;
for(;x;x -= lowbit(x)) {
for(long long j = y;j;j -= lowbit(j)) {
ans += Bit[x][j];
}
}
return ans;
}
int main() {
scanf("%lld %lld",&n,&m);
long long type;
while(scanf("%lld",&type) != EOF) {
if(type == 1) {
long long x,y,k;
scanf("%lld %lld %lld",&x,&y,&k);
Update(x,y,k);
}
else {
long long a,b,c,d;
scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
printf("%lld\n",Sum(a - 1,b - 1) + Sum(c,d) - Sum(a - 1,d) - Sum(c,b - 1));
}
}
return 0;
}
#include
#include
#include
#define ll long long
using namespace std;
const ll MAXN = 2e3 + 55;
ll n,m,Bit1[MAXN][MAXN],Bit2[MAXN][MAXN],Bit3[MAXN][MAXN],Bit4[MAXN][MAXN];
ll lowbit(ll x) {
return x & (-x);
}
void Update(ll x,ll y,ll z) {
ll p = x;
for(;x <= n;x += lowbit(x)) {
for(ll j = y;j <= n;j += lowbit(j)) {
Bit1[x][j] += z;
Bit2[x][j] += z * p;
Bit3[x][j] += z * y;
Bit4[x][j] += p * y * z;
}
}
}
ll Sum(ll x,ll y) {
ll ans = 0,p = x;
for(;x;x -= lowbit(x)) {
for(ll j = y;j;j -= lowbit(j)) {
ans += (p + 1) * (y + 1) * Bit1[x][j] - (y + 1) * Bit2[x][j] - (p + 1) * Bit3[x][j] + Bit4[x][j];
}
}
return ans;
}
int main() {
scanf("%lld %lld",&n,&m);
ll type;
while(scanf("%lld",&type) != EOF) {
if(type == 1) {
ll a,b,c,d,x;
scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&x);
Update(a,b,x);
Update(c + 1,b,-x);
Update(a,d + 1,-x);
Update(c + 1,d + 1,x);
}
else {
ll a,b,c,d;
scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
printf("%lld\n",Sum(a - 1,b - 1) - Sum(a - 1,d) - Sum(c,b - 1) + Sum(c,d));
}
}
return 0;
}
实际做题需要很多技巧,需要灵机应变,不能死记模板。本文只包括了模板,给初学者的。
又水了一篇题解
END