F - Yura and Developers

第一次知道单调栈搞出来的区间也能启发式合并。。。

你把它想想成一个树的形式, 可以发现确实可以启发式合并。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std;const int N = 3e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;int n, k, tot, a[N], stk[N], L[N], R[N], sum[N];
vector<int> num[1000007];void add(int &a, int b) {a += b; if(a >= mod) a -= mod;
}
int cal(int x, int l, int r) {auto it1 = upper_bound(num[x].begin(), num[x].end(), r);auto it2 = lower_bound(num[x].begin(), num[x].end(), l);return it1 - it2;
}
int main() {scanf("%d%d", &n, &k);num[0].push_back(0);for(int i = 1; i <= n; i++) {scanf("%d", &a[i]);sum[i] = (sum[i-1]+a[i]%k)%k;num[sum[i]].push_back(i);}a[0] = a[n+1] = inf;stk[++tot] = 0;for(int i = 1; i <= n; i++) {while(tot && a[stk[tot]] < a[i]) tot--;L[i] = stk[tot];stk[++tot] = i;}tot = 0;stk[++tot] = n+1;for(int i = n; i >= 1; i--) {while(tot && a[stk[tot]] <= a[i]) tot--;R[i] = stk[tot];stk[++tot] = i;}LL ans = 0;for(int i = 1; i <= n; i++) {if(i - L[i] < R[i] - i) {for(int j = L[i]; j < i; j++)ans += cal((sum[j]+a[i])%k, i, R[i]-1);} else {for(int j = i; j < R[i]; j++) {ans += cal((sum[j]-(a[i]%k)+k)%k, L[i], i-1);}}}printf("%lld\n", ans - n);return 0;
}/*
*/

转载于:https://www.cnblogs.com/CJLHY/p/9916153.html

Looksery Cup 2015 F - Yura and Developers 单调栈+启发式合并相关推荐

  1. Looksery Cup 2015 F. Yura and Developers(单调栈+二分+分治)(难*)

    题目链接 题意:给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数. 思路:日后补. #include<bits/stdc++.h> using namespace std; t ...

  2. 【Codeforces549F】Yura and Developers [单调栈][二分]

    Yura and Developers Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 4 ...

  3. 【Codeforces 549F】Yura and Developers | 单调栈、启发式合并、二分

    题目链接:https://codeforces.com/problemset/problem/549/F 题目大意: 给定一个序列和一个mod值,定义[l,r]合法当l到r的所有元素和减去其中的最大值 ...

  4. [CF 526 F] Pudding Monsters(单调栈 + 线段树)

    CF526F Pudding Monsters problem solution code problem luogu翻译 solution observation :每行每列恰好有一个棋子,所以如果 ...

  5. Looksery Cup 2015 H. Degenerate Matrix 数学

    H. Degenerate Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/ ...

  6. codeforces Looksery Cup 2015 H Degenerate Matrix

    The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...

  7. Codeforces - Yura and Developers

    题目链接:Codeforces - Yura and Developers 有一个显然的2个log做法. 分治最大值,然后枚举小区间启发式计算答案. 但是其实可以一个log,因为分治的时候,对于区间形 ...

  8. codeforces 549F Yura and Developers(分治、启发式合并)

    codeforces 549F Yura and Developers 题意 给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数. 题解 分治,对于一个区间,找出最大值之后,分成两个区间. ...

  9. COCI 2015/2016 PROKLETNIK(单调栈+线段树)

    COCI 2015/2016 PROKLETNIK 题目大意 给定序列A1..NA_{1..N}A1..N​,分别求出QQQ组询问区间[L,R][L,R][L,R]中最长的连续子序列Al..rA_{l ...

最新文章

  1. 同济计算机转专业吗,被不喜欢的专业录取了?大学想转专业需谨慎
  2. python中os.path.isdir()等函数的作用和用法
  3. cf1562E. Rescue Niwen!
  4. Poj 2676 Sudoku[dfs]
  5. Python正则表达式指南上半部
  6. 【0x50 动态规划】传纸条【线性DP】
  7. 3dmax无法显示缩略图 或者 缩略图显示为黑色 -解决方法
  8. ie8 升级页面html,ie7浏览器怎么升级到ie8?
  9. 用C++实现随机分组
  10. 大物 磁场对载流导线的作用 中dl转化为dx
  11. TOJ 3015.Convert Kilometers to Miles
  12. 双非二本一战上岸科软经验贴
  13. Android——Hander+Service,实现后台长期周期性定时任务
  14. f5双机配置_F5负载均衡器双机切换机制及配置
  15. 互联网数字营销广告管理平台应用
  16. python获取a股数据_python获取A股数据列表的例子
  17. Geek 强力卸载工具,注册表,缓存,垃圾数据全清,免安装
  18. 视频调度指挥系统服务器,非常实用的应急指挥中心的视频系统设计方案
  19. android应用更新完成后自动打开
  20. T1073 正常血压

热门文章

  1. QGraphicsItem设置绘图区域和鼠标响应以及碰撞检测区域,并实现碰撞检测
  2. Grain 颗粒感 后期处理系列14
  3. 清晰理解红黑树的演变-红黑的含义
  4. chrome开发者工具使用
  5. 用Look'n'Stop防火墙实现ARP终极防御
  6. 【Windows】台式机cpu骤升100%
  7. 随手记:小程序相关知识点
  8. 1024分辨率《圣徒/天神魔煞/猎魔教士》BD中字无水印
  9. 手眼标定算法---Navy算法(Robot sensor calibration: solving AX=XB on the Euclidean group)
  10. R语言统计入门第四章描述性统计和图形——4.6表格的图形显示