题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6602

题目大意为求最长的区间,满足C种数字在区间内要么不出现,要么出现的次数都不小于K。

大致的分析一下,可以知道对于以R为右端点的区间来说,每种颜色的合法区间在[1~出现k次]和(上一次出现~下一次出现)。PS:[]为闭区间,()为开区间。

所以可以线段树维护一下,枚举区间右端点,然后在线段树上将上一次更新这种颜色的操作撤销(上次是+1,则当前-1),然后再次更新(+1)。

对于每个区间右端点,最大的有效区间为线段树上种类为C的最左边的位置。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<vector>
 7 #define lson l,mid,i<<1
 8 #define rson mid+1,r,i<<1|1
 9 using namespace std;
10 typedef long long ll;
11 const int maxn = 2e5 + 10;
12 const ll mod = 1000000007;
13 int Max[maxn * 4], lazy[maxn * 4];
14 vector<int>cnt[maxn];
15 int num[maxn];
16 int n, c, k;
17 void up(int i) {
18     Max[i] = max(Max[i << 1], Max[i << 1 | 1]);
19 }
20 void down(int i) {
21     if (lazy[i]) {
22         lazy[i << 1] += lazy[i];
23         lazy[i << 1 | 1] += lazy[i];
24         Max[i << 1 | 1] += lazy[i];
25         Max[i << 1] += lazy[i];
26         lazy[i] = 0;
27     }
28 }
29 void build(int l, int r, int i) {
30     Max[i] = lazy[i] = 0;
31     if (l == r)
32         return;
33     int mid = l + r >> 1;
34     build(lson);
35     build(rson);
36     up(i);
37 }
38 void  update(int L, int R, int k, int l, int r, int i) {
39     if (L > R)return;
40     if (L <= l && r <= R) {
41         Max[i] += k;
42         lazy[i] += k;
43         return;
44     }
45     down(i);
46     int mid = l + r >> 1;
47     if (L <= mid)update(L, R, k, lson);
48     if (R > mid)update(L, R, k, rson);
49     up(i);
50 }
51 int query(int l, int r, int i) {
52     if (l == r)return l;
53     int ans = -1, mid = l + r >> 1;
54     down(i);
55     if (Max[i << 1] == c)ans = query(lson);
56     else if (Max[i << 1 | 1] == c)ans = query(rson);
57     return ans;
58 }
59 int a[maxn];
60 int main() {
61     while (scanf("%d%d%d", &n, &c, &k) != EOF) {
62         for (int i = 1; i <= c; i++)
63             cnt[i].clear(), cnt[i].push_back(0), num[i] = 0;
64         for (int i = 1; i <= n; i++) {
65             scanf("%d", &a[i]);
66             cnt[a[i]].push_back(i);
67         }
68         build(1, n, 1);
69         for (int i = 1; i <= c; i++) {
70             cnt[i].push_back(n + 1);
71             update(cnt[i][0] + 1, cnt[i][1] - 1, 1, 1, n, 1);
72         }
73         int ans = 0;
74         for (int i = 1; i <= n; i++) {
75             update(cnt[a[i]][num[a[i]]] + 1, cnt[a[i]][num[a[i]] + 1] - 1, -1, 1, n, 1);
76             if (num[a[i]] >= k)
77                 update(1, cnt[a[i]][num[a[i]] - k + 1], -1, 1, n, 1);
78             num[a[i]]++;
79             update(cnt[a[i]][num[a[i]]] + 1, cnt[a[i]][num[a[i]] + 1] - 1, 1, 1, n, 1);
80             if (num[a[i]] >= k)
81                 update(1, cnt[a[i]][num[a[i]] - k + 1], 1, 1, n, 1);
82             int q = query(1, n, 1);
83             if (q != -1)
84                 ans = max(ans, i - q + 1);
85         }
86         printf("%d\n", ans);
87     }
88 }

转载于:https://www.cnblogs.com/sainsist/p/11329245.html

[2019杭电多校第二场][hdu6602]Longest Subarray(线段树)相关推荐

  1. 2019杭电多校第二场1009 HDU6599:求本质不同的回文串长度及数量

    hdu6599:求本质不同的回文串长度及数量 hdu6599题意: manacher+后缀自动机+倍增 $O(nlog(n))$ manacher+后缀数组+二分 $O(nlog(n))$ 回文树(回 ...

  2. 2022“杭电杯”中国大学生算法设计超级联赛 (2) 杭电多校第二场

    题目 1001 Static Query on Tree AC代码 1002 C++ to Python AC代码 1003 Copy AC代码 1005 Slayers Come AC代码 1007 ...

  3. 20190724杭电多校第二场

    没有补题..倒是又想到了1002的二分做法,比原来好写了不少,也快了不少. #include<bits/stdc++.h> using namespace std; #define p_b ...

  4. 树形dp ---- 2018年杭电多校第二场 H travel

    题目大意: 就是给你一个带点权的树,找到3条独立互不相交的路径使得权值和最大 解题思路: 很经典的树形dp 我们设dp[root][j][k]dp[root][j][k]dp[root][j][k]表 ...

  5. 2018杭电多校第二场1006(容斥原理,组合数学)

    有能力时再回来解决吧= ̄ω ̄= 转载于:https://www.cnblogs.com/ldudxy/p/9521721.html

  6. 2019杭电多校第一场 Operation HDU - 6579

    题意:给出一个序列,两种操作,求区间[l,r]的区间最大异或和,和在末尾添加一个数 思路:强制在线,保存每个线性基的数值,接下去直接去搜第r个线性基,但要保持时间比l要大,新增了一个pos数组代表一个 ...

  7. 2019杭电多校第九场 Rikka with Cake (hdu6681)

    题意:给出一个n * m的蛋糕,切 k 刀,每次从一个点(x,y)向 上下左右的一个方向切,问最后蛋糕被切成了几块 题解:显然,蛋糕的块数就是那么多线段的交点数 + 1.先离散,考虑向左切和向上切的, ...

  8. 2019杭电多校第一场 HDU 6599

    题解 枚举所有的回文串 注意,本质不同的回文串最多只有∣S∣|S|∣S∣个 在这些回文串中,有一些是满足要求的,我们对这些串打上标记 首先跑一个ManacherManacherManacher,然后枚 ...

  9. 2022杭电多校第二场

    1001 Static Query on Tree 题意:树上有A,B,C三个集合,求有多少个点在A某个点到C某个点的路径与A某个点到C某个点的路径的交上 把1到A集合中所有点的路径打上标记1,把1到 ...

  10. 2020杭电多校第二场 Lead of Wisdom(爆搜)

    Problem Description In an online game, "Lead of Wisdom" is a place where the lucky player ...

最新文章

  1. Google Expeditions项目今年获得AR支持
  2. shufflenet-centernet
  3. Jupyter notebook与Spyder,以及Jupyter notebook与Spyder集成插件
  4. array DEMO
  5. LaTeX中用BibTex管理参考文献
  6. HAproxy + Keepalive实现LDAP代理服务
  7. linux pmap 内存泄露,一个驱动导致的内存泄漏问题的分析过程(meminfo-pmap-slabtop-alloc_calls)...
  8. vue踩坑-This relative module was not found
  9. Unieap3.5-Grid编辑列中数字与下拉改变
  10. MySQL的主动优化和被动优化_MySQL“被动”性能优化汇总
  11. 【编辑器】VSCode项目管理器——Project Manager
  12. 偏相关分析在matlab上的实现
  13. 侯晓迪:全身心的投入,吃住都在实验室
  14. ARM9——五级流水线结构,以及PC指针
  15. CSDN表格换行方法
  16. three.js入门到实战
  17. Charles抓包与改写
  18. 域组策略与本地组策略
  19. 2022年最赚钱地推项目-WiFi贴项目(月入10个W)
  20. Docker安装及部署

热门文章

  1. 简明Python3教程 17.更多
  2. Windows设计师:多核芯片要求全新操作系统
  3. 基于 Keras 用 LSTM 网络做时间序列预测
  4. 定量论文:探究「健康水平、婚姻状况」对幸福感的影响
  5. Django打造大型企业官网-项目实战(二)
  6. background相关属性
  7. Spark-Mllib(二)基本统计
  8. .NET中string[]数组和Liststring泛型的相互转换以及Array类的Sort()方法(转)
  9. 设计模式-----桥接模式(Bridge Pattern)(转)
  10. memcache/memcached/memcachedb 配置、安装