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. Lucio: We avoided Mourinho after every loss

    Former Inter defender Lucio has revealed how players had to avoid former Nerazzurri coach Mourinho e ...

  2. PowerDesigner常用设置

    使用powerdesigner进行数据库设计确实方便,以下是一些常用的设置 附加:工具栏不见了 调色板(Palette)快捷工具栏不见了 PowerDesigner 快捷工具栏 palette 不见了 ...

  3. nodejs文件上传组件multer使用

    多图上传,发送端: var express = require('express') var rp = require('request-promise') var fs = require(&quo ...

  4. 16、IO (Properties、序列化流、打印流、CommonsIO)

    Properties集合的特点 * A: Properties集合的特点 * a: Properties类介绍 * Properties 类表示了一个持久的属性集.Properties 可保存在流中或 ...

  5. java EE 新手入门了解

    郑重申明:本文转载至https://blog.csdn.net/Neuf_Soleil/article/details/80962686,在此深表感谢! 为什么选择java? 想必有很多初学者会像我一 ...

  6. Max Sum(经典DP)

    求最长总和序列,状态转移方程:dp[i] = max(dp[i-1]+a[i].a[i]) 因为可能有负数,所以要判断dp是否大于0,如果小于0则序列中断,从中断点开始 起始点可以用数组s保存,有中断 ...

  7. vim 配置文件.vimrc,高亮+自动缩进+行号+折叠+优化

    将一下代码copy到 用户目录下 新建文件为  .vimrc保存即可生效: 如果想所有用户生效 请修改 /etc/vimrc (建议先cp一份)"====================== ...

  8. python单继沿用父类属性的两种方法

    方法一 在子类中用父类调用其init方法(不建议) 方法二 在子类中使用super获得父类的方法 class Aaimal(object): type_name = '动物类' def __init_ ...

  9. Github+hexo+next搭建教程

    今天参考的是大神的教程,学了一个新东西,但是可能由于原教程中运用的npm包与我当前使用的npm包版本不同的原因,有出过多处运行错误,但都在此教程中解决了; 总结了下命令: npm install he ...

  10. 【PyQt5 学习记录】004:简单QThread笔记

    在文本编辑框中每隔几秒添加一行文本,代码如下: #!/usr/bin/python3 # -*- coding:utf-8 -*- import sys from PyQt5.QtWidgets im ...