D - Ki

题意:给一棵有根树,节点1为根,有$Q$次操作,每次操作将一个节点及其子树的所有节点的权值加上一个值,问最后每个节点的权值。

思路:dfs序再差分一下就行了。

#include <bits/stdc++.h>
using namespace std; const int N = 2e5 + ;
vector<int> G[N];
int dfn[N], id, n, q, last[N], pos[N];
long long val[N], ans[N]; void dfs(int u, int fa) {
dfn[u] = ++id;
pos[id] = u;
for (auto v: G[u]) {
if (v == fa) continue;
dfs(v, u);
}
last[u] = id;
} int main() {
scanf("%d%d", &n, &q);
for (int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(, );
while (q--) {
long long x;
int u;
scanf("%d%lld", &u, &x);
val[dfn[u]] += x;
val[last[u] + ] -= x;
}
for (int i = ; i <= n; i++) {
val[i] += val[i - ];
ans[pos[i]] = val[i];
}
for (int i = ; i <= n; i++)
printf("%lld%c", ans[i], " \n"[i == n]);
return ;
}

E - Strings of Impurity

题意:给两个字符串$s$, $t$,把$s$复制$10^{100}$次成$s'$,即'abc'变成'abcabc...abcabc',问$t$作为$s'$的子序列出现,最早结束位置的下标,不存在则输出-1。
如样例一
$s'$: conteStcONtest
$t$: son
大写表示匹配上的位置,答案为10。

思路:子序列问题都可以用一个$ne[i][j]$表示$s$串中到了$i$位置,下一个$j$字符的位置,从后往前扫可以预处理出来,然后统计答案就用$t$来贪心地匹配$s$,能匹配多前就多前,当前位置之后没有$t_{j}$这个字符了,就必须进入下一个$s$串中,然后答案只需要看跳了多少个$s$串和最后的位置在哪。

#include <bits/stdc++.h>
using namespace std; const int N = 1e5 + ; char s[N], t[N];
int ne[N][];
int fir[]; int main() {
scanf("%s%s", s + , t + );
int len1 = strlen(s + ), len2 = strlen(t + );
for (int i = len1; i >= ; i--) {
for (int j = ; j < ; j++)
ne[i][j] = fir[j];
if (i) fir[s[i] - 'a'] = i;
}
int id = , ans = ;
for (int i = ; i <= len2; i++) {
if (ne[id][t[i] - 'a']) {
id = ne[id][t[i] - 'a'];
} else {
if (!fir[t[i] - 'a']) {
puts("-1");
return ;
}
id = fir[t[i] - 'a'];
ans++;
}
}
printf("%lld\n", 1LL * (ans - ) * len1 + id);
return ;
}

F - Coincidence

题意:给定 $L$ 和 $R$,问有多少对 $x, y\left(L \leq x \leq y \leq R\right)$ 满足$y$ Mod $x = y$ XOR $x$

思路:
$\because y$ Mod $x < x$
$\therefore y$ XOR $x < x$
所以说明$y$和$x$最高位的1必须在同一个位置
$\therefore \dfrac {y}{x} < 2$
$\therefore y - x = y$ XOR $x$
举几个例子就会发现其实满足这个条件的$y$和$x$在二进制下是可以不用借位直接相减的,比如
$y$: 10110(22)
$x$: 10010(18)
此时$y$ XOR $x = y - x = 4$
就可以得到$y$ And $x = x$且$x$和$y$最高位的1的位置相同
然后就可以数位DP做。
数位DP同时枚举两个数二进制的情况,其中$y$不超过$R$,$x$不小于$L$,当前还有前导零的情况下,这一位$y$和$x$必须相同,之后保证一下$x$的每一位不能大于$y$(即出现$y$当前位是0,$x$当前位是1)就行了。

#include <bits/stdc++.h>
#define ll long long
using namespace std; const int MOD = 1e9 + ;
ll dp[][][][]; ll solve(ll l, ll r, int pos, bool limit1, bool limit2, bool lead) {
if (pos < ) return ;
ll &ans = dp[pos][limit1][limit2][lead];
if (ans != -) return ans;
ans = ;
int up2 = limit2 ? (r >> pos & ) : ;
int up1 = limit1 ? (l >> pos & ) : ;
for (int i = up1; i <= ; i++)
for (int j = ; j <= up2; j++) {
if (i > j) continue;
if (lead && i != j) continue;
(ans += solve(l, r, pos - , limit1 && i == up1, limit2 && j == up2, lead && j == )) %= MOD;
}
return ans;
} int main() {
memset(dp, -, sizeof(dp));
ll l, r;
scanf("%lld%lld", &l, &r);
printf("%lld", solve(l, r, , , , ));
return ;
}

Atcoder Beginner Contest 138 简要题解的更多相关文章

  1. 2018.09.08 AtCoder Beginner Contest 109简要题解

    比赛传送门 水题大赛? 全是水题啊!!! T1 ABC333 就是判断是不是两个数都是奇数就行了. 代码: #include<bits/stdc++.h> using namespace ...

  2. KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解

    KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解 哦淦我已经菜到被ABC吊打了. A - Century 首先把当前年 ...

  3. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  4. AtCoder Beginner Contest 089完整题解

    A - Grouping 2 Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There a ...

  5. AtCoder Grand Contest 039 简要题解

    从这里开始 比赛目录 Problem A Connection and Disconnection 简单讨论即可. Code #include <bits/stdc++.h> using ...

  6. 2018.09.02 Atcoder Regular Contest 102简要题解

    比赛传送门 T1 Triangular Relationship 分析之后发现有两种情况: 1. n为奇数,那么所有数都是k的倍数. 2. n为偶数,那么所有数都是k/2的倍数. 然后就可以愉快A题了 ...

  7. AtCoder Grand Contest 040 简要题解

    从这里开始 比赛目录 A < B < E < D < C = F,心情简单.jpg. Problem A >< 把峰谷都设成 0. Code #include &l ...

  8. AtCoder Grand Contest 035 简要题解

    从这里开始 题目目录 Problem A XOR Circle 你发现,权值的循环节为 $a_0, a_1, a_0\oplus a_1$,然后暴力即可. Code #include <bits ...

  9. AtCoder Grand Contest 036 简要题解

    从这里开始 比赛目录 Problem A Triangle 考虑把三角形移到和坐标轴相交,即 然后能够用坐标比较简单地计算面积,简单构造一下就行了. Code #include <bits/st ...

随机推荐

  1. [转帖]图解分布式一致性协议Paxos

    图解分布式一致性协议Paxos https://www.cnblogs.com/hugb/p/8955505.html   Paxos协议/算法是分布式系统中比较重要的协议,它有多重要呢? <分 ...

  2. 使用Fiddler抓包、wireshark抓包分析(三次握手、四次挥手深入理解)

    ==================Fiddler抓包================== Fiddler支持代理的功能,也就是说你所有的http请求都可以通过它来转发,Fiddler代理默认使用端口 ...

  3. Docker的安装与使用

    Docker的安装 (1)卸载老版本yum remove docker \                  docker-client \                  docker-clien ...

  4. 简洁的 Python Schema

    目录 Python Schema使用说明 1. Schema是什么? 2. 安装 1. 给Schema类传入类型(int.str.float等) 2. 给Schema类传入可调用的对象(函数.带__c ...

  5. C# Winform 打印控件PrintDocument

    由于本着节约的原则,这里的打印都只是保存为.oxps格式的文件. 在我调试时每次打印完成后,窗体都会自己闪退. 在网上并没有相关资料,经过加入断点确认问题在 private void btnPrint ...

  6. [Leetcode] 1120. Maximum Average Subtree

    Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subt ...

  7. 探索etcd,Zookeeper和Consul一致键值数据存储的性能

    这篇博文是探索三个分布式.一致性键值数据存储软件性能的系列文章中的第一篇:etcd.Zookeeper和Consul,由etcd团队所写,可以让我们全面地了解如何评估三个分布式一致存储软件的性能.翻译 ...

  8. 几何不变矩--Hu矩

    [图像算法]图像特征: ---------------------------------------------------------------------------------------- ...

  9. python后端链接数据库-----MySQLdb

    连接数据库之前请先确认好以下事宜: 1.已经建议好相应的数据库 2.在数据库中已经建立了相应的表 3.已经安装了MySQldb模块 示例: import MySQLdb # 打开数据库连接 db = ...

  10. 【开发笔记】- 将MySQL数据库表中自增ID从0开始

    命令: 用于清空某表的数据 且让自增的id重新从0开始 truncate table 你的表名