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. bootstrap-switch

    首先需要引入bootstrap的css和js文件,再引入bootstrap-switch.css和bootstrap-switch.js文件 <script type="text/ja ...

  2. mq使用场景、不丢不重、时序性

    mq使用场景.不丢不重.时序性.削峰 参考: http://zhuanlan.51cto.com/art/201704/536407.htm http://zhuanlan.51cto.com/art ...

  3. 使用spring boot访问mongodb数据库

    一. spring boot中传参的方法 1.自动化配置 spring Boot 对于开发人员最大的好处在于可以对 Spring 应用进行自动配置.Spring Boot 会根据应用中声明的第三方依赖 ...

  4. 20155310 2016-2017-2 《Java程序设计》第六周学习总结

    20155310 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 4.1 Y86指令集体系结构 •有8个程序寄存器:%eax.%ecx.%edx.%ebx.% ...

  5. JS设计模式——7.工厂模式(示例-RSS阅读器)

    RSS阅读器 由于我们只想跟RSS容器对象打交道,所以用一个工厂来实例化这些内部对象并把它们组装到一个RSS阅读器中. 使用工厂方法在好处在于,我们创建的RSS阅读器类不会与那些成员对象紧密耦合在一起 ...

  6. 手动刷入Android 4.4.1 KOT49E OTA更新包

    一.Android 4.4 KitKat Google前段时间发布了Android新版本Android 4.4 KitKat,由于我的Nexus 4也是托朋友从US带回来的,所以很快就收到了Googl ...

  7. 内核工具 – Sparse 简介【转】

    转自:http://www.cnblogs.com/wang_yb/p/3575039.html Sparse是内核代码静态分析工具, 能够帮助我们找出代码中的隐患. 主要内容: Sparse 介绍 ...

  8. Tslib步骤以及出现问题的解决方案【转】

    转自:http://forum.eepw.com.cn/thread/267828/1 嵌入式设备中触摸屏使用非常广泛,但触摸屏的坐标和屏的坐标是不对称的,需要校准.校准广泛使用的是开源的tslib. ...

  9. 转 Java的 BigDecimal类型比较大小

    这个类是java里精确计算的类 1.比较对象是否相等,一般的对象用equals,但是BigDecimal比较特殊,举个例子 BigDecimal a = new BigDecimal.valueOf( ...

  10. ParameterizedType获取java泛型参数类型

    ParameterizedType getClass().getGenericSuperclass() 返回表示此 Class 所表示的实体(类.接口.基本类型或 void)的直接超类的 Type,然 ...