两份代码,一份#define int long long,一份没有,为什么挂?哪个变量需要long long?求助。
AC:
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define per(i,n) for(int i=(n)-1;i>=0;--i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define per1(i,n) for(int i=(n);i>=1;--i)
#define repk(i,a,b) for(int i=(a);i<=(b);++i)
#define perk(i,a,b) for(int i=(a);i>=(b);--i)
#define rep0(i,a,b) for(int i=(a);i<(b);++i)
#define pb push_back
#define mp make_pair
#define g(x) cout<<#x<<'='<<x<<endl
#define nextp next_permutation
#define pq priority_queue
#define fi first
#define se second
#define int long long
typedef long long ll;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
//ios_base::sync_with_stdio(false);加快cin
const int N=100005;
ll a[N];
struct node{
int l;
int r;
ll s;
ll lz;
};
node val[N<<2];
void build(int l,int r,int pos)
{
val[pos].l=l,val[pos].r=r;
if(l==r) val[pos].s=a[l];
else
{
int m=(l+r)>>1;
build(l,m,pos<<1);
build(m+1,r,pos<<1|1);
val[pos].s=val[pos<<1].s+val[pos<<1|1].s;
}
}
void pushdown(int x)
{
if(val[x].lz)
{
val[x<<1].s+=(val[x<<1].r-val[x<<1].l+1)*val[x].lz;
val[x<<1|1].s+=(val[x<<1|1].r-val[x<<1|1].l+1)*val[x].lz;
val[x<<1].lz+=val[x].lz;
val[x<<1|1].lz+=val[x].lz;
val[x].lz=0;
}
}
void upd(int l,int r,ll x,int pos)
{
if(l<=val[pos].l&&val[pos].r<=r)
{
val[pos].s+=(val[pos].r-val[pos].l+1)*x;
val[pos].lz+=x;
}
else
{
pushdown(pos);
int m=(val[pos].l+val[pos].r)>>1;
if(m>=l) upd(l,r,x,pos<<1);
if(m<r) upd(l,r,x,pos<<1|1);
val[pos].s=val[pos<<1].s+val[pos<<1|1].s;
}
}
ll query(int l,int r,int pos)
{
if(l<=val[pos].l&&val[pos].r<=r)
{
return val[pos].s;
}
pushdown(pos);
int m=(val[pos].l+val[pos].r)>>1,s=0;
if(m>=l) s+=query(l,r,pos<<1);
if(m<r) s+=query(l,r,pos<<1|1);
return s;
}
int n,q,t,x,y;
ll k;
signed main()
{
ios_base::sync_with_stdio(false);
cin>>n>>q;
rep1(i,n) cin>>a[i];
build(1,n,1);
while(q--)
{
cin>>t;
if(t==1)
{
cin>>x>>y>>k;
upd(x,y,k,1);
}
else
{
cin>>x>>y;
cout<<query(x,y,1)<<endl;
}
}
return 0;
}
/* things to check
1. int overflow or long long memory need
2. recursion/array/binary search/dp bounds
3. precision
4. forever loop
5. special cases(n=1,bounds)
*/
70pts:
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define per(i,n) for(int i=(n)-1;i>=0;--i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define per1(i,n) for(int i=(n);i>=1;--i)
#define repk(i,a,b) for(int i=(a);i<=(b);++i)
#define perk(i,a,b) for(int i=(a);i>=(b);--i)
#define rep0(i,a,b) for(int i=(a);i<(b);++i)
#define pb push_back
#define mp make_pair
#define g(x) cout<<#x<<'='<<x<<endl
#define nextp next_permutation
#define pq priority_queue
#define fi first
#define se second
typedef long long ll;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
//ios_base::sync_with_stdio(false);加快cin
const int N=100005;
ll a[N];
struct node{
int l;
int r;
ll s;
ll lz;
};
node val[N<<2];
void build(int l,int r,int pos)
{
val[pos].l=l,val[pos].r=r;
if(l==r) val[pos].s=a[l];
else
{
int m=(l+r)>>1;
build(l,m,pos<<1);
build(m+1,r,pos<<1|1);
val[pos].s=val[pos<<1].s+val[pos<<1|1].s;
}
}
void pushdown(int x)
{
if(val[x].lz)
{
val[x<<1].s+=(val[x<<1].r-val[x<<1].l+1)*val[x].lz;
val[x<<1|1].s+=(val[x<<1|1].r-val[x<<1|1].l+1)*val[x].lz;
val[x<<1].lz+=val[x].lz;
val[x<<1|1].lz+=val[x].lz;
val[x].lz=0;
}
}
void upd(int l,int r,ll x,int pos)
{
if(l<=val[pos].l&&val[pos].r<=r)
{
val[pos].s+=(val[pos].r-val[pos].l+1)*x;
val[pos].lz+=x;
}
else
{
pushdown(pos);
int m=(val[pos].l+val[pos].r)>>1;
if(m>=l) upd(l,r,x,pos<<1);
if(m<r) upd(l,r,x,pos<<1|1);
val[pos].s=val[pos<<1].s+val[pos<<1|1].s;
}
}
ll query(int l,int r,int pos)
{
if(l<=val[pos].l&&val[pos].r<=r)
{
return val[pos].s;
}
pushdown(pos);
int m=(val[pos].l+val[pos].r)>>1,s=0;
if(m>=l) s+=query(l,r,pos<<1);
if(m<r) s+=query(l,r,pos<<1|1);
return s;
}
int n,q,t,x,y;
ll k;
int main()
{
ios_base::sync_with_stdio(false);
cin>>n>>q;
rep1(i,n) cin>>a[i];
build(1,n,1);
while(q--)
{
cin>>t;
if(t==1)
{
cin>>x>>y>>k;
upd(x,y,k,1);
}
else
{
cin>>x>>y;
cout<<query(x,y,1)<<endl;
}
}
return 0;
}
/* things to check
1. int overflow or long long memory need
2. recursion/array/binary search/dp bounds
3. precision
4. forever loop
5. special cases(n=1,bounds)
*/