Looksery Cup 2015 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 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, k, tot, a[N], stk[N], L[N], R[N], sum[N];
vector<int> num[]; 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[].push_back();
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
sum[i] = (sum[i-]+a[i]%k)%k;
num[sum[i]].push_back(i);
}
a[] = a[n+] = inf;
stk[++tot] = ;
for(int i = ; i <= n; i++) {
while(tot && a[stk[tot]] < a[i]) tot--;
L[i] = stk[tot];
stk[++tot] = i;
}
tot = ;
stk[++tot] = n+;
for(int i = n; i >= ; i--) {
while(tot && a[stk[tot]] <= a[i]) tot--;
R[i] = stk[tot];
stk[++tot] = i;
}
LL ans = ;
for(int i = ; 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]-);
} else {
for(int j = i; j < R[i]; j++) {
ans += cal((sum[j]-(a[i]%k)+k)%k, L[i], i-);
}
}
}
printf("%lld\n", ans - n);
return ;
} /*
*/
Looksery Cup 2015 F - Yura and Developers 单调栈+启发式合并的更多相关文章
- 【Codeforces549F】Yura and Developers [单调栈][二分]
Yura and Developers Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 4 ...
- Looksery Cup 2015 Editorial
下面是题解,做的不好.下一步的目标是rating涨到 1800,没打过几次cf A. Face Detection Author: Monyura One should iterate through ...
- 2019牛客暑假多校赛(第二场) F和H(单调栈)
F-Partition problem https://ac.nowcoder.com/acm/contest/882/F 题意:输入一个数n,代表总共有2n个人,然后每个人对所有人有个贡献值,然后问 ...
- Looksery Cup 2015 A. Face Detection 水题
A. Face Detection Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/pro ...
- Looksery Cup 2015 B. Looksery Party 暴力
B. Looksery Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/pro ...
- Looksery Cup 2015 D. Haar Features 暴力
D. Haar Features Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/prob ...
- Looksery Cup 2015 H. Degenerate Matrix 数学
H. Degenerate Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/ ...
- 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 ...
- Looksery Cup 2015 C. The Game Of Parity —— 博弈
题目链接:http://codeforces.com/problemset/problem/549/C C. The Game Of Parity time limit per test 1 seco ...
随机推荐
- python---基础知识回顾(七)迭代器和生成器
前戏:迭代器和生成器 迭代: 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). Python的for循环不仅可以用在 ...
- memcmp 和 memcpy使用
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...
- lvm--pv丢失后恢复
[root@db-backup ~]# vgcfgrestore vg_backup Couldn't find device with uuid JgYDQu-R1AG-wrD2-AHpX-A14 ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(五)
使用application.ym进行多环境配置 1.配置激活选项 spring: profiles: active: dev 2.在配置文件添加若干个英文状态下的短横线即可区分 spring: pro ...
- 解析html和采集网页的神兵利器
HtmlAgilityPack是一个基于.Net的.第三方免费开源的微型类库,主要用于在服务器端解析html文档(在B/S结构的程序中客户端可以用Javascript解析html).截止到本文发表时, ...
- Eltwise层解析
Concat层虽然利用到了上下文的语义信息,但仅仅是将其拼接起来,之所以能起到效果,在于它在不增加算法复杂度的情形下增加了channel数目.那有没有直接关联上下文的语义信息呢?答案是Eltwise层 ...
- 20、List集合中特有的方法
List里面的特有方法简介 List中除了Collection里面的方法以外,内部还有一些方法,通过这些方法,开发者可以更方便的操作List接口的实现类. package com.monkey1024 ...
- datatable表格框架服务器端分页查询设置
更多内容推荐微信公众号,欢迎关注: js代码如下: $('#mytable').dataTable( { "bServerSide": true, //开启服务器模式,使用服务器端 ...
- 使用TS+Sequelize实现更简洁的CRUD
如果是经常使用Node来做服务端开发的童鞋,肯定不可避免的会操作数据库,做一些增删改查(CRUD,Create Read Update Delete)的操作,如果是一些简单的操作,类似定时脚本什么的, ...
- bootstrap_bootstrap中日历范围选择插件daterangepicker的使用
1.引入脚本 <link rel="stylesheet" type="text/css" href="assets/css/bootstrap ...