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 单调栈+启发式合并的更多相关文章

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

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

  2. Looksery Cup 2015 Editorial

    下面是题解,做的不好.下一步的目标是rating涨到 1800,没打过几次cf A. Face Detection Author: Monyura One should iterate through ...

  3. 2019牛客暑假多校赛(第二场) F和H(单调栈)

    F-Partition problem https://ac.nowcoder.com/acm/contest/882/F 题意:输入一个数n,代表总共有2n个人,然后每个人对所有人有个贡献值,然后问 ...

  4. Looksery Cup 2015 A. Face Detection 水题

    A. Face Detection Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/pro ...

  5. Looksery Cup 2015 B. Looksery Party 暴力

    B. Looksery Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/pro ...

  6. Looksery Cup 2015 D. Haar Features 暴力

    D. Haar Features Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/prob ...

  7. Looksery Cup 2015 H. Degenerate Matrix 数学

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. np.diff函数

    np.diff函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 数组中a[n]-a[n-1] import numpy as np a=np.array([1, 6, 7, 8, 12]) ...

  2. JAVA核心技术I---JAVA开发环境配置

    一:常常有看到Java SE,Java EE,Java ME,那么他们的区别呢? 1. Java SE(Java Platform,Standard Edition  java平台标准版). Java ...

  3. 关于720p和1080p观看距离和效果

    分类: 不同分辩率的信号源,对观看距离的要求是不同的.在标清时代,看电视台的画面,要离得好鬼远,否则就会觉得画面粗糙闪炼唔舒服.720P及1080P的观看距离又应该点样呢?系唔系买个50寸的平板电视要 ...

  4. select()函数

    select(),确定一个或多个套接口的状态,本函数用于确定一个或多个套接口的状态,对每一个套接口,调用者可查询它的可读性.可写性及错误状态信息,用fd_set结构来表示一组等待检查的套接口,在调用返 ...

  5. [USACO07FEB]牛的词汇The Cow Lexicon

    https://daniu.luogu.org/problemnew/show/P2875 dp[i]表示前i-1个字符,最少删除多少个 枚举位置i, 如果打算从i开始匹配, 枚举单词j,计算从i开始 ...

  6. [ONTAK2015]Bajtman i Okrągły Robin

    bzoj 4276: [ONTAK2015]Bajtman i Okrągły Robin Time Limit: 40 Sec  Memory Limit: 256 MB Description 有 ...

  7. (转)Oracle 字符集的查看和修改

    一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库 ...

  8. mysql zip 解压安装

    系统:win10 专业版 mysql 5.7.21 解压安装. 对于Windows,mysql官网推荐使用可执行文件进行安装,这里我还是暂时用noinstall 解压zip文件来安装 zip 文件解压 ...

  9. Spring4笔记4--基于XML的DI(依赖注入)

    基于XML的DI(依赖注入): Bean 实例在调用无参构造器创建了空值对象后,就要对 Bean 对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类:设值注入. ...

  10. pom可以过滤resource 下的文件