题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节点,然后数量就变成了 u + v - lca * 2.专题里那道算点权的应该算原题吧.1A = =,强行做模板题提高自信. 代码: #include<cmath> #include<set> #include<map> #include<queue> #incl…
1000ms 262144K   DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what…
题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k​\) 分析 在树的每个点上建\(1​\)到\(i​\)的权值线段树,查询的时候同时跑\(u,v,lca(u,v)​\)三个版本的线段树,查询\(1​\)到\(k​\)的树上差分和\(val[u]+val[v]-2*val[lca]​\) Code #include<bits/stdc++.h> #define fi first #define se second #define pb push_b…
传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说,这道题,一眼看去,就是树链剖分,然鹅,太久没写树链剖分的我一时也木有思路: 今天上午把树链剖分温习了一遍,做了个模板题: 下午再想了一下这道题,思路喷涌而出............ 首先,介绍一下相关变量: int fa[maxn];//fa[u]:u的父节点 int son[maxn];//son…
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Cl…
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Cl…
D题:马的管辖 二进制枚举方案.判断该方案是否全部能被覆盖,将最优方案存下来并进行剪枝. #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; int vis[15][15]; int ans=0x3f3f3f3f; int dx[]= {1,1,2,2,-1,-1,-2,-2};…
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; ][]; ,,-,}; ,-,,}; int ans; void dfs(int x,int y,int k) { ;i<;i++) { int fx=x+dx[i]; int fy=y+dy[i]; &&fx<=&&fy>…
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int ans; void dfs(int x,int sum) { ) { ans++; return; } ) return; ;i<=(-sum)/x;i++) { dfs(x+,sum+i*x); } } int main() { ans=; dfs(,);…
样例输入: 3 1 -2 1 样例输出: 2 方法一: 将环形数组拆分成为普通数组,(通过搬运复制数据到尾部),再求前缀和,找出最大前缀和.因为枚举了每一个起点,所以最大连续和也一定出现在前缀和中... #include<iostream> using namespace std; int n; ]; ]; int main(){ /*freopen("in.txt","r",stdin);*/ cin>>n; ;i<=n;i++){…
样例输入: 3 4 5 1 0 0 0 1 1 0 1 0 1 1 0 1 0 1 1 0 0 0 1 5 6 1 1 1 1 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 10 10 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1…
样例输入: 6 1 9 7 3 5 5 样例输出: 4 思路:贪心,选错贪心思路,只能过一小部分数据,正确贪心思路:从前一半遍历,在后一半中找到比当前元素的两倍大的数(因为这里指针不会后移,所以可以采用双指针) 代码: #include<bits/stdc++.h> using namespace std; int arr[500005]; int n; int main(){ cin>>n; for(int i=0;i<n;i++){ scanf("%d"…
样例输入: 3 ba a aba 样例输出: 2 3 1 思路一:暴力,只能过50%数据,枚举每一个字符串,内层枚举其他字符串判断是否以这个字符串为后缀 思路二:哈希表,存储每一个后缀的数目,string.substr函数取后缀 substr用法: 代码一: #include <bits/stdc++.h> using namespace std; string s[10010]; int n; int main(){ cin>>n; for(int i = 0;i<n;i+…
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int f[N], a[N]; int n; //二分查找: 在f数组中查找到第一个比x大的数的下标 int find(int l, int r, int x) { while (l < r) { int mid = (l + r) / 2; if (f[mid] < x) { l = mid + 1; } else { r = mid; } }…
思路:从l枚举到r肯定超时,这时我们要转变思路!题目让我们求一个区间内的d的倍数,只需要求出r/d - l/d就是区间内d倍数的个数. 代码: #include <iostream> using namespace std; long long r = 12302135942453; int l = 1032; int d = 234; int main(){ cout<<r/d - 1032/234<<endl; //左右区间都是闭合的 return 0; }…
找质数 思路:数据大,用线性筛,筛选素数表,最后查表:题目让我们查找相加等于n的两个数,那么我们就枚举1个素数a,在素数表中查找是否存在n-a也是素数. 注意事项:数据大,不宜用输入输出流,cout.cin.endl这些改成printf scanf 代码: #include<cstdio> int t; int n; int prime[1000010]; //筛选素数 打表 void Prime(){ for (int i = 2; i <= 1000000; i++) { prime…
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; ; struct node{ int l,r,cnt; }tree[maxx*]; int head[maxx],rk[maxx],siz[maxx],top[maxx],son[maxx],d[m…
The Nth Iteam 题意:F(0)=1,F(1)=1,F(n)=3*F(n-1)+2*F(n-2) (n>=2) ,F(n) mod 998244353.给出Q跟N1,Ni=Ni-1^(F(Ni-1)*F(Ni-1)),求F(N1)^F(N2)^...^F(NQ) 这个比赛的E跟H数据都水得很,但题还是不错的. 比赛时是求了个循环节,又进行了矩阵的降幂,然后本地+测试在时限内跑出了几组1e7的数据,才敢交的,但没想到数据那么水,矩阵快速幂+map记忆化一下就能过. #include<c…
http://poj.org/problem?id=2796 https://nanti.jisuanke.com/t/38228 背景 给定一个序列,对于任意区间,min表示区间中最小的数,sum表示区间和,求使得min*sum最大的区间或区间值. POJ-2796中,序列的值非负,而在网络赛I题中,序列存在负值. 解法分析 直觉上,后者是前者的拓展,我们先考虑序列非负的情况. 非负情况 设序列存储在数组a中.当我们考虑数值a[i]作为区间最小值时,显然我们应该向i的左右两侧扩展并终止于遇到更…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6704 题意为查询子串s[l...r]第k次出现的位置. 写完博客后5分钟的更新 写完博客才发现这份代码和杭电的代码查重了.... 为什么会变成这样呢?是我先交的,明明是我先交的… 激动!!第一次网络赛做出这种(板子)题. 首先求一下后缀数组的Height,我们知道Height数组表示两个排名相邻的后缀的最长公共前缀,则Height数组的区间最小值即为区间排名相邻的后缀的最长公共前缀. 我们想知道那些…
H.Ryuji doesn't want to study 27.34% 1000ms 262144K   Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That…
传送门 发现这题选或不选对状态的优劣程度不会产生影响,如果已经确定了两个数a和b,那么最优的首项和公比也都是唯一确定的, 与对于后面的数x,加进去也好不加进去也好,首项和公比依旧是原来的 于是我们用尺取算法,用两个指针来扫一遍, 如果只有一个数且下一个数能被整除,就加进去,然后确定首项和公比 如果只有一个数且下一个数不能整除,两个指针直接指向下一个数 如果有多个数且下一个数满足公式,就加进来 如果有多个数且下一个数不满足公式,两个指针直接指向下一个数 这样对于最优解,一定是可以找到的 顺便说下最…
传送门 很水的题目啦QAQ #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<map> #include<set> #include<queue> #include<vector> #define INF 0x7f7f7f7f #define pii pa…
传送门 这题可以用线段树来维护 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<map> #include<set> #include<queue> #include<vector> #define INF 0x7f7f7f7f #define pii…
传送门 用hash,因为map的复杂度可能在这题中因为多一个log卡掉,但是hash不会 可能因为这个生成的随机数有循环的情况,不是完全均匀的 而且这题hash表的长度也可以开的很大 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<map> #include<set> #inc…
传送门 这题有点坑啊 设A为两边颜色不同的角,B为两边颜色相同的角 那么考虑三种三角形:异色,同色,其他 对于任何一个异色三角形,一定会有三个颜色不同的角, 对于任何一个同色三角形,一定会有零个颜色不同的角, 对于任何一个其他三角形,一个会有两个颜色不同的角, 那么A一定等于异色三角形数目*3+其他三角形数目*2 对于任何一个异色三角形,一定会有零个颜色相同的角, 对于任何一个同色三角形,一定会有三个颜色相同的角, 对于任何一个其他三角形,一个会有一个颜色相同的角, 那么B一定等于同色三角形数目…
传送门 简单几何+逆序对 发现当两条直线甲乙与平板的交点在上面甲在较左的位置,那么下面甲在较右的位置就可以相交 然后把上面的位置排下序,下面离散化+树状数组即可 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<map> #include<set> #include<que…
传送门 dp,注意边界 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<map> #include<set> #include<queue> #include<vector> #define INF 0x7f7f7f7f #define pii pair…
火山喷发 火山喷发对所有附近的生物具有毁灭性的影响.在本题中,我们希望用数值来模拟这一过程. 在环境里有 n 个生物分别具有 A​1​​,A​2​​,⋯,A​n​​点生命值,一次火山喷发总计 MM 轮,每轮造成 11 点伤害,等概率地分给所有存活的生物,即如果目前有 K 个活着的生物,每个生物受到这点伤害的概率是 1/K​​.如果一个生物的生命值减为 0,它会立即死去,此后都不会再占用受到伤害的概率.如果没有生物存活,那么将没有生物会受到伤害. 现在你的任务是,给定 n,M 和全部生物的生命值,…
T1:小X的质数 小 X 是一位热爱数学的男孩子,在茫茫的数字中,他对质数更有一种独特的情感.小 X 认为,质数是一切自然数起源的地方. 在小 X 的认知里,质数是除了本身和 1 以外,没有其他因数的数字. 但由于小 X 对质数的热爱超乎寻常,所以小 X 同样喜欢那些虽然不是质数,但却是由两个质数相乘得来的数. 于是,我们定义,一个数是小 X 喜欢的数,当且仅当其是一个质数,或是两个质数的乘积. 而现在,小 X 想要知道,在 L 到 R 之间,有多少数是他喜欢的数呢? 输入格式 第一行输入一个正…