#include <cstdio>
const int sizeOfNumber=;
const int sizeOfSeg=; inline int max(int, int);
inline int getint();
inline void putint(int); struct node
{
int lmax, rmax, smax, ssum;
inline node(int=);
};
inline node merge(node, node); struct seg
{
node data;
seg * l, * r;
inline void maintain();
};
seg memory[sizeOfSeg], * port=memory;
inline seg * newseg();
seg * build(int, int);
node query(seg * , int, int, int, int); int n, m;
int a[sizeOfNumber];
seg * t; int main()
{
n=getint();
for (int i=;i<=n;i++)
a[i]=getint();
t=build(, n); m=getint();
for (int i=;i<=m;i++)
{
int x=getint(), y=getint();
putint(query(t, , n, x, y).smax);
} return ;
} inline int max(int x, int y)
{
return x>y?x:y;
}
inline int getint()
{
register int num=;
register char ch=, last;
do last=ch, ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
if (last=='-') num=-num;
return num;
}
inline void putint(int num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
if (num<) putchar('-'), num=-num;
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
} inline node::node(int x)
{
lmax=rmax=smax=ssum=x;
}
inline node merge(node a, node b)
{
node c;
c.ssum=a.ssum+b.ssum;
c.lmax=max(a.lmax, a.ssum+b.lmax);
c.rmax=max(a.rmax+b.ssum, b.rmax);
c.smax=max(a.smax, b.smax);
c.smax=max(c.smax, a.rmax+b.lmax);
return c;
} inline seg * newseg()
{
seg * ret=port++;
return ret;
}
inline void seg::maintain()
{
this->data=merge(this->l->data, this->r->data);
}
inline seg * build(int l, int r)
{
seg * t=newseg();
int m=(l+r)>>; if (l==r)
t->data=node(a[m]);
else
{
t->l=build(l, m);
t->r=build(m+, r);
t->maintain();
} return t;
}
node query(seg * t, int l, int r, int ql, int qr)
{
node ret;
int m=(l+r)>>; if (l==ql && r==qr) ret=t->data;
else
{
if (qr<=m) ret=query(t->l, l, m, ql, qr);
else if (ql>m) ret=query(t->r, m+, r, ql, qr);
else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+, r, m+, qr));
} return ret;
}

GSS1

 #include <cstdio>
#include <cstring>
#include <algorithm>
const int sizeOfNumber=;
const int sizeOfQuestion=;
const int sizeOfSeg=; inline int max(int, int);
inline int getint();
inline void putint(int); struct seg
{
int ssum, lmax;
int flag, maxflag;
seg * l, * r;
inline void pushdown();
inline void maintain();
};
seg memory[sizeOfSeg], * port=memory;
inline seg * newseg();
seg * build(int, int);
void update(seg * , int, int, int, int, int);
int query(seg * , int, int, int, int); int n, q;
int a[sizeOfNumber], p[sizeOfNumber];
int d[sizeOfQuestion], l[sizeOfQuestion], r[sizeOfQuestion];
int ans[sizeOfQuestion];
seg * t;
inline bool cmpForQuestion(int, int);
inline bool cmpForDiscrete(int, int);
inline void prepare(); int main()
{
n=getint();
for (int i=;i<=n;i++)
a[i]=getint();
prepare();
t=build(, n);
q=getint();
for (int i=;i<=q;i++)
l[i]=getint(), r[i]=getint();
for (int i=;i<=q;i++)
d[i]=i;
std::sort(d+, d+q+, cmpForQuestion); int j=;
for (int i=;i<=n;i++)
{
update(t, , n, p[i]+, i, a[i]);
for ( ;j<=q && r[d[j]]==i;j++)
ans[d[j]]=query(t, , n, l[d[j]], r[d[j]]);
} for (int i=;i<=q;i++)
putint(ans[i]); return ;
} inline int max(int x, int y)
{
return x>y?x:y;
}
inline int getint()
{
register int num=;
register char ch=, last;
do last=ch, ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
if (last=='-') num=-num;
return num;
}
inline void putint(int num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
if (num<) putchar('-'), num=-num;
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
} inline void seg::pushdown()
{
this->l->maxflag=max(this->l->maxflag, this->l->flag+this->maxflag);
this->r->maxflag=max(this->r->maxflag, this->r->flag+this->maxflag);
this->l->flag+=this->flag;
this->r->flag+=this->flag;
this->l->lmax=max(this->l->lmax, this->l->ssum+this->maxflag);
this->r->lmax=max(this->r->lmax, this->r->ssum+this->maxflag);
this->l->ssum+=this->flag;
this->r->ssum+=this->flag;
this->flag=;
this->maxflag=;
}
inline void seg::maintain()
{
this->ssum=max(this->l->ssum, this->r->ssum);
this->lmax=max(this->l->lmax, this->r->lmax);
}
inline seg * newseg()
{
seg * ret=port++;
return ret;
}
seg * build(int l, int r)
{
seg * t=newseg();
int m=(l+r)>>;
if (l==r) return t;
t->l=build(l, m);
t->r=build(m+, r);
return t;
}
void update(seg * t, int l, int r, int ql, int qr, int v)
{
if (l==ql && r==qr)
{
t->ssum+=v;
t->lmax=max(t->lmax, t->ssum);
t->flag+=v;
t->maxflag=max(t->maxflag, t->flag);
}
else
{
int m=(l+r)>>;
t->pushdown();
if (qr<=m) update(t->l, l, m, ql, qr, v);
else if (ql>m) update(t->r, m+, r, ql, qr, v);
else update(t->l, l, m, ql, m, v), update(t->r, m+, r, m+, qr, v);
t->maintain();
}
}
int query(seg * t, int l, int r, int ql, int qr)
{
int ret=; if (l==ql && r==qr)
ret=t->lmax;
else
{
int m=(l+r)>>;
t->pushdown();
if (qr<=m) ret=query(t->l, l, m, ql, qr);
else if (ql>m) ret=query(t->r, m+, r, ql, qr);
else ret=max(query(t->l, l, m, ql, m), query(t->r, m+, r, m+, qr));
t->maintain();
} return ret;
} inline bool cmpForQuestion(int i, int j)
{
return r[i]<r[j];
}
inline bool cmpForDiscrete(int i, int j)
{
return a[i]<a[j];
}
inline void prepare()
{
static int d[sizeOfNumber], l[sizeOfNumber];
int m, t; for (int i=;i<=n;i++)
l[i]=i;
std::sort(l+, l+n+, cmpForDiscrete); m=, t=a[l[m]];
d[l[]]=;
for (int i=;i<=n;i++)
{
if (a[l[i]]>t)
++m, t=a[l[i]];
d[l[i]]=m;
} memset(l, , sizeof(l));
for (int i=;i<=n;i++)
{
p[i]=l[d[i]];
l[d[i]]=i;
}
}

GSS2

 #include <cstdio>
const int sizeOfNumber=;
const int sizeOfSeg=; inline int max(int, int);
inline int getint();
inline void putint(int); struct node
{
int lmax, rmax, smax, ssum;
inline node(int=);
};
inline node merge(node, node); struct seg
{
node data;
seg * l, * r;
inline void maintain();
};
seg memory[sizeOfSeg], * port=memory;
inline seg * newseg();
seg * build(int, int);
void update(seg * , int, int, int);
node query(seg * , int, int, int, int); int n, m;
int a[sizeOfNumber];
seg * t; int main()
{
n=getint();
for (int i=;i<=n;i++)
a[i]=getint();
t=build(, n); m=getint();
for (int i=;i<=m;i++)
{
int k=getint(), x=getint(), y=getint();
if (k==)
putint(query(t, , n, x, y).smax);
else
{
a[x]=y;
update(t, , n, x);
}
} return ;
} inline int max(int x, int y)
{
return x>y?x:y;
}
inline int getint()
{
register int num=;
register char ch=, last;
do last=ch, ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
if (last=='-') num=-num;
return num;
}
inline void putint(int num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
if (num<) putchar('-'), num=-num;
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
} inline node::node(int x)
{
lmax=rmax=smax=ssum=x;
}
inline node merge(node a, node b)
{
node c;
c.ssum=a.ssum+b.ssum;
c.lmax=max(a.lmax, a.ssum+b.lmax);
c.rmax=max(a.rmax+b.ssum, b.rmax);
c.smax=max(a.smax, b.smax);
c.smax=max(c.smax, a.rmax+b.lmax);
return c;
} inline seg * newseg()
{
seg * ret=port++;
return ret;
}
inline void seg::maintain()
{
this->data=merge(this->l->data, this->r->data);
}
inline seg * build(int l, int r)
{
seg * t=newseg();
int m=(l+r)>>; if (l==r)
t->data=node(a[m]);
else
{
t->l=build(l, m);
t->r=build(m+, r);
t->maintain();
} return t;
}
void update(seg * t, int l, int r, int k)
{
int m=(l+r)>>; if (l==r)
t->data=node(a[m]);
else
{
if (k<=m) update(t->l, l, m, k);
else update(t->r, m+, r, k);
t->maintain();
}
}
node query(seg * t, int l, int r, int ql, int qr)
{
node ret;
int m=(l+r)>>; if (l==ql && r==qr) ret=t->data;
else
{
if (qr<=m) ret=query(t->l, l, m, ql, qr);
else if (ql>m) ret=query(t->r, m+, r, ql, qr);
else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+, r, m+, qr));
} return ret;
}

GSS3

 #include <cstdio>
#include <cmath>
typedef long long LL;
const int sizeOfNumber=;
const int sizeOfSeg=; inline void swap(int & , int & );
inline LL getint();
inline void putint(LL); struct seg
{
LL sum;
bool flag;
seg * l, * r;
inline void maintain();
};
seg memory[sizeOfSeg], * port=memory;
inline seg * newseg();
seg * build(int, int);
void update(seg * , int, int, int, int);
LL query(seg * , int, int, int, int); int n, m;
LL a[sizeOfNumber];
seg * t; int main()
{
int c=; while (scanf("%d", &n)!=EOF)
{
for (int i=;i<=n;i++)
a[i]=getint();
port=memory;
t=build(, n); printf("Case #%d:\n", ++c); m=getint();
for (int i=;i<=m;i++)
{
int k=getint(), x=getint(), y=getint();
if (x>y) swap(x, y);
if (k==) update(t, , n, x, y);
else putint(query(t, , n, x, y));
}
} return ;
} inline void swap(int & x, int & y)
{
int t=x; x=y; y=t;
}
inline LL getint()
{
register LL num=;
register char ch;
do ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
return num;
}
inline void putint(LL num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
} inline void seg::maintain()
{
sum=l->sum+r->sum;
flag=l->flag&r->flag;
}
inline seg * newseg()
{
return port++;
}
seg * build(int l, int r)
{
seg * t=newseg();
int m=(l+r)>>; if (l==r)
{
t->sum=a[m];
t->flag=t->sum<=;
}
else
{
t->l=build(l, m);
t->r=build(m+, r);
t->maintain();
} return t;
}
void update(seg * t, int l, int r, int ql, int qr)
{
if (t->flag)
return ;
if (l==r && ql==qr)
{
t->sum=static_cast<int>(sqrt(t->sum));
t->flag=t->sum<=;
return ;
} int m=(l+r)>>;
if (qr<=m) update(t->l, l, m, ql, qr);
else if (ql>m) update(t->r, m+, r, ql, qr);
else update(t->l, l, m, ql, m), update(t->r, m+, r, m+, qr);
t->maintain();
}
LL query(seg * t, int l, int r, int ql, int qr)
{
if (l==ql && r==qr)
return t->sum;
int m=(l+r)>>;
if (qr<=m) return query(t->l, l, m, ql, qr);
else if (ql>m) return query(t->r, m+, r, ql, qr);
else return query(t->l, l, m, ql, m)+query(t->r, m+, r, m+, qr);
}

GSS4

 #include <cstdio>
const int sizeOfNumber=;
const int sizeOfSeg=;
const int inf=0x3F3F3F3F; inline int max(int, int);
inline int getint();
inline void putint(int); struct node
{
int lmax, rmax, smax, ssum;
inline node(int=);
};
inline node merge(node, node); struct seg
{
node data;
seg * l, * r;
inline void maintain();
};
seg memory[sizeOfSeg], * port=memory;
inline seg * newseg();
seg * build(int, int);
node query(seg * , int, int, int, int); int c, n, m;
int a[sizeOfNumber];
seg * t; int main()
{
int ans; for (c=getint();c;c--)
{
n=getint();
for (int i=;i<=n;i++)
a[i]=getint();
port=memory;
t=build(, n); m=getint();
for (int i=;i<=m;i++)
{
int x1=getint(), y1=getint(), x2=getint(), y2=getint(); if (y1<x2)
ans=query(t, , n, x1, y1).rmax+query(t, , n, x2, y2).lmax+query(t, , n, y1+, x2-).ssum;
else
{
node l=query(t, , n, x1, x2-), m=query(t, , n, x2, y1), r=query(t, , n, y1+, y2);
ans=max(merge(l, m).rmax+r.lmax, l.rmax+merge(m, r).lmax);
ans=max(ans, l.rmax+m.ssum+r.lmax);
ans=max(ans, m.smax);
} putint(ans);
}
} return ;
} inline int max(int x, int y)
{
return x>y?x:y;
}
inline int getint()
{
register int num=;
register char ch=, last;
do last=ch, ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
if (last=='-') num=-num;
return num;
}
inline void putint(int num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
if (num<) putchar('-'), num=-num;
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
} inline node::node(int x)
{
lmax=rmax=smax=ssum=x;
}
inline node merge(node a, node b)
{
node c;
c.ssum=a.ssum+b.ssum;
c.lmax=max(a.lmax, a.ssum+b.lmax);
c.rmax=max(a.rmax+b.ssum, b.rmax);
c.smax=max(a.smax, b.smax);
c.smax=max(c.smax, a.rmax+b.lmax);
return c;
} inline seg * newseg()
{
seg * ret=port++;
return ret;
}
inline void seg::maintain()
{
this->data=merge(this->l->data, this->r->data);
}
inline seg * build(int l, int r)
{
seg * t=newseg();
int m=(l+r)>>; if (l==r)
t->data=node(a[m]);
else
{
t->l=build(l, m);
t->r=build(m+, r);
t->maintain();
} return t;
}
node query(seg * t, int l, int r, int ql, int qr)
{
node ret();
int m=(l+r)>>; if (ql>qr) return ret;
if (l==ql && r==qr) ret=t->data;
else
{
if (qr<=m) ret=query(t->l, l, m, ql, qr);
else if (ql>m) ret=query(t->r, m+, r, ql, qr);
else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+, r, m+, qr));
} return ret;
}

GSS5

 #include <cstdio>
#include <cstdlib>
const int sizeOfNumber=;
const int sizeOfTreap=;
const int inf=0x3F3F3F3F; inline int max(int, int);
inline char getch();
inline int getint();
inline void putint(int); struct treap
{
int key, sum;
int lmax, rmax, smax;
int size, weight;
treap * l, * r;
inline treap();
inline void maintain();
};
treap * null=new treap();
treap memory[sizeOfTreap], * port=memory;
inline treap * newtreap(int);
void split(treap * , int, treap *& , treap *& );
treap * merge(treap * , treap * ); int N, Q;
treap * t; int main()
{
treap * l, * m, * r;
char c;
int x, y; N=getint();
t=null;
for (int i=;i<=N;i++)
{
x=getint();
t=merge(t, newtreap(x));
} Q=getint();
for (int i=;i<=Q;i++)
{
c=getch(); x=getint(); if (c=='I')
{
y=getint();
split(t, x-, l, r);
m=newtreap(y);
l=merge(l, m);
t=merge(l, r);
}
else if (c=='D')
{
split(t, x-, l, r);
split(r, , m, r);
t=merge(l, r);
}
else if (c=='R')
{
y=getint();
split(t, x-, l, r);
split(r, , m, r);
m=newtreap(y);
l=merge(l, m);
t=merge(l, r);
}
else
{
y=getint();
split(t, x-, l, r);
split(r, y-x+, m, r);
putint(m->smax);
l=merge(l, m);
t=merge(l, r);
}
} return ;
} inline int max(int x, int y)
{
return x>y?x:y;
}
inline char getch()
{
register char ch;
do ch=getchar(); while (ch!='I' && ch!='D' && ch!='R' && ch!='Q');
return ch;
}
inline int getint()
{
register int num=;
register char ch=, last;
do last=ch, ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
if (last=='-') num=-num;
return num;
}
inline void putint(int num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
if (num<) putchar('-'), num=-num;
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
} inline treap::treap()
{
this->key=; this->sum=;
this->lmax=-inf; this->rmax=-inf; this->smax=-inf;
this->size=; this->weight=;
this->l=this->r=this;
}
inline void treap::maintain()
{
this->sum=this->l->sum+this->key+this->r->sum; this->lmax=max(this->l->lmax, this->l->sum+this->key); this->lmax=max(this->lmax, this->l->sum+this->key+this->r->lmax);
this->rmax=max(this->r->rmax, this->key+this->r->sum); this->rmax=max(this->rmax, this->l->rmax+this->key+this->r->sum); this->smax=max(this->l->smax, this->r->smax); this->smax=max(this->smax, this->l->rmax+this->key+this->r->lmax);
this->smax=max(this->smax, this->l->rmax+this->key); this->smax=max(this->smax, this->key+this->r->lmax);
this->smax=max(this->smax, this->key); this->size=this->l->size++this->r->size;
}
inline treap * newtreap(int _key)
{
treap * ret=port++;
ret->key=_key; ret->sum=_key;
ret->lmax=ret->rmax=ret->smax=_key;
ret->size=; ret->weight=rand();
ret->l=ret->r=null;
return ret;
}
void split(treap * t, int k, treap *& l, treap *& r)
{
if (t==null)
{
l=r=null;
return ;
} if (t->l->size+<=k)
{
l=t;
split(t->r, k-t->l->size-, t->r, r);
l->maintain();
}
else
{
r=t;
split(t->l, k, l, t->l);
r->maintain();
}
}
treap * merge(treap * l, treap * r)
{
if (l==null) return r;
if (r==null) return l; if (l->weight>r->weight)
{
l->r=merge(l->r, r);
l->maintain();
return l;
}
else
{
r->l=merge(l, r->l);
r->maintain();
return r;
}
}

GSS6

 #include <cstdio>
#include <cstring>
const int sizeOfNumber=;
const int sizeOfEdge=;
const int sizeOfSeg=;
const int inf=0x7FFFFFFF; inline int lg(int);
inline void swap(int & , int & );
inline int max(int, int);
inline int getint();
inline void putint(int); struct edge
{
int point;
edge * next;
};
edge memoryOfEdge[sizeOfEdge], * portOfEdge=memoryOfEdge;
inline edge * newedge(int, edge * );
inline void link(int, int); struct node
{
int lmax, rmax, smax, ssum;
inline node(int=);
};
inline node merge(node, node); struct seg
{
node data;
int flag, size;
seg * l, * r;
inline void pushdown();
inline void maintain();
};
seg memoryOfSeg[sizeOfSeg], * portOfSeg=memoryOfSeg;
inline seg * newseg();
seg * build(int, int);
void update(seg * , int, int, int, int, int);
node query(seg * , int, int, int, int); int n, q;
int x[sizeOfNumber];
edge * e[sizeOfNumber];
int a[][sizeOfNumber];
int s[sizeOfNumber], d[sizeOfNumber], f[sizeOfNumber];
int tmp, idx[sizeOfNumber], r_idx[sizeOfNumber], son[sizeOfNumber], top[sizeOfNumber];
seg * t;
inline void dfsTree();
inline void dfsChain();
inline int lca(int, int);
inline int anc(int, int);
inline void update(int, int, int);
inline node query(int, int); int main()
{
n=getint();
for (int i=;i<=n;i++)
x[i]=getint();
for (int i=;i<n;i++)
{
int u=getint(), v=getint();
link(u, v);
}
dfsTree();
dfsChain(); t=build(, n);
q=getint();
for (int i=;i<=q;i++)
{
int k=getint(), x=getint(), y=getint(); if (k==)
{
if (d[x]>d[y])
swap(x, y);
if (x==y)
{
putint(query(t, , n, idx[x], idx[x]).smax);
continue;
} int a=lca(x, y), z=anc(y, d[y]-d[a]-);
node l=query(y, z), r=query(x, a);
swap(l.lmax, l.rmax);
putint(merge(l, r).smax);
}
else
{
int z=getint();
update(x, y, z);
}
} return ;
} inline int lg(int x)
{
return x>?-__builtin_clz(x):;
}
inline void swap(int & x, int & y)
{
int z=x; x=y; y=z;
}
inline int max(int x, int y)
{
return x>y?x:y;
}
inline int getint()
{
register int num=;
register char ch=, last;
do last=ch, ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
if (last=='-') num=-num;
return num;
}
inline void putint(int num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
if (num<) putchar('-'), num=-num;
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
} inline edge * newedge(int point, edge * next)
{
edge * ret=portOfEdge++;
ret->point=point; ret->next=next;
return ret;
}
inline void link(int u, int v)
{
e[u]=newedge(v, e[u]);
e[v]=newedge(u, e[v]);
} inline node::node(int x)
{
ssum=x;
lmax=rmax=smax=max(x, );
}
inline node merge(node a, node b)
{
node c;
c.ssum=a.ssum+b.ssum;
c.lmax=max(a.lmax, a.ssum+b.lmax);
c.rmax=max(a.rmax+b.ssum, b.rmax);
c.smax=max(a.smax, b.smax);
c.smax=max(c.smax, a.rmax+b.lmax);
return c;
} inline void seg::pushdown()
{
if (this->flag<inf)
{
this->l->data.ssum=this->l->size*this->flag;
this->r->data.ssum=this->r->size*this->flag;
this->l->data.lmax=this->l->data.rmax=this->l->data.smax=max(this->l->data.ssum, );
this->r->data.lmax=this->r->data.rmax=this->r->data.smax=max(this->r->data.ssum, );
this->l->flag=this->r->flag=this->flag;
this->flag=inf;
}
}
inline void seg::maintain()
{
this->data=merge(this->l->data, this->r->data);
this->size=this->l->size+this->r->size;
}
inline seg * newseg()
{
seg * ret=portOfSeg++;
ret->flag=inf;
return ret;
}
seg * build(int l, int r)
{
seg * t=newseg();
int m=(l+r)>>; if (l==r)
{
t->data=node(x[r_idx[m]]);
t->size=;
}
else
{
t->l=build(l, m);
t->r=build(m+, r);
t->maintain();
} return t;
}
void update (seg * t, int l, int r, int ql, int qr, int v)
{
if (l==ql && r==qr)
{
t->data.ssum=t->size*v;
t->data.lmax=t->data.rmax=t->data.smax=max(t->data.ssum, );
t->flag=v;
}
else
{
int m=(l+r)>>;
t->pushdown();
if (qr<=m) update(t->l, l, m, ql, qr, v);
else if (ql>m) update(t->r, m+, r, ql, qr, v);
else update(t->l, l, m, ql, m, v), update(t->r, m+, r, m+, qr, v);
t->maintain();
}
}
node query(seg * t, int l, int r, int ql, int qr)
{
node ret; if (l==ql && r==qr)
ret=t->data;
else
{
int m=(l+r)>>;
t->pushdown();
if (qr<=m) ret=query(t->l, l, m, ql, qr);
else if (ql>m) ret=query(t->r, m+, r, ql, qr);
else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+, r, m+, qr));
t->maintain();
} return ret;
} inline void dfsTree()
{
static edge * t[sizeOfNumber];
memset(f, 0xFF, sizeof(f)); f[]=;
memmove(t, e, sizeof(e));
int lim; for (int u=;true; )
{
if (!s[u])
{
s[u]=; lim=lg(d[u]);
a[][u]=f[u];
for (int i=;i<=lim;i++)
a[i][u]=a[i-][a[i-][u]];
} edge *& i=t[u];
for ( ;i && f[i->point]>=;i=i->next);
if (i)
{
f[i->point]=u;
d[i->point]=d[u]+;
u=i->point;
}
else
{
if (u==) break;
s[f[u]]+=s[u];
if (s[u]>s[son[f[u]]])
son[f[u]]=u;
u=f[u];
}
}
}
inline void dfsChain()
{
static edge * t[sizeOfNumber];
memmove(t, e, sizeof(e)); top[]=;
for (int u=;true; )
{
if (!idx[u])
{
idx[u]=++tmp;
r_idx[tmp]=u;
}
if (son[u] && !idx[son[u]])
{
top[son[u]]=top[u];
u=son[u];
continue;
} edge *& i=t[u];
for ( ;i && idx[i->point];i=i->next);
if (i)
{
top[i->point]=i->point;
u=i->point;
}
else
{
if (u==)
break;
u=f[u];
}
}
}
inline int lca(int u, int v)
{
if (d[u]<d[v]) swap(u, v);
while (int dist=d[u]-d[v])
u=a[__builtin_ctz(dist)][u];
if (u==v) return u;
for (int i=;i>=;i--)
if (a[i][u]!=a[i][v])
u=a[i][u],
v=a[i][v];
return a[][u];
}
inline int anc(int u, int d)
{
for (int i=;i>=;i--)
if ((d>>i)&)
u=a[i][u];
return u;
}
inline void update(int u, int v, int c)
{
while (top[u]!=top[v])
{
if (d[top[u]]<d[top[v]]) swap(u, v);
update(t, , n, idx[top[u]], idx[u], c);
u=f[top[u]];
}
if (d[u]>d[v]) swap(u, v);
update(t, , n, idx[u], idx[v], c);
}
inline node query(int u, int a)
{
node ret; while (top[u]!=top[a])
{
ret=merge(query(t, , n, idx[top[u]], idx[u]), ret);
u=f[top[u]];
}
ret=merge(query(t, , n, idx[a], idx[u]), ret); return ret;
}

GSS7

 #include <cstdio>
#include <cstring>
typedef long long llint;
typedef unsigned int uint; namespace IO
{
const int sizeOfInput=;
char inputBuffer[sizeOfInput], * port=inputBuffer; inline void assign();
inline void close();
inline char getch();
inline uint getint();
inline void putint(uint);
}; namespace random
{
llint num, seed, mod;
inline void srand();
inline int getrand();
}; namespace treap
{
const int sizeOfMemory=;
uint C[][];
uint P[sizeOfMemory][]; struct node
{
uint c[];
uint key, size;
int weight;
node * left, * right; inline node();
inline void maintain();
};
node * null=new node();
node memory[sizeOfMemory], * port=memory; inline void prepare();
inline node * newnode(uint);
inline void update(node * , uint);
void split(node * , uint, node *& , node *& );
node * merge(node * , node * );
}; int main()
{
using namespace treap;
using namespace IO; node * root=null;
node * L, * M, * R;
int N, Q;
int pos, val, l, r, k;
char ch; assign();
random::srand();
prepare(); N=getint();
for (int i=;i<N;i++)
root=merge(root, newnode(getint())); for (Q=getint();Q;Q--)
{
ch=getch();
if (ch=='I')
{
pos=getint(), val=getint();
M=treap::newnode(val);
split(root, pos, L, R);
L=merge(L, M);
root=merge(L, R);
}
else if (ch=='D')
{
pos=getint();
split(root, pos, L, R);
split(R, , M, R);
root=merge(L, R);
}
else if (ch=='R')
{
pos=getint(), val=getint();
split(root, pos, L, R);
split(R, , M, R);
update(M, val);
L=merge(L, M);
root=merge(L, R);
}
else
{
l=getint(), r=getint(), k=getint();
split(root, r+, L, R);
split(L, l, L, M);
putint(M->c[k]);
L=merge(L, M);
root=merge(L, R);
}
} return ;
} namespace IO
{
inline void assign()
{
freopen("GSS8.in", "r", stdin);
freopen("GSS8.out", "w", stdout);
fread(inputBuffer, , , stdin);
}
inline void close()
{
fclose(stdin);
fclose(stdout);
}
inline char getch()
{
register char ch;
do ch=*(port++); while (ch<'A' || ch>'Z');
return ch;
}
inline uint getint()
{
register uint num=;
register char ch;
do ch=*(port++); while (ch<'' || ch>'');
do num=num*+ch-'', ch=*(port++); while (ch>='' && ch<='');
return num;
}
inline void putint(uint num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
}
} namespace random
{
inline void srand()
{
num=, seed=, mod=;
for (int i=;i<=;i++)
getrand();
}
inline int getrand()
{
num=num*seed%mod;
return num;
}
} namespace treap
{
inline node::node()
{
memset(c, , sizeof(c));
key=size=;
weight=;
left=right=this;
}
inline void node::maintain()
{
int tmp=left->size+;
size=left->size++right->size; for (int i=;i<=;i++) c[i]=left->c[i];
for (int i=;i<=;i++) c[i]+=key*P[tmp][i];
for (int i=;i<=;i++) for (int j=;j<=i;j++)
c[i]+=C[i][j]*P[tmp][i-j]*right->c[j];
}
inline void prepare()
{
C[][]=;
for (int i=;i<=;i++)
{
C[i][]=;
for (int j=;j<i;j++)
C[i][j]=C[i-][j-]+C[i-][j];
C[i][i]=;
} for (int i=;i<sizeOfMemory;i++)
{
P[i][]=;
for (int j=;j<=;j++)
P[i][j]=P[i][j-]*i;
}
}
inline node * newnode(uint _key)
{
node * ret=port++;
for (int i=;i<=;i++) ret->c[i]=_key;
ret->key=_key, ret->size=;
ret->weight=random::getrand();
ret->left=ret->right=null;
return ret;
}
inline void update(node * t, uint _key)
{
for (int i=;i<=;i++) t->c[i]=_key;
t->key=_key;
}
void split(node * t, uint k, node *& l, node *& r)
{
if (t==null) l=r=null;
else
{
if (t->left->size<k)
{
l=t;
split(t->right, k-t->left->size-, t->right, r);
l->maintain();
}
else
{
r=t;
split(t->left, k, l, t->left);
r->maintain();
}
}
}
node * merge(node * l, node * r)
{
if (l==null) return r->maintain(), r;
if (r==null) return l->maintain(), l;
if (l->weight>r->weight)
{
l->right=merge(l->right, r);
l->maintain();
return l;
}
else
{
r->left=merge(l, r->left);
r->maintain();
return r;
}
}
}

GSS8

激!GSS系列的更多相关文章

  1. SPOJ GSS 系列

    来怒做GSS系列了: GSS1:https://www.luogu.org/problemnew/show/SP1043 这题就是维护一个 sum , mx , lmx , rmx,转移时用结构体就好 ...

  2. spoj GSS系列简要题解

    文章目录 GSS1 GSS2 GSS3 GSS4 GSS5 GSS6 GSS7 GSS8 传送门 这个GSSGSSGSS系列全部是跟子段有关的数据结构菜题. 于是来水一篇博客. GSS1 传送门 题意 ...

  3. SPOJ GSS系列

    众所周知的仅次于ynoi的毒瘤数据结构系列.(跟Qtree系列并列?) GSS1: 长度为 $n$ 的序列 $a$,$m$ 个询问,每次询问区间 $[l,r]$ 之间的最大子段和. $1\le n,m ...

  4. SPOJ GSS系列(数据结构维护技巧入门)

    题目链接 GSS $GSS1$ 对于每个询问$l$, $r$,查询$a_{l}$, $a_{l+1}$, $a_{l+2}$, ..., $a_{r}$这个序列的最大字段和. 建立线段树,每个节点维护 ...

  5. GSS 系列题解

    GSS GSS1 随便猫树或者线段树,就可以过了 猫树不说,线段树可以维护左边最大,右边最大,区间最大,区间值然后就做出来了. //Isaunoya #pragma GCC optimize(2) # ...

  6. GSS系列(1)——GSS1&&GSS3

    题意:询问一个区间内的最大连续子段和(GSS1),并且有单点修改的操作(GSS2). 思路:这个题目在老人家的大白鼠里出现过,不过那个是求两个下标,并且相同取更小值.——传的东西更多,判断也稍微繁琐一 ...

  7. GSS系列

    GSS1 直接维护静态区间和即可 #include<bits/stdc++.h> using namespace std; ; int n,a[N]; ]; inline void pus ...

  8. GSS系列题解——最大子段和系列

    开坑啦! 2019 3/28 以前一直不知道怎么搞最大子段和,如今终于可以学习,其实真的很简单啊. 2019 3/29 树链剖分上最大子段和也OK啦 前置技能:线段树 题目大意:询问区间[l,r]的最 ...

  9. SPOJ GSS3 线段树系列1

    SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...

随机推荐

  1. JAVA第四周作业

    import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...

  2. DataTable转Json字符串(使用Newtonsoft.Json.dll)

    DataTable转Json字符串(使用Newtonsoft.Json.dll) 在需要把DataTable转为Json字符串时,自己手动拼接太麻烦,而且容易出错,费时费力,使用Newtonsoft. ...

  3. mstsc连接服务器时如何避免每次在登陆窗口输入密码(rdp passwd decrypted )

    http://blog.chinaunix.net/uid-1835840-id-2831518.html 我们经常需要连接很多服务器,或者服务器的ip经常变动.把连接的密码保存在rdp配置文件里,用 ...

  4. GreenDao数据库框架的配置与增删改查

    并非原创,原创地址http://blog.csdn.net/njweiyukun/article/details/51893092 配置-------------------------------- ...

  5. position窗口居中

    position的四个属性值: relative absolute fixed static 下面分别讲述这四个属性. <div id="parent">     &l ...

  6. 新篇章,Golang 和 beego 初识

    beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API.Web 及后端服务等各种应用,是一个 RESTful 的框架,主要设计灵感来源于 tornado.sinatra ...

  7. 64位系统下找不到office 32位组件

    如果系统式64位的,而装的是32位的office软件,在运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,但是却发现找不到Microsoft Excel程序, 这主要是64位系统的问题,exc ...

  8. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  9. iOS内置音频

    Predefined soundsThere are some predefined system sounds, for the system sound ID in the range 1000 ...

  10. pdf生成器

    apose, http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version