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. 027 ElasticSearch----全文检索技术02---快速入门

    1.基本概念 Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的. 注意:6.0之前的版本有type(类型)概念,type相当于关系数据库的表,E ...

  2. 1083 是否存在相等的差 PAT (Basic Level)

    题目链接: https://pintia.cn/problem-sets/994805260223102976/problems/994805260780945408 分析: 将某个差值的次数存在数组 ...

  3. (原创)使用C#开发PLC上位机监控系统客户端应用程序

    PLC客户端监控系统的特点: 0.客户端系统软件可部署在 多个管理层的PC机上,或者需要部署在距离服务器较远区域的PC机上,通过网线连接到服务器端的交换机. 1应用范围: (1)所有客户端都只有监视功 ...

  4. 【WEB基础】HTML & CSS 基础入门(10)布局与定位

    块级元素和行内元素 HTML里的元素可以分为块级元素和行内元素两大类:

  5. Spring MVC Web.xml配置

    Web.xml spring&spring mvc 在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个 ...

  6. windows查看某个端口被哪个进程占用

    找出端口对应的PID netstat -ano | findstr 8080 帮助命令netstat -? -a 显示所有连接和侦听端口. -n 以数字形式显示地址和端口号. -o 显示拥有的与每个连 ...

  7. javaScript 一些小技巧

    日历 创建过去七天的数组,如果将代码中的减号换成加号,你将得到未来7天的数组集合 // 创建过去七天的数组 [...Array(7).keys()].map(days => new Date(D ...

  8. Beego学习笔记四:编写Model

    MVC实践一:编写模型 1>     打开mysql数据库,设计表的结构 <1>登录mysql数据库,如下 <2>这三个标注的参数皆有用,需要谨记. <3>创 ...

  9. Android-----spinner组件使用(实现下单)

    list view组件和spinner组件使用方法类似,从string.xml中通过entries获取数据显示.但如果要显示的列表项无法在执行前确定,或是要在程序执行的过程中变更选项内容,通过entr ...

  10. SAP技术 - How to create a CDS redirect view for a given database table

    Scenario Suppose we have a database table A, and then we create a CDS redirect view B for it, then e ...