codeforces 549F Yura and Developers(分治、启发式合并)
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(分治、启发式合并)的更多相关文章
- ●CodeForces 549F Yura and Developers
题链: http://codeforces.com/problemset/problem/549/F题解: 分治,链表. 考虑对于一个区间[L,R],其最大值在p位置, 那么答案的贡献就可以分为3部分 ...
- Codeforces 549F Yura and Developers
probelm 题意 给定一个序列和一个mod值,定义[l,r]合法当l到r的全部元素和减去当中的最大值的结果能够整除mod.问共同拥有多少区间合法. 思路 一開始想的分治. 对于一个[l,r]我们能 ...
- 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 ...
- SPOJ:Free tour II (树分治+启发式合并)
After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...
- Codeforces 1455G - Forbidden Value(map 启发式合并+DP)
Codeforces 题面传送门 & 洛谷题面传送门 首先这个 if 与 end 配对的结构显然形成一个树形结构,考虑把这棵树建出来,于是这个程序的结构就变为,对树进行一遍 DFS,到达某个节 ...
- Codeforces 208E - Blood Cousins(树上启发式合并)
208E - Blood Cousins 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor.多次查询,给出 u k,问有多少个与 u 具有相同 k-ance ...
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- SPOJ Free TourII(点分治+启发式合并)
After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...
- 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], ...
随机推荐
- Ibatis SqlMap映射关系总结
一.一对一关系一对一关系即一对单个对象,下面举例说明:一对单个对象例如:<resultMap id="loadAResult" class="A"> ...
- 亿级别记录的mongodb分页查询java代码实现
1.准备环境 1.1 mongodb下载 1.2 mongodb启动 C:\mongodb\bin\mongod --dbpath D:\mongodb\data 1.3 可视化mongo工具Robo ...
- 说一说HTTP
什么是URI和URL URI用字符串标示某一互联网资源,而URL表示资源的地点.可见URL是URI的子集. URI要使用涵盖全部必要信息的URI.绝对URL以及相对URL.相对URL是指从浏览器中基本 ...
- Linux du查询文件大小
#查询磁盘当前容量信息 $df -h #查询当前目录下所有文件的大小 $du -m . #两种方式查询 仅当前目录下的子文件(文件夹)大小 $du -sh /cloud/* $du -h ...
- 仿58同城UITableViewCell动画
之前看58同城APP有一个页面中Cell依次从右向左移动,今天试着做了下. 在做的过程中也遇到了几个小的问题,也算是注意点吧. 1.Cell出现时每个Cell的动画时间一样,导致没有依次移动的效果. ...
- 再说优化MySQL索引
这几天开发尤其重视数据库索引的优化,是一件好事情,开发特意提过来几个要删除的索引,oh!我的佛陀呀!历史上出现过因为评估不到位,删索引引发故障的案例.那么有什么办法可以评估索引是否合理呢? perco ...
- BTREE索引和HASH索引的区别
从本质上理解,BTREE是一种有序树,而hash是无序的.所以最关键的区别在于: 1,BTREE可以用来做范围查询,比如大于,小于,而HASH索引仅在"=","IN&qu ...
- Restful的优势
1. 轻量,直接基于http,不在需要任何别的诸如消息协议.get/post/put/delete为CRUD操作2. 面向资源,一目了然,具有自解释性.3. 数据描述简单,一般以xml,json做数据 ...
- 三:SQL server基础
/一:创建数据库/ use master if exists (select * from sysdatabases where name='数据库名称') drop database 数据库名称 - ...
- 一键安装lamp环境出现的问题
前言:之前安装lamp是独立安装的,安装扩展很方便,现在用这个一键安装包,不知道怎么样,尝试一把. Part1:安装过程中出现的问题 error: utf8_mime2text() has new s ...