好多东西都不熟练……

数论

数论分块「bzoj2956: 模积和

10.28.2018

 #include<bits/stdc++.h>
typedef long long ll;
const int MO = ;
const int inv6 = ; ll n,m,ans,del; inline void Add(ll &x, ll y){x = ((x+y)%MO+MO)%MO;}
ll sum(ll x){return x*(x+)%MO*(*x+)%MO*inv6%MO;}
ll calc(ll x)
{
ll ret = ;
for (ll i=, j=; i<=x; i=j+)
{
j = x/(x/i);
Add(ret, 1ll*(x/i)*(i+j)*(j-i+)/%MO);
}
return ((x%MO*x%MO-ret)+MO)%MO;
}
int main()
{
scanf("%lld%lld",&n,&m);
if (n > m) std::swap(n, m);
ans = calc(n)*calc(m)%MO;
del = n*m%MO*n%MO;
for (ll i=, j=; i<=n; i=j+)
{
j = std::min(n/(n/i), m/(m/i));
ll s1 = (sum(j)-sum(i-))*(n/i)%MO*(m/i)%MO;
ll s2 = (n*(m/i)%MO+m*(n/i)%MO)%MO*((i+j)*(j-i+)/%MO);
Add(del, (s1-s2)%MO);
}
Add(ans, -del);
printf("%lld\n",ans);
return ;
}

图论

点-树链剖分「1036: [ZJOI2008]树的统计Count」

10.24.2018

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ;
const int INF = 0x3f3f3f3f; struct node
{
int tot,fa,son,top;
}a[maxn];
int n,m;
char opt[];
int f[maxn<<][];
int chTot,chain[maxn],cnVal[maxn],d[maxn],dep[maxn];
int edgeTot,head[maxn],nxt[maxm],edges[maxm]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void addedge(int u, int v)
{
edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = u, nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void pushup(int rt)
{
f[rt][] = std::max(f[rt<<][], f[rt<<|][]);
f[rt][] = f[rt<<][]+f[rt<<|][];
}
void build(int rt, int l, int r)
{
f[rt][] = -INF;
if (l==r){
f[rt][] = f[rt][] = cnVal[l];
return;
}
int mid = (l+r)>>;
build(rt<<, l, mid), build(rt<<|, mid+, r);
pushup(rt);
}
void dfs1(int x, int fa)
{
a[x].fa = fa, a[x].tot = ;
dep[x] = dep[fa]+, a[x].son = -;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i];
if (v!=fa){
dfs1(v, x);
a[x].tot += a[v].tot;
if (a[x].son==-||a[v].tot > a[a[x].son].tot)
a[x].son = v;
}
}
}
void dfs2(int x, int top)
{
chain[x] = ++chTot, cnVal[chTot] = d[x], a[x].top = top;
if (a[x].son==-) return;
dfs2(a[x].son, top);
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i];
if (v!=a[x].son&&v!=a[x].fa) dfs2(v, v);
}
}
void modify(int rt, int l, int r, int pos, int c)
{
if (l==r){
f[rt][] = f[rt][] = c;
return;
}
int mid = (l+r)>>;
if (pos <= mid) modify(rt<<, l, mid, pos, c);
else modify(rt<<|, mid+, r, pos, c);
pushup(rt);
}
int queryMx(int rt, int L, int R, int l, int r)
{
if (L <= l&&r <= R) return f[rt][];
int mid = (l+r)>>, ret = -INF;
if (L <= mid) ret = queryMx(rt<<, L, R, l, mid);
if (R > mid) ret = std::max(ret, queryMx(rt<<|, L, R, mid+, r));
return ret;
}
int querySm(int rt, int L, int R, int l, int r)
{
if (L <= l&&r <= R) return f[rt][];
int mid = (l+r)>>, ret = ;
if (L <= mid) ret = querySm(rt<<, L, R, l, mid);
if (R > mid) ret += querySm(rt<<|, L, R, mid+, r);
return ret;
}
int queryChainMx(int x, int y)
{
int ret = -INF;
while (a[x].top!=a[y].top)
{
if (dep[a[x].top] > dep[a[y].top]) std::swap(x, y);
ret = std::max(ret, queryMx(, chain[a[y].top], chain[y], , n));
y = a[a[y].top].fa;
}
if (dep[x] > dep[y]) std::swap(x, y);
ret = std::max(ret, queryMx(, chain[x], chain[y], , n));
return ret;
}
int queryChainSm(int x, int y)
{
int ret = ;
while (a[x].top!=a[y].top)
{
if (dep[a[x].top] > dep[a[y].top]) std::swap(x, y);
ret += querySm(, chain[a[y].top], chain[y], , n);
y = a[a[y].top].fa;
}
if (dep[x] > dep[y]) std::swap(x, y);
ret += querySm(, chain[x], chain[y], , n);
return ret;
}
int main()
{
memset(head, -, sizeof head);
n = read();
for (int i=; i<n; i++) addedge(read(), read());
for (int i=; i<=n; i++) d[i] = read();
dfs1(, );
dfs2(, );
build(, , n);
m = read();
while (m--)
{
scanf("%s",opt);
int x = read(), y = read();
if (opt[]=='C') modify(, , n, chain[x], y);
else if (opt[]=='M')
printf("%d\n",queryChainMx(x, y));
else printf("%d\n",queryChainSm(x, y));
}
return ;
}

打挂地方已标注。

边-树链剖分「bzoj2238: Mst」

10.28

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ;
const int INF = 0x3f3f3f3f; struct Edge
{
int u,v,w,id;
bool vis;
Edge(int a=, int b=, int c=):u(a),v(b),w(c) {}
}ev[maxm],edges[maxm];
struct node
{
int tot,son,top,fa;
}a[maxn];
int n,m,q,cnt,ans;
int dep[maxn],fa[maxn],chain[maxn],chTot,f[maxn<<];
int edgeTot,head[maxn],nxt[maxm];
bool evis[maxm]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
bool cmp1(Edge a, Edge b)
{
return a.w < b.w;
}
bool cmp2(Edge a, Edge b)
{
return a.id < b.id;
}
int get(int x){return x==fa[x]?x:fa[x]=get(fa[x]);}
void addedge(int u, int v, int c)
{
ans += c, fa[fa[u]] = fa[v];
edges[++edgeTot] = Edge(u, v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = Edge(v, u, c), nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void dfs1(int x, int fa)
{
a[x].tot = , a[x].son = a[x].top = -;
a[x].fa = fa, dep[x] = dep[fa]+;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].v;
if (v!=fa){
dfs1(v, x), a[x].tot += a[v].tot;
if (a[x].son==-||a[a[x].son].tot < a[v].tot) a[x].son = v;
}
}
}
void dfs2(int x, int top)
{
a[x].top = top, chain[x] = ++chTot;
if (a[x].son==-) return;
dfs2(a[x].son, top);
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].v;
if (v!=a[x].son&&v!=a[x].fa) dfs2(v, v);
}
}
void build(int rt, int l, int r)
{
f[rt] = INF;
if (l==r) return;
int mid = (l+r)>>;
build(rt<<, l, mid), build(rt<<|, mid+, r);
}
void Min(int &x, int y){x = x>y?y:x;}
void pushdown(int rt)
{
Min(f[rt<<], f[rt]), Min(f[rt<<|], f[rt]);
}
void modify(int rt, int L, int R, int l, int r, int c)
{
if (L > R) return;
if (L <= l&&r <= R){
f[rt] = std::min(f[rt], c);
return;
}
int mid = (l+r)>>;
pushdown(rt);
if (L <= mid) modify(rt<<, L, R, l, mid, c);
if (R > mid) modify(rt<<|, L, R, mid+, r, c);
}
int query(int rt, int L, int R, int l, int r)
{
if (L > R) return INF;
if (L <= l&&r <= R) return f[rt];
int mid = (l+r)>>, ret = INF;
pushdown(rt);
if (L <= mid) Min(ret, query(rt<<, L, R, l, mid));
if (R > mid) Min(ret, query(rt<<|, L, R, mid+, r));
return ret;
}
void modifyChain(int x, int y, int c)
{
while (a[x].top!=a[y].top)
{
if (dep[a[x].top] > dep[a[y].top]) std::swap(x, y);
modify(, chain[a[y].top], chain[y], , n, c);
y = a[a[y].top].fa;
}
if (dep[x] > dep[y]) std::swap(x, y);
modify(, chain[x]+, chain[y], , n, c);
}
int queryChain(int x, int y)
{
int ret = INF;
while (a[x].top!=a[y].top)
{
if (dep[a[x].top] > dep[a[y].top]) std::swap(x, y);
Min(ret, query(, chain[a[y].top], chain[y], , n));
y = a[a[y].top].fa;
}
if (dep[x] > dep[y]) std::swap(x, y);
Min(ret, query(, chain[x]+, chain[y], , n));
return ret;
}
int main()
{
memset(head, -, sizeof head);
n = read(), m = read();
for (int i=; i<=n; i++) fa[i] = i;
for (int i=; i<=m; i++)
ev[i].u = read(), ev[i].v = read(), ev[i].w = read(), ev[i].id = i;
std::sort(ev+, ev+m+, cmp1);
for (int i=; i<=m; i++)
if (get(ev[i].u)!=get(ev[i].v)){
int u = ev[i].u, v = ev[i].v;
ev[i].vis = evis[ev[i].id] = , cnt++, addedge(u, v, ev[i].w);
if (cnt==n-) break;
}
if (cnt!=n-){
q = read();
while (q--) puts("Not connected");
return ;
}
dfs1(, );
dfs2(, );
build(, , n);
for (int i=m; i; i--)
if (!ev[i].vis) modifyChain(ev[i].u, ev[i].v, ev[i].w);
std::sort(ev+, ev+m+, cmp2);
for (q=read(); q; q--)
{
int cse = read();
if (!evis[cse]) printf("%d\n",ans);
else{
int cnt = queryChain(ev[cse].u, ev[cse].v);
if (cnt!=INF) printf("%d\n",ans-ev[cse].w+cnt);
else puts("Not connected");
}
}
return ;
}

evis开成maxn一发RE

SPFA负权环「bzoj1715: [Usaco2006 Dec]Wormholes 虫洞」

10.24.2018

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ; struct Edge
{
int y,val;
Edge(int a=, int b=):y(a),val(b) {}
}edges[maxm];
int T,n,m,w;
bool vis[maxn];
int dis[maxn],edgeTot,head[maxn],nxt[maxm]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void addedge(int u, int v, int c)
{
edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
}
bool dfs(int x)
{
vis[x] = ;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].y;
if (dis[v] > dis[x]+edges[i].val){
dis[v] = dis[x]+edges[i].val;
if (vis[v]||dfs(v)){
vis[x] = ;
return ;
}
}
}
vis[x] = ;
return ;
}
int main()
{
T = read();
while (T--)
{
memset(head, -, sizeof head);
memset(dis, , sizeof dis);
edgeTot = , n = read(), m = read(), w = read();
for (int i=; i<=m; i++)
{
int u = read(), v = read(), c = read();
addedge(u, v, c), addedge(v, u, c);
}
for (int i=; i<=w; i++)
{
int u = read(), v = read(), c = read();
addedge(u, v, -c);
}
bool fl = ;
for (int i=; i<=n; i++)
if (dfs(i)){
puts("YES"), fl = ;
break;
}
if (!fl) puts("NO");
}
return ;
}

差分约束「poj1201Intervals」

10.26.2018

这个和线性规划相比,要把式子全都化成三角形不等式的形式。

所以小于等于或者大于等于的两种情况自己注意。

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ; struct Edge
{
int y,val;
Edge(int a=, int b=):y(a),val(b) {}
}edges[maxm];
int n,mx;
bool stk[maxn];
std::queue<int> q;
int dis[maxn],vis[maxn];
int edgeTot,head[maxn],nxt[maxm]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void addedge(int u, int v, int c)
{
edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
}
void spfa()
{
memset(dis, -0x3f3f3f3f, sizeof dis);
dis[] = , q.push();
while (q.size())
{
int tt = q.front();
q.pop(), stk[tt] = ;
for (int i=head[tt]; i!=-; i=nxt[i])
{
int v = edges[i].y, w = edges[i].val;
if (dis[v] < dis[tt]+w){
dis[v] = dis[tt]+w;
if (!stk[v]) stk[v] = , q.push(v);
}
}
}
}
int main()
{
memset(head, -, sizeof head);
n = read();
for (int i=; i<=n; i++)
{
int a = read()+, b = read()+, c = read();
mx = std::max(mx, b);
addedge(a-, b, c);
}
for (int i=; i<mx; i++) addedge(i, i+, ), addedge(i+, i, -);
spfa();
printf("%d\n",dis[mx]);
return ;
}

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ; struct Edge
{
int y,val;
Edge(int a=, int b=):y(a),val(b) {}
}edges[maxm];
int n,mx;
bool stk[maxn];
std::queue<int> q;
int dis[maxn],vis[maxn];
int edgeTot,head[maxn],nxt[maxm]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void addedge(int u, int v, int c)
{
edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
}
void spfa()
{
memset(dis, 0x3f3f3f3f, sizeof dis);
dis[mx] = , q.push(mx);
while (q.size())
{
int tt = q.front();
q.pop(), stk[tt] = ;
for (int i=head[tt]; i!=-; i=nxt[i])
{
int v = edges[i].y, w = edges[i].val;
if (dis[v] > dis[tt]+w){
dis[v] = dis[tt]+w;
if (!stk[v]) stk[v] = , q.push(v);
}
}
}
}
int main()
{
memset(head, -, sizeof head);
n = read();
for (int i=; i<=n; i++)
{
int a = read()+, b = read()+, c = read();
mx = std::max(mx, b);
addedge(b, a-, -c);
}
for (int i=; i<mx; i++) addedge(i, i+, ), addedge(i+, i, );
spfa();
printf("%d\n",-dis[]);
return ;
}

点树上差分「bzoj4390: [Usaco2015 dec]Max Flow」

11.5.2018

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ;
const int maxLog = ; int n,m;
int f[maxn][maxLog],sum[maxn],ans;
int edgeTot,head[maxn],edges[maxm],nxt[maxm],dep[maxn]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void addedge(int u, int v)
{
edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = u, nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void dfs(int x, int fa)
{
f[x][] = fa, dep[x] = dep[fa]+;
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i]!=fa) dfs(edges[i], x);
}
int lca(int u, int v)
{
if (dep[u] > dep[v]) std::swap(u, v);
for (int i=; i>=; i--)
if (dep[u] <= dep[v]-(<<i))
v = f[v][i];
if (u==v) return u;
for (int i=; i>=; i--)
if (f[u][i]!=f[v][i])
u = f[u][i], v = f[v][i];
return f[u][];
}
void fnd(int x)
{
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i]!=f[x][])
fnd(edges[i]), sum[x] += sum[edges[i]];
if (ans < sum[x]) ans = sum[x];
}
int main()
{
memset(head, -, sizeof head);
n = read(), m = read();
for (register int i=; i<n; i++) addedge(read(), read());
dfs(, );
for (register int j=; j<=; j++)
for (register int i=; i<=n; i++)
f[i][j] = f[f[i][j-]][j-];
for (register int i=; i<=m; i++)
{
register int u = read(), v = read(), ger = lca(u, v);
sum[u]++, sum[v]++, sum[ger]--, sum[f[ger][]]--;
}
fnd();
printf("%d\n",ans);
return ;
}

NOIP2018 - 一些板子的更多相关文章

  1. NOIP计划索引

    药丸的节奏 亟待解决的问题 灵光一现的trick 2018上学期刷题记录 NOIP2018 - 暑期博客整理 NOIP2018 - 一些板子 NOIP2018 - 每日填坑

  2. NOIP2018赛前停课集训记——最后的刷板子计划

    前言 再过两天就\(NOIP2018\)了. 于是,我决定不做其他题目,开始一心一意刷板子了. 这篇博客记录的就是我的刷板子计划. [洛谷3383][模板]线性筛素数 这种普及-的题目我还写挂了两次( ...

  3. NOIP2018 游记 QAQ

    写在前面: 本人初三党.NOIP前两个月不好好停课搞信竞愣是要搞文化课.于是,期中考与NOIP一起凉凉[微笑] 本人写的第一篇NOIP游记,各位大佬们随便看一看就好 Day -n 初赛71,竟然跟wx ...

  4. NOIP2018题解

    Preface 联赛结束后趁着自己还没有一下子把题目忘光,所以趁机改一下题目. 没有和游记一起写主要是怕篇幅太长不美观. 因此这里我们直接讲题目,关于NOIP2018的一些心得和有趣的事详见:NOIP ...

  5. NOIp2018停课刷题记录

    Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...

  6. NOIP2018滚粗记

    NOIP2018滚粗记 day 0 上午,说是可以休息,然后睡到快9点起来吃个早饭去了机房.刷了几个板子就十二点了 下午大概就是看别人总结,颓知乎,完全没心思写代码. 晚上不要求,然后在寝室颓了一下, ...

  7. 租酥雨的NOIP2018赛前日记

    租酥雨的NOIP2018赛前日记 离\(\mbox{NOIP2018}\)只剩下不到一个月的时间辣! 想想自己再过一个月就要退役了,觉得有必要把这段时间的一些计划与安排记录下来. 就从国庆收假开始吧. ...

  8. 【日记】NOIP2018

    day-2: 最后一次走出机房,刚下过几天的雨,感受到的是彻骨的寒意.下午离开教室,跟班主任请了接下来几天的假,班主任斜视了我一眼,哼了一声,确认了一下,不再理会我了.班里的同学或是忙着自己的作业,或 ...

  9. NOIP2018学军中学游记(11.09~11.11)

    前言 这篇博客记录的是我在\(NOIP2018\)提高组比赛中的经历. 这一次的\(NOIP\)是在学军中学举办的, 莫名感到一阵慌张. 但愿能有一个好成绩,不然就要\(AFO\)了... ... 说 ...

随机推荐

  1. 你必须学写 Python 装饰器的五个理由

    你必须学写Python装饰器的五个理由 ----装饰器能对你所写的代码产生极大的正面作用 作者:Aaron Maxwell,2016年5月5日 Python装饰器是很容易使用的.任何一个会写Pytho ...

  2. 黑马Spring学习 bean

  3. MySQL 查询练习记录

    MySQL 查询练习记录 最近在复习mysql,在b站上找了一个感觉还不错的视频,把视频中查询练习相关的内容记录了下来,以便自己日后查阅和复习. 视频连接:https://www.bilibili.c ...

  4. Avito Cool Challenge 2018-A. Definite Game(思维题)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. CSS3动画总结学习(一)

    参考文章: CSS3 Transitions, Transforms和Animation使用简介与应用展示 CSS 参考手册 动画的分类 平移动画 transform: 就是变换, 变换, 变换 也就 ...

  6. mongodb 分片技术

    MongoDB Sharding Cluster 分片集群 规划:10个实例:38017-38026 (1)configserver:3台构成的复制集(1主两从,不支持arbiter)38018-38 ...

  7. docker mysql安装

    Docker MySQL-Server 安装1.搜索docker search mysql# 一般会选择mysql-server 版本 2.拉取 docker pull mysql-server 3. ...

  8. windows下指定端口号起步memcached

    双击.exe启动的话,默认启动的端口是11211 ,要指定端口的话加 -p + 端口号,如: E:\tools\memcached-1.4.5-win32>memcached-1.4.5.exe ...

  9. Typora--Draw Diagrams With Markdown

    Typora Typora supports some Markdown extension for diagrams, you could enable this feature from pref ...

  10. Java基础语法(数组)

    第4天 Java基础语法 今日内容介绍 u 流程控制语句(switch) u 数组 第1章 流程控制语句 1.1 选择结构switch switch 条件语句也是一种很常用的选择语句,它和if条件语句 ...