这真的只是我的一个云剪贴板。
#include<bits/stdc++.h>
#define LL long long
#define MAXN 1000000
using namespace std;
template<typename T> void Read(T &cn)
{
char c; int sig = 1;
while(!isdigit(c = getchar())) if(c == '-') sig = 0;
if(sig) {cn = c-48; while(isdigit(c = getchar())) cn = cn*10+c-48; }
else {cn = 48-c; while(isdigit(c = getchar())) cn = cn*10+48-c; }
}
template<typename T> void Write(T cn)
{
T cm = 0; int wei = 0, cx = cn%10; cn/=10;
if(cn < 0 || cx < 0) {cx = 0-cx; cn = 0-cn; putchar('-'); }
while(cn) wei++, cm = cm*10+cn%10, cn/=10;
while(wei--) putchar(cm%10+48), cm/=10;
putchar(cx+48);
}
template<typename T> void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T> void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
int get_r(int cn) {return (1ll*rand()*rand()+rand())%cn; }
int get_n(int cn, int cm) {return cn + get_r(cm-cn+1); }
struct qwe{
int a,b;
void mk(int cn, int cm) {a = cn; b = cm; }
void outit() {Write(a); putchar(' '); Write(b); puts(""); }
};
int n,m;
qwe a[MAXN+1];
int main()
{
// freopen(.in","r",stdin);
freopen("cactus.in","w",stdout);
srand(time(NULL));
n = 1; m = 0;
for(int i = 1;i<=8;i++)
{
int lin = get_n(1,3);
int lin2 = get_n(1,n);
a[++m].mk(lin2, n+1);
a[++m].mk(lin2, n+lin);
for(int j = 2;j<=lin;j++) a[++m].mk(n+j-1, n+j);
n = n+lin;
}
Write(n); putchar(' '); Write(m); puts("");
for(int i = 1;i<=m;i++) a[i].outit();
for(int i = 1;i<=n;i++) Write(get_n(1,15)), putchar(' '); puts("");
return 0;
}
考前还要打的板子:
多项式全家桶虚树- lct
- splay
- 斯特林数有关知识
- 异或FWT
辛普森积分可并堆prufer矩形覆盖三维凸包- 支配树
- Pollard's rho
- 动态点分治
- 2-SAT
#include<bits/stdc++.h>
#define LL long long
using namespace std;
template<typename T>void Read(T &cn)
{
char c; int sig = 1;
while(!isdigit(c = getchar())) if(c == '-') sig = -1; cn = c-48;
while(isdigit(c = getchar())) cn = cn*10+c-48; cn*=sig;
}
template<typename T>void Write(T cn)
{
if(cn < 0) {putchar('-'); cn = 0-cn; }
int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
while(cn) wei++, cm = cm*10+cn%10, cn/=10;
while(wei--) putchar(cm%10+48), cm /= 10;
putchar(cx+48);
}
template<typename T>void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T>void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
int erwei(int cn) {int guo = -1; while(cn) guo++, cn>>=1; return guo; }
struct Tree{
struct qwe{
int a,b,ne;
void mk(int cn, int cm, int cx) {a = cn; b = cm; ne = cx; }
};
qwe a[MAXN*2+1];
int alen;
int head[MAXN+1];
void build() {alen = 0; memset(head,0,sizeof(head)); }
void lian(int cn, int cm) {a[++alen].mk(cn,cm,head[cn]); head[cn] = alen; }
}T1, T2;
struct Seg{
LL a[MAXN*MAXLOG+1];
int alen;
struct Szsz{
int l, r;
void jia(int cn, LL cm) {for(;cn<=(r-l+1);cn+=cn&-cn) a[l+cn-1]+=cm; }
LL qiu(int cn) {Min(cn,r-l+1); LL guo = 0; for(;cn;cn-=cn&-cn) guo+=a[l+cn-1]; return guo; }
void build(int cn) {l = alen+1; r = alen+cn; alen = r; }
}ro[MAXN+1];
void build(int cn, int cm) {ro[cn].build(cm+1); }
void jia(int cn, int cm, LL cx) {ro[cn].jia(cm+1, cx); }
LL qiu(int cn, int cm) {return ro[cn].qiu(cm+1); }
}S1, S2;
namespace GET_jl{
int dfn[MAXN+1], lie[MAXN*2+1], shi;
int ST[MAXN*2+1][22];
int ern[MAXN*2+1];
int shen[MAXN+1];
void dfs1(int cn, int fa, Tree &T)
{
lie[dfn[cn] = ++shi] = cn;
for(int i = T.head[cn];i;i = T.a[i].ne)
{
int y = T.a[i].b;
if(y == fa) continue;
shen[y] = shen[cn]+1;
dfs1(y, cn, T);
lie[++shi] = cn;
}
}
void mk_ST(int cn)
{
for(int i = 1;i<=cn;i++) ern[i] = erwei(i);
for(int i = 1;i<=cn;i++) ST[i][0] = shen[lie[i]];
for(int i = 1;i<=ern[cn];i++) for(int j = 1;j<=cn;j++) ST[j][i] = min(ST[j][i-1], ST[j+(1<<(i-1))][i-1]);
}
int get_lca(int cn, int cm)
{
cn = dfn[cn]; cm = dfn[cm]; if(cn > cm) swap(cn,cm);
int lin = ern[cm-cn+1]; return min(ST[cn][lin], ST[cm-(1<<lin)+1][lin]);
}
int get_jl(int cn, int cm) {return shen[cn]+shen[cm]-2*get_lca(cn,cm); }
void jl_build(Tree &T) {shen[1] = 1; shen[0] = 0; shi = 0; dfs1(1, 0); mk_ST(shi); }
};
int get_vis(int);
namespace GET_zx{
int ans;
int da[MAXN+1];
void dfs1(int cn, int fa)
{
}
int get_zx(int cn) {ans = cn; dfs1(cn, 0); dfs2(cn, 0); return ans; }
};
using GET_jl::get_jl;
using GET_jl::jl_build;
using GET_zx::get_zx;
int vis[MAXN+1];
int get_vis(int cn) {return vis[cn]; }
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
return 0;
}
#include<bits/stdc++.h>
#define LL long long
#define MAXN 100000
#define MAXSQR 150
#define MAXQ 100
#define MAXAI 1000010
using namespace std;
template<typename T>void Read(T &cn)
{
char c; int sig = 1;
while(!isdigit(c = getchar())) if(c == '-') sig = -1; cn = c-48;
while(isdigit(c = getchar())) cn = cn*10+c-48; cn*=sig;
}
template<typename T>void Write(T cn)
{
if(cn < 0) {putchar('-'); cn = 0-cn; }
int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
while(cn) wei++, cm = cm*10+cn%10, cn/=10;
while(wei--) putchar(cm%10+48), cm /= 10;
putchar(cx+48);
}
template<typename T>void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T>void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
struct Posi{
int num, pos1, pos2, zhi;
};
struct node{
int da, fa, pos, num;
LL zong;
void mk(int cn, int cm, LL cx, int cy, int cw) {da = cn; fa = cm; zong = cx; pos = cy; num = cw; }
};
node zhan[MAXQ*MAXSQR*2+1];
int zlen;
int da[MAXN*MAXSQR*2+1], fa[MAXN*MAXSQR*2+1];
int xu[MAXN*MAXSQR+1], xu2[MAXN*MAXSQR+1];
int he[MAXAI+1];
Posi fen[MAXN+MAXQ+1][MAXSQR+1];
int fenlen[MAXN+MAXQ+1];
int bian[MAXN+1];
LL ans;
int ssh[MAXAI+1], pri[MAXAI+1], xiao[MAXAI+1], mu[MAXAI+1], plen;
struct Bcj{
int L, R, num;
LL zong;
int get_fa(int cn) {return fa[cn] == cn ? cn : get_fa(fa[cn]); }
void build(int cl, int cr, int cn)
{
zong = 0; L = cl; R = cr; num = cn;
for(int i = L;i<=R;i++) fa[i] = i, da[i] = 1;
}
void jia(int cn, int cm)
{
cn = get_fa(cn); cm = get_fa(cm);
if(cn == cm) return;
if(da[cn] < da[cm]) swap(cn, cm);
gai(cn, da[cn]+da[cm], cn);
gai(cm, 0, cn);
}
void gai(int cpos, int cda, int cfa)
{
ans = ans-mu[num]*zong;
zhan[++zlen].mk(da[cpos], fa[cpos], zong, cpos, num);
zong = zong - 1ll*da[cpos]*(da[cpos]+1)/2;
da[cpos] = cda; fa[cpos] = cfa;
zong = zong + 1ll*da[cpos]*(da[cpos]+1)/2;
ans = ans+mu[num]*zong;
}
}B[MAXN*MAXSQR+1];
void erase() {zlen = 0; }
void relea()
{
while(zlen)
{
int cn = zhan[zlen].pos, cm = zhan[zlen].num;
da[cn] = zhan[zlen].da; fa[cn] = zhan[zlen].fa;
ans = ans - mu[B[cm].num]*B[cm].zong;
B[cm].zong = zhan[zlen].zong;
ans = ans + mu[B[cm].num]*B[cm].zong;
zlen--;
}
}
struct Bian{
int a, b, w, you;
void getit() {Read(a); Read(b); Read(w); you = 0; }
};
struct qwe{
int x, y, pre;
void getit() {Read(x); Read(y); }
};
Bian a[MAXN+1];
qwe b[MAXQ+1];
int lst[MAXN+1];
int n, q;
void yuchu(int cn)
{
cerr<<"in yuchu"<<endl;
memset(ssh,0,sizeof(ssh)); plen = 0;
ssh[1] = 1; mu[1] = 1;
for(int i = 2;i<=cn;i++)
{
if(i % 1000 == 0) cerr<<i<<endl;
if(!ssh[i]) {
pri[++plen] = i;
xiao[i] = plen;
mu[i] = 1;
for(LL j = 1ll*i*i;j<=cn;j = j*i) mu[j] = 0;
}
for(int j = 1;j<=xiao[i] && 1ll*i*pri[j]<=cn;j++)
{
int lin = i*pri[j];
ssh[lin] = 1; xiao[lin] = j;
mu[lin] = mu[i]*mu[pri[j]];
}
}
}
void fenjie(Posi a[], int &alen, int cn)
{
for(int i = 1;i*i<=cn;i++) if(cn%i == 0) {if(mu[i]!=0) a[++alen].zhi = i; if(mu[cn/i]!=0) a[++alen].zhi = cn/i; }
if(a[alen-1].zhi == a[alen].zhi) alen--;
}
void build_Bcj()
{
printf("in build_Bcj
");
yuchu(MAXAI);
cerr<<"yuchu over"<<endl;
for(int i = 1;i<=n-1;i++) fenjie(fen[i], fenlen[i], a[i].w);
for(int i = 1;i<=q;i++) fenjie(fen[i+n], fenlen[i+n-1], b[i].y);
cerr<<"fenjie over"<<endl;
memset(he,0,sizeof(he));
for(int i = 1;i<=n+q-1;i++) for(int j = 1;j<=fenlen[i];j++) he[fen[i][j].zhi]++;
for(int i = 1;i<=MAXAI;i++) he[i] += he[i-1];
for(int i = 1;i<=n+q-1;i++) for(int j = 1;i<=fenlen[i];j++)
{
int lin = he[fen[i][j].zhi]--;
xu[lin] = i; xu2[lin] = j;
}
cerr<<"xu proed"<<endl;
int lin = he[MAXAI];
memset(bian,0,sizeof(bian));
int xian = 0; int xian_num = 0;
for(int i = 1;i<=lin;)
{
int j = i; xian_num++;
while(j < lin && fen[xu[j+1]][xu2[j+1]].zhi == fen[xu[j]][xu2[j]].zhi) j++;
int L = xian+1, R = xian;
for(int k = i;k<=j;k++)
{
int hao = xu[k]; if(hao >= n) hao = b[hao].x;
if(!bian[a[hao].a]) bian[a[hao].a] = ++R;
if(!bian[a[hao].b]) bian[a[hao].b] = ++R;
fen[xu[k]][xu2[k]].num = xian_num;
fen[xu[k]][xu2[k]].pos1 = bian[a[hao].a];
fen[xu[k]][xu2[k]].pos2 = bian[a[hao].b];
}
for(int k = i;k<=j;k++)
{
int hao = xu[k]; if(hao >= n) hao = b[hao].x;
bian[a[hao].a] = 0;
bian[a[hao].b] = 0;
}
B[xian_num].build(L, R, fen[xu[R]][xu[R]].zhi);
i = j+1;
}
zlen = ans = 0;
for(int i = 1;i<=n-1;i++) if(!a[i].you)
{
for(int j = 1;j<=fenlen[i];j++) B[fen[i][j].num].jia(fen[i][j].pos1, fen[i][j].pos2), zlen = 0;
}
for(int i = 1;i<=xian_num;i++) ans = ans + mu[B[i].num]*B[i].zong;
printf("re build_Bcj
");
}
void suan(int cn)
{
for(int i = 1;i<=q;i++) if(i > cn && b[i].pre <= cn)
{
int hao = b[i].pre;
if(hao == 0) hao = b[i].x;
for(int j = 1;j<=fenlen[hao];j++) B[fen[hao][j].num].jia(fen[hao][j].pos1, fen[hao][j].pos2);
}
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
Read(n);
for(int i = 1;i<=n-1;i++) a[i].getit(), lst[i] = 0;
Read(q);
for(int i = 1;i<=q;i++) b[i].getit(), a[b[i].x].you = 1, b[i].pre = lst[b[i].x], lst[b[i].x] = i;
ans = 0; build_Bcj();
suan(0); Write(ans); puts(""); relea();
for(int i = 1;i<=q;i++) suan(1), Write(ans), puts(""), relea();
return 0;
}
#include<bits/stdc++.h>
#define LL long long
#define MAXN 100000
using namespace std;
template<typename T>void Read(T &cn)
{
char c; int sig = 1;
while(!isdigit(c = getchar())) if(c == '-') sig = -1; cn = c-48;
while(isdigit(c = getchar())) cn = cn*10+c-48; cn*=sig;
}
template<typename T>void Write(T cn)
{
if(cn < 0) {putchar('-'); cn = 0-cn; }
int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
while(cn) wei++, cm = cm*10+cn%10, cn/=10;
while(wei--) putchar(cm%10+48), cm /= 10;
putchar(cx+48);
}
template<typename T>void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T>void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
void cal_ans(int, int);
int erwei(int cn) {int guo = -1; while(cn) guo++, cn>>=1; return guo; }
struct Tree_B{
struct qwe{
int a,b,ne;
void mk(int cn, int cm, int cx) {a = cn; b =cm; ne = cx; }
};
qwe a[MAXN*2+1];
int alen;
int head[MAXN+1];
int n;
int dy[MAXN+1], fa[MAXN+1], shen[MAXN+1];
void lian(int cn, int cm) {a[++alen].mk(cn, cm, head[cn]); head[cn] = alen; }
void build(int cn) {n = cn; alen = 0; memset(head,0,sizeof(head)); }
void get_dfs(int dfn[], int lef[], int lie[]) {int shi = 0; dfs(1, dfn, lef, lie, shi); }
void dfs(int cn, int dfn[], int lef[], int lie[], int &shi)
{
lie[dfn[cn] = ++shi] = cn;
for(int i = head[cn];i;i = a[i].ne) dfs(a[i].b, dfn, lef, lie, shi);
lef[cn] = shi;
}
void outit()
{
printf(" n = %d
",n);
for(int i = 1;i<=n;i++)
{
printf(" dy[%d] = %d fa[%d] = %d shen[%d] = %d
",i,dy[i],i,fa[i],i,shen[i]);
for(int j = head[i];j;j = a[j].ne) printf(" -> %d
",a[j].b);
}
}
}T1, T2;
struct SAM{
struct node{
int link, len, ch[27];
void qing() {link = len = 0; memset(ch,0,sizeof(ch)); }
};
node t[MAXN*2+1];
int tlen, last;
int pos[MAXN+1], n;
void build() {n = 0; tlen = last = 1; t[0].qing(); t[1].qing(); }
void jia(int cn, int cm)
{
int cur = ++tlen;
t[cur].qing(); t[cur].len = t[last].len+1;
int p = last;
for(;p && !t[p].ch[cn];p = t[p].link) t[p].ch[cn] = cur;
if(!p) t[cur].link = 1;
else {
int q = t[p].ch[cn];
if(t[q].len == t[p].len+1) t[cur].link = q;
else {
int cln = ++tlen;
t[cln] = t[q]; t[cln].len = t[p].len+1;
t[q].link = t[cur].link = cln;
for(;p && t[p].ch[cn] == q;p = t[p].link) t[p].ch[cn] = cln;
}
}
last = cur;
pos[cm] = cur; Max(n, cm);
}
void anpai(Tree_B &T)
{
T.build(tlen);
for(int i = 1;i<=n;i++) T.dy[pos[i]] = i;
for(int i = 2;i<=tlen;i++) T.fa[i] = t[i].link, T.lian(t[i].link,i);
for(int i = 1;i<=tlen;i++) T.shen[i] = t[i].len;
}
}S;
struct Szsz1{
int a[MAXN+1];
int n;
void build(int cn) {n = cn; memset(a,0,sizeof(a)); }
void jia(int cn, int cm) {for(;cn <= n;cn+=n&-cn) a[cn]+=cm; }
int qiu_1(int cn) {int guo = 0; for(;cn>0;cn-=cn&-cn) guo+=a[cn]; return guo; }
int qiu(int cl, int cr) {return qiu_1(cr) - qiu_1(cl-1); }
};
struct Szsz2{
int a[MAXN+1];
int n;
void build(int cn) {n = cn; memset(a,0,sizeof(a)); }
void jia_1(int cn, int cm) {for(;cn <= n;cn+=cn&-cn) a[cn]+=cm; }
void jia(int cl, int cr, int cm) {jia_1(cl,cm); jia_1(cr+1,-cm); }
int qiu(int cn) {int guo = 0; for(;cn>0;cn-=cn&-cn) guo+=a[cn]; return guo; }
};
struct Jh{
int zhan[MAXN+1];
int zlen;
void build() {zlen = 0; zhan[0] = 0; }
void jia(int cn) {zhan[++zlen] = cn; }
int shan() {return zlen ? zhan[zlen--] : 0; }
};
struct STbiao{
int ST[MAXN+1][21];
int shen[MAXN+1];
int ewn;
void build(Tree_B &t)
{
ewn = erwei(t.n);
for(int i = 1;i<=t.n;i++) shen[i] = t.shen[i], ST[i][0] = t.fa[i];
for(int i = 1;i<=ewn;i++) for(int j = 1;j<=t.n;j++) ST[j][i] = ST[ST[j][i-1]][i-1];
}
int tiao(int cn, int cm) {for(int i = ewn;i>=0;i--) if(shen[ST[cn][i]] >= cm && ST[cn][i]) cn = ST[cn][i]; return cn; }
};
struct Szsz_pile{
Szsz1 a[21];
Szsz2 b[21];
Jh c[21];
int val[21];
int len, n;
int dfn[MAXN+1], lef[MAXN+1], lie[MAXN+1];
int dy[MAXN+1], pos[MAXN+1];
STbiao ST;
void build(Tree_B &t)///////////////////////////////////////////////////////////////////////////
{
len = -1; n = t.n;
for(int i = 0;i<=20;i++) a[i].build(n), b[i].build(n);
t.get_dfs(dfn, lef, lie);
for(int i = 1;i<=n;i++) dy[i] = t.dy[i], pos[dy[i]] = i;
ST.build(t);
}
void set_new(int cn) {len++; c[len].build(); val[len] = cn; }///////////////////////////////////////////////////////////////////////////
void zeng_1(int cn, int cm)
{
if(cm<0) return;
c[cm].jia(cn);
int cx = ST.tiao(pos[cn], val[cm]);
cal_ans(cn, a[cm].qiu(dfn[cx], lef[cx]));
a[cm].jia(dfn[pos[cn]], 1);
b[cm].jia(dfn[cx], lef[cx], 1);
cal_ans(cn, -b[cm].qiu(dfn[pos[cn]]));
}
void shan_1(int cn, int cm)
{
if(cm<0) return;
int cx = ST.tiao(pos[cn], val[cm]);
a[cm].jia(dfn[pos[cn]], -1);
b[cm].jia(dfn[cx], lef[cx], -1);
}
void zeng(int cn) {zeng_1(cn, len); }///////////////////////////////////////////////////////////////////////////
void jiesuan(int cm) {for(int i = 1;i<=c[cm].zlen;i++) cal_ans(c[cm].zhan[i], b[cm].qiu(dfn[pos[c[cm].zhan[i]]])); }
void bing()///////////////////////////////////////////////////////////////////////////
{
jiesuan(len);
for(int i = c[len].shan();i;i = c[len].shan()) shan_1(i, len), zeng_1(i, len-1);
}
}SP;
int n,m;
char c[MAXN+1];
int zhong[MAXN+1], size[MAXN+1];
int ans[MAXN+1];
void dfs_p(int cn, Tree_B &t)
{
zhong[cn] = 0; size[cn] = 1;
for(int i = t.head[cn];i;i = t.a[i].ne)
{
int y = t.a[i].b;
dfs_p(y, t); size[cn] += size[y];
if(size[y] > size[zhong[cn]]) zhong[cn] = y;
}
}
void dfs_z(int cn, Tree_B &t)
{
printf("in dfs_z : cn = %d
", cn);
if(t.dy[cn]) SP.zeng(cn);
printf(" zeng over
");
if(!zhong[cn]) return;
dfs_z(zhong[cn], t);
for(int i = t.head[cn];i;i = t.a[i].ne)
{
int y = t.a[i].b;
if(y == zhong[cn]) continue;
SP.set_new(m-1-t.shen[y]);
dfs_z(y, t);
SP.bing();
}
}
void getit(char c[], int &clen)
{
while(!isalpha(c[1] = getchar())); clen = 1;
while(isalpha(c[++clen] = getchar())); clen--;
}
void yuchu()
{
printf("in yuchu
");
S.build();
for(int i = 1;i<=n-m+1;i++) S.jia(c[i]-'a', i);
for(int i = n-m+2;i<=n;i++) S.jia(c[i]-'a', 0);
S.anpai(T1);
printf("T1 anpaied :
"); T1.outit();
S.build();
for(int i = n;i>=m;i--) S.jia(c[i]-'a',i-m+1);
for(int i = m-1;i>=1;i--) S.jia(c[i]-'a', 0);
S.anpai(T2);
printf("T2 anpaied :
"); T2.outit();
SP.build(T2);
printf("Szsz_pile built
");
dfs_p(1, T1);
printf("dfs_p over
");
memset(ans,0,sizeof(ans));
}
void cal_ans(int cn, int cm) {ans[cn] += cm; }
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
Read(n); Read(m); getit(c, n); yuchu();
SP.set_new(m-1); dfs_z(1, T1);
for(int i = 1;i<=n-m+1;i++) Write(ans[i]), putchar(' '); puts("");
return 0;
}
#include<bits/stdc++.h>
#define LL long long
#define MAXN 2010
using namespace std;
template<typename T>void Read(T &cn)
{
char c; int sig = 1;
while(!isdigit(c = getchar())) if(c == '-') sig = -1; cn = c-48;
while(isdigit(c = getchar())) cn = cn*10+c-48; cn*=sig;
}
template<typename T>void Write(T cn)
{
if(cn < 0) {putchar('-'); cn = 0-cn; }
int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
while(cn) wei++, cm = cm*10+cn%10, cn/=10;
while(wei--) putchar(cm%10+48), cm /= 10;
putchar(cx+48);
}
template<typename T>void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T>void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
struct qwe{
int l,r;
int w;
void getit() {Read(l); Read(r); Read(w); }
};
struct Seg{
struct node{
LL p, da;
void zeng(int cn) {p = p + cn; da = da + cn; }
};
node t[4000*4+1];
int L, R;
void build(int cn, int l, int r)
{
t[cn].p = t[cn].da = 0;
if(l == r) return;
int zh = (l+r)>>1; build(cn<<1,l,zh); build((cn<<1)|1,zh+1,r);
}
void jia(int cn, int cl, int cr, LL cm, int l, int r)
{
if(cl <= l && r <= cr) {t[cn].zeng(cm); return; }
int zh = (l+r)>>1; tui(cn);
if(cl <= zh) jia(cn<<1, cl, cr, cm, l, zh);
if(cr > zh) jia((cn<<1)|1,cl,cr,cm,zh+1,r);
update(cn);
}
LL qiu(int cn, int cl, int cr, int l, int r)
{
if(cl <= l && r <= cr) return t[cn].da;
tui(cn); int zh = (l+r)>>1; LL guo = 0;
if(cl <= zh) guo = qiu(cn<<1,cl,cr,l,zh);
if(cr > zh) Max(guo, qiu((cn<<1)|1,cl,cr,zh+1,r));
return guo;
}
void tui(int cn) {t[cn<<1].zeng(t[cn].p); t[(cn<<1)|1].zeng(t[cn].p); t[cn].p = 0; }
void update(int cn) {t[cn].da = max(t[cn<<1].da, t[(cn<<1)|1].da); }
void build_ou(int cl, int cr) {L = cl; R = cr; build(1,L,R); }
void jia_ou(int cl, int cr, LL cm) {/*printf("in jia : %d %d %lld
",cl,cr,cm); */jia(1,cl,cr,cm,L,R); }
LL qiu_ou(int cl, int cr) {/*printf("in qiu : %d %d
",cl,cr); */return qiu(1,cl,cr,L,R); }
}T1, T2;
int n;
qwe a[MAXN+1];
LL duo[MAXN+1];
LL he;
int zhan1[MAXN+1], zlen1;
int zhan2[MAXN+1], zlen2;
LL zuo1()
{
LL ans = 0;
// printf("n = %d
",n);
// for(int i = 1;i<=n;i++) printf("[%d,%d) %d
",a[i].l,a[i].r,a[i].w);
for(int i = 0;i<=3999;i++)
{
LL guo = 0;
for(int j = 1;j<=n;j++)
{
if(a[j].l < a[j].r) {
if(a[j].l <= i && i < a[j].r) guo = guo + a[j].w;
}
else {
if(i < a[j].r || a[j].l <= i) guo = guo + a[j].w;
}
}
// if(guo == 3135767 && ans != guo) {
// printf("i = %d
",i);
// for(int j = 1;j<=n;j++)
// {
// if(a[j].l < a[j].r) {
// if(a[j].l <= i && i < a[j].r) printf("%d(%d %d %d) ",j,a[j].l,i,a[j].r);
// }
// else {
// if(i < a[j].r || a[j].l <= i) printf("%d ",j);
// }
// }
// puts("");
// }
Max(ans, guo);
// if(i == 5) printf(" guo = %lld
",guo);
}
// printf("re zuo1 : ans = %lld
",ans);
return ans;
}
int Cmp1(int cn, int cm) {return a[cn].r < a[cm].r; }
int Cmp2(int cn, int cm) {return a[cn].l < a[cm].l; }
LL suan()
{
T1.build_ou(0,3999); T2.build_ou(0,3999);
zlen1 = zlen2 = 0;
for(int i = 1;i<=n;i++)
{
if(a[i].l <= 0) {
zhan1[++zlen1] = i;
T1.jia_ou(0, a[i].r, a[i].w);
}
else {
zhan2[++zlen2] = i;
}
}
sort(zhan1+1, zhan1+zlen1+1, Cmp1);
sort(zhan2+1, zhan2+zlen2+1, Cmp2);
int xian = 0;
LL ans = 0;
// cerr<<"pred"<<endl;
// printf("pred
");
for(int i = 1;i<=zlen2;i++)
{
while(xian < zlen1 && a[zhan1[xian+1]].r < a[zhan2[i]].l)
{
xian++;
T2.jia_ou(a[zhan1[xian]].l+4000, 3999, a[zhan1[xian]].w);
}
T2.jia_ou(a[zhan2[i]].l, a[zhan2[i]].r, a[zhan2[i]].w);
Max(ans, T1.qiu_ou(a[zhan2[i]].l, a[zhan2[i]].l) + T2.qiu_ou(a[zhan2[i]].l, a[zhan2[i]].r));
}
return ans;
}
LL zuo2()
{
LL ans = 0;
for(int i = 1;i<=n;i++) if(a[i].l > a[i].r) a[i].l -= 4000;
for(int i = 0;i<=3999;i++)
{
for(int j = 1;j<=n;j++) a[j].l -= i, a[j].r -= i;
// if(i == 5) for(int i = 1;i<=n;i++) printf("[%d,%d] %d
",a[i].l,a[i].r,a[i].w);
for(int j = 1;j<=n;j++) if(a[j].r <= 0) a[j].r += 4000, a[j].l += 4000;
for(int j = 1;j<=n;j++) a[j].r--;
// if(i == 3000) for(int i = 1;i<=n;i++) printf("%d : [%d,%d] %d
",i,a[i].l,a[i].r,a[i].w);
Max(ans, suan());
for(int j = 1;j<=n;j++) a[j].r++;
for(int j = 1;j<=n;j++) a[j].l += i, a[j].r += i;
for(int j = 1;j<=n;j++) if(a[j].l >= 4000) a[j].r -= 4000, a[j].l -= 4000;
}
for(int i = 1;i<=n;i++) if(a[i].l < 0) a[i].l += 4000;
// printf("ans = %lld
",ans);
return ans;
}
int main()
{
freopen("epitaph.in","r",stdin);
freopen("epitaph.out","w",stdout);
Read(n);
for(int i = 1;i<=n;i++) a[i].getit();
Write(max(zuo2(), zuo1())); puts("");
return 0;
}