点击跳转
动态开点线段树裸题
我这里使用了标记永久化的思想,避免下放标记,这样可以节约时间和空间
#include
#include
#include
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct DynamicSegmentTree
{
ll tot, ch[6000000][2], sum[6000000], add[6000000]; //sum是后代节点的sum之和,add是当前节点上的标记
ll New(ll v)
{
add[++tot]=v;
return tot;
}
void upd(ll o, ll l, ll r)
{
ll mid(l+r>>1);
sum[o] = add[ch[o][0]]*(mid-l+1) + add[ch[o][1]]*(r-mid) + sum[ch[o][0]] + sum[ch[o][1]];
}
void init()
{
ll i;
rep(i,1,tot)ch[i][0]=ch[i][1]=sum[i]=add[i]=0;
tot=1;
}
ll Add(ll o, ll l, ll r, ll tarl, ll tarr, ll v)
{
ll mid(l+r>>1), x;
if(!o)o=New(0);
if(tarl<=l and r<=tarr){add[o]+=v;return o;}
if(tarl<=mid)ch[o][0]=Add(ch[o][0],l,mid,tarl,tarr,v);
if(tarr>mid)ch[o][1]=Add(ch[o][1],mid+1,r,tarl,tarr,v);
upd(o,l,r);
return o;
}
ll qsum(ll o, ll l, ll r, ll ql, ll qr, ll S) //S是从当前节点到根节点的标记之和
{
ll mid(l+r>>1), ret(0);
if(!o)return (min(r,qr)-max(l,ql)+1)*S; //当前节点为空
if(ql<=l and r<=qr)return (S+add[o])*(r-l+1)+sum[o];
if(ql<=mid)ret+=qsum(ch[o][0],l,mid,ql,qr,S+add[o]);
if(qr>mid)ret+=qsum(ch[o][1],mid+1,r,ql,qr,S+add[o]);
return ret;
}
}segtree;
int main()
{
ll T=read(), n, maxR=2e9;
while(T--)
{
segtree.init();
n=read();
ll i, lastans=0, M=19980105;
rep(i,1,n)
{
ll tmp = lastans%M;
ll l=read()^tmp, r=read()^tmp, x=read(), ql=read()^tmp, qr=read()^tmp;
if(l>r)swap(l,r);
if(ql>qr)swap(ql,qr);
segtree.Add(1,0,maxR,l,r,x);
lastans = segtree.qsum(1,0,maxR,ql,qr,0);
printf("%lld\n",lastans);
}
}
return 0;
}