codeforces 549F Yura and Developers

题意

给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数。

题解

分治,对于一个区间,找出最大值之后,分成两个区间。

至于统计答案,可以枚举小的那一端。

也可以结合熟练剖分的思想,由于dfs解决答案的过程是一棵二叉树,所以用全局变量保存当前信息,先做重儿子即可。

代码

\(O(nlog_2n)\)

PS:由于搜索树是二叉树,所以可以直接用全局变量维护当前处理区间的信息。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//--- const int N = 303030, M = 1010101; int n, k;
int a[N], f[22][N], b[M], g[N];
ll ans;
ll s[N]; inline int Max(int i, int j) {
return a[i] > a[j] ? i : j;
}
inline int st(int l, int r) {
int _ = log2(r-l+1);
return Max(f[_][l], f[_][r-(1<<_)+1]);
}
inline void upd(int p, int c) {
b[g[p]] += c;
} void solve(int l, int r) {
if(l>=r) {
if(l==r) upd(l, -1), ++ans;
return ;
}
int mid = st(l, r);
int l1 = l-1, r1 = mid-1;
int l2 = mid, r2 = r;
if(r1-l1 < r2-l2) {
rep(i, l, mid) upd(i, -1);
rep(i, l1, r1+1) {
ans += b[(s[i]+a[mid])%k];
}
upd(mid, -1);
solve(mid+1, r);
rep(i, l, mid) upd(i, 1);
solve(l, mid-1);
} else {
rep(i, mid, r+1) upd(i, -1);
upd(l-1, 1);
rep(i, l2, r2+1) {
ans += b[(s[i]-a[mid])%k];
}
upd(l-1, -1);
solve(l, mid-1);
rep(i, mid+1, r+1) upd(i, 1);
solve(mid+1, r);
}
} int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> k;
rep(i, 1, n+1) cin >> a[i], s[i] = s[i-1] + a[i], f[0][i] = i, g[i] = s[i]%k, upd(i, 1);
for(int i = 1; (1<<i) <= n; ++i) {
for(int j = 1; j+(1<<i)-1 <= n; ++j) {
f[i][j] = Max(f[i-1][j], f[i-1][j+(1<<(i-1))]);
}
}
solve(1, n);
cout << ans - n << endl;
return 0;
}

\(O(nlog_2^2n)\)

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//--- const int N = 303030, M = 1010101; int n, k;
int a[N], pr[N], ne[N];
ll s[N];
vi b[M];
pii e[N]; inline int qry(int l, int r, int x) {
int res = upper_bound(all(b[x]), r) - upper_bound(all(b[x]), l-1);
return res;
} int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> k;
b[0].pb(0);
rep(i, 1, n+1) cin >> a[i], s[i] = s[i-1] + a[i], b[s[i]%k].pb(i), e[i] = mp(a[i], i);
sort(e+1, e+1+n);
rep(i, 1, n+1) pr[i] = i-1, ne[i] = i+1;
ll ans = 0;
rep(_, 1, n+1) {
int i = e[_].se;
int l = pr[i]+1, r = ne[i]-1;
int l1 = l-1, r1 = i-1;
int l2 = i, r2 = r;
if(r1-l1 < r2-l2) {
rep(j, l1, r1+1) {
ans += qry(l2, r2, (s[j]+a[i])%k);
}
} else {
rep(j, l2, r2+1) {
ans += qry(l1, r1, (s[j]-a[i])%k);
}
}
pr[ne[i]] = pr[i];
ne[pr[i]] = ne[i];
}
cout << ans - n << endl;
return 0;
}

codeforces 549F Yura and Developers(分治、启发式合并)的更多相关文章

  1. ●CodeForces 549F Yura and Developers

    题链: http://codeforces.com/problemset/problem/549/F题解: 分治,链表. 考虑对于一个区间[L,R],其最大值在p位置, 那么答案的贡献就可以分为3部分 ...

  2. Codeforces 549F Yura and Developers

    probelm 题意 给定一个序列和一个mod值,定义[l,r]合法当l到r的全部元素和减去当中的最大值的结果能够整除mod.问共同拥有多少区间合法. 思路 一開始想的分治. 对于一个[l,r]我们能 ...

  3. Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map

    E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...

  4. SPOJ:Free tour II (树分治+启发式合并)

    After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...

  5. Codeforces 1455G - Forbidden Value(map 启发式合并+DP)

    Codeforces 题面传送门 & 洛谷题面传送门 首先这个 if 与 end 配对的结构显然形成一个树形结构,考虑把这棵树建出来,于是这个程序的结构就变为,对树进行一遍 DFS,到达某个节 ...

  6. Codeforces 208E - Blood Cousins(树上启发式合并)

    208E - Blood Cousins 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor.多次查询,给出 u k,问有多少个与 u 具有相同 k-ance ...

  7. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

  8. SPOJ Free TourII(点分治+启发式合并)

    After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...

  9. Codeforces 1156E Special Segments of Permutation(启发式合并)

    题意: 给一个n的排列,求满足a[l]+a[r]=max(l,r)的(l,r)对数,max(l,r)指的是l到r之间的最大a[p] n<=2e5 思路: 先用单调栈处理出每个点能扩展的l[i], ...

随机推荐

  1. java调用c/c++代码简单实现以及遇见的坑

    以下内容均来自互联网,感谢你们的分享,我只是使用的时候看这方便,可以称呼我“搬运工” 如有不合适的地方请与我联系,我会及时改正 首先你可能会遇见以下错误 第一个错误是你在vs编译器没有选择使用rele ...

  2. JAVA泛型——逆变

    在上篇<JAVA泛型——协变>这篇文章中遗留以下问题——协变不能解决将子类型添加到父类型的泛型列表中.本篇将用逆变来解决这个问题. 实验准备 我们首先增加以下方法,见代码清单1所示. 代码 ...

  3. 设置tomcat字符编码

    Tomcat的默认编码是ISO-8859-1,如果有是get请求时,会出现乱码,这种情况可以修改Tomcat的编码解决,当然也可以写个过滤器来解决. 在tomcat的conf目录下,编辑server. ...

  4. 【转】IIS网站浏览时提示需要用户名密码登录-解决方法

    打开iis,站点右键----属性----目录安全性----编辑----允许匿名访问钩选 IIS连接127.0.0.1要输入用户名密码的解决办法原因很多,请尝试以下操作: 1.查看网站属性——文档看看启 ...

  5. WPF实现夜间模式

    背景 项目中设计了一个黑色主题,稍加改正也可作为夜间模式,效果图如下: 原理 由于项目中存在地图,而地图完全是由位图组成,不能直接改变背景色,所以我在内容上面放置了一个黑色的Border作为遮罩.可通 ...

  6. SpringMVC笔记:annotation注解式开发

    一.配置式开发 在我们之前的学习中,springmvc使用配置式开发模式,需要在核心配置文件中springmvc.xml注册大量的bean来注入controller控制器,工作繁琐容易出错,下面我们学 ...

  7. ZOJ 1049 判断坐标点

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=49 水题 #include<iostream> #include&l ...

  8. 粘性页脚 Sticky Footer 最佳方式

    前段时间工作中遇到粘性页脚的需求,以前用过JS控制过,最后发现flex布局是解决这类问题的好帮手. 粘性页脚:即使没有足够的内容填充页面,也要将页脚固定到窗口的底部. <!DOCTYPE htm ...

  9. [POI2005]AUT-The Bus

    树状数组维护前缀最大值+扫描线DP #include"cstdio" #include"cstring" #include"iostream" ...

  10. BZOJ1031 [JSOI2007]字符加密

    Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作 ...