Another wave of template finishing ~
Segment Tree:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define L (x) (x
<<1) #define R (x) (x<<1|1) #define SZ (x) (tree[x].r-tree[x].l+1) using namespace std;
typedef long Long LL;
const int SZ=200000+10;
LL A[sz]; struct sgt{LL l,r,add,sum;}
tree[sz<<2];
void Update (LL p) {tree[p].sum=tree[r (P)].sum+tree[l (p)].sum;
Return
} void Spread (LL p) {if (!tree[p].add) return;
Tree[l (p)].add+=tree[p].add;
Tree[r (p)].add+=tree[p].add;
Tree[l (P)].sum+=tree[p].add*sz (L (p));
Tree[r (P)].sum+=tree[p].add*sz (R (p));
Tree[p].add=0;
Update (P);
Return
} void Build (LL l,ll r,ll p) {tree[p].l=l;tree[p].r=r;
if (l==r) {tree[p].sum=a[l];
Return
LL mid= (TREE[P].L+TREE[P].R) >>1;
Build (L,mid,l (p));
Build (Mid+1,r,r (p));
Update (P);
Return } void Change (LL l,ll r,ll P,ll v) {if (l<=tree[p].l&&tree[p].r<=r) {tree[P].add+=v;
Tree[p].sum+=v*sz (P);
Return
} spread (p);
LL mid= (TREE[P].L+TREE[P].R) >>1;
if (l<=mid) Change (L,r,l (p), v);
if (mid<r) Change (L,r,r (p), v);
Update (P);
Return
ll ask (ll l,ll R,ll p) {ll ans=0;
if (l<=tree[p].l&&tree[p].r<=r) return tree[p].sum;
Spread (p);
LL mid= (TREE[P].L+TREE[P].R) >>1;
if (l<=mid) Ans+=ask (L,r,l (p));
if (mid<r) Ans+=ask (L,r,r (p));
return ans;
int main () {int n;
scanf ("%d", &n);
for (int i=1;i<=n;i++) scanf ("%lld", &a[i));
Build (1,n,1);
int m;
scanf ("%d", &m);
for (int i=1;i<=m;i++) {int t;
scanf ("%d", &t);
if (t==1) {int l,r,s;
scanf ("%d%d%d", &l,&r,&s);
Change (l,r,1,s);
else if (t==2) {int l,r;
scanf ("%d%d", &l,&r); printf ("%lld\n", Ask (l,r,1));
} return 0; }
Tree-like array + difference sequence:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define
Lowbit (x) (x& (-X)) using namespace Std;
const int MAXN=100000+10;
int TREE[MAXN],C[MAXN],A[MAXN];
int n;
void Add (int x,int y) {while (x<=n) {tree[x]+=y;
X+=lowbit (x);
int sum (int x) {int ans=0;
while (x) {ans+=tree[x];
X-=lowbit (x);
return ans;
int main () {scanf ("%d", &n);
for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);
C[I]=A[I]-A[I-1];
Add (I,c[i]);
int q;scanf ("%d", &q);
while (q--) {int t,l,r,s;
scanf ("%d", &t);
if (t==1) {scanf ("%d%d%d", &l,&r,&s);
Add (l,s);
Add (r+1,-s);
} if (t==2) {int pos;
scanf ("%d", &pos);
printf ("%d\n", SUM (POS));
} return 0; }
And check the collection:
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN=5000+50;
int FA[MAXN];
int find (int x)
{
if (fa[x]==x) return x;
else return Fa[x]=find (fa[x]);
void merge (int x,int y)
{
int fx=find (x);
int Fy=find (y);
fa[fx]=fy;
}
int main ()
{
int n,m,q,a,b;
scanf ("%d%d%d", &n,&m,&q);
for (int i=1;i<=n;i++)
fa[i]=i;
for (int i=1;i<=m;i++)
{
scanf ("%d%d", &a,&b);
Merge (a,b);
}
int x,y;
for (int i=1;i<=q;i++)
{
scanf ("%d%d", &x,&y);
if (find (x) ==find (y))
puts ("Yes");
Else
puts ("No");
return 0;
}
monotonic queues:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
int n,m;
int num[1000000+10];
int q[1000000+10],que[1000000+10];
void Init () {scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++) scanf ("%d", &num[i]);
} void Up () {puts ("");
int head=1,tail=0;
for (int i=1;i<=n;i++) {while (Head<=tail&&num[i]>num[q[tail]]) tail--;
Q[++tail]=i;
if (i>=m) {while (Head<=tail&&i-m>=q[head]) head++;
printf ("%d", Num[q[head]);
}} void Down () {int head=1,tail=0;
for (int i=1;i<=n;i++) {while (Head<=tail&&num[i]<num[que[tail]]) tail--;
Que[++tail]=i;
if (i>=m) {while (Head<=tail&&i-m>=que[head]) head++;
printf ("%d", Num[que[head]);
}} void Work () {down ();
Up (); int main ()
{init ();
Work ();
return 0; }