统计满足某些性质的区间个数。

我们考虑移动 \(r\) 指针。

然后考虑把不能选的区间 \(ban\)掉。

具体看下细节吧。

#include<iostream>
#include<cstdio>
#define ll long long
#define N 500005 int n,c[N],u[N],pre[N]; struct P{
int tag,sum;
}T[N << 2]; #define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define t(x) T[x].tag
#define s(x) T[x].sum
#define mid ((l + r) >> 1) inline void build(int u,int l,int r){
t(u) = 0;
s(u) = r - l + 1;
if(l == r)
return ;
build(ls(u),l,mid);
build(rs(u),mid + 1,r);
} ll ans; #define root 1,1,n ll L; inline void up(int x,int l,int r){if(t(u))s(u) = 0;else if(l == r) s(u) = 1;else s(u) = s(ls(u)) + s(rs(u));} inline void change(int u,int l,int r,int tl,int tr,int p){
if(L >= l && R <= r)t(u) += p,up(x,l,r);
else{
if(mid >= tl)
change(ls(u),l,mid,tl,tr,p);
if(mid < tr)
change(rs(u),mid + 1,r,tl,tr,p);
}
up(x,l,r);
} inline ll find(int u,int l,int r,int tl,int tr){
if(t(u))return 0;
if(tl <= l && r <= tr)
return s(u);
ll ans = 0;
if(tl <= mid)
ans = ans + find(ls(u),l,mid,tl,tr);
if(tr > mid)
ans = ans + find(rs(u),mid + 1,r,tl,tr);
return ans;
} int main(){
scanf("%d",&n);
for(int i = 1;i <= n;++i)
scanf("%d",&u[i]),pre[i] = c[u[i]],c[u[i]] = i;
for(int i = 1;i <= n;++i){
if(!pre[i])change(root,1,i,1);
if(!pre[pre[i]])change(root,1,pre[i],-1),change(root,1,i,1);
if(!pre[pre[pre[i]]])change(root,1,pre[i],-1),change(root,pre[pre[i]] + 1,i,1);
else{
change(root,pre[pre[pre[i]]] + 1,pre[i],-1);
change(1,pre[pre[i]] + 1,i,1);
}
L = std::max(L,pre[pre[pre[i]]] + 1);
ans += find(root,L,i);
}
std::cout<<ans<<std::endl;
}

CF1418G Three Occurrences的更多相关文章

  1. 题解-CF1418G Three Occurrences

    题面 CF1418G Three Occurrences 给一个 \(n\) 个数的序列 \(a_i\),求每个出现过的数出现次数为 \(3\) 的子序列个数. 数据范围:\(1\le n\le 5\ ...

  2. [Java Web]Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors

    手机客户端向服务器提交Http请求时,Tomcat抛出错误: 十二月 31, 2014 2:32:45 下午 org.apache.coyote.http11.AbstractHttp11Proces ...

  3. eclipse使用tips-Toggle Mark Occurrences 颜色更改

    Toggle Mark Occurrences这个功能非常好用,能把选中的方法/变量在本类中全部出现的地方高亮显示,是一个非常实用的功能.但是默认颜色是灰色,非常毁眼.可以通过下面的设置更改为自己喜欢 ...

  4. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...

  5. WordLight: highlights all occurrences of a selected text for VS2008

    https://visualstudiogallery.msdn.microsoft.com/ad686131-47d4-4c13-ada2-5b1a9019fb6f About This is a ...

  6. org.apache.jasper.JasperException: - Page directive must not have multiple occurrences of pageencoding

    最近写jsp遇到一系列的低级错误,记录下来权当前车之鉴吧. 错误提示: SEVERE: Servlet.service() for servlet jsp threw exceptionorg.apa ...

  7. further occurrences of HTTP header parsing errors will be logged at DEBUG level.错误

    今天进行项目测试的时候出现了further occurrences of HTTP header parsing errors will be logged at DEBUG level.错误,查了半 ...

  8. jsp include 报错:illegal to have multiple occurrences of contentType with different values (old: text/html; charset=UTF-8, new: text/html; carset=UTF-8)

    严重: Servlet.service() for servlet jsp threw exception org.apache.jasper.JasperException: /jsp.jsp(1, ...

  9. Segment Occurrences(string find函数)

    Description You are given two strings s and t, both consisting only of lowercase Latin letters.The s ...

随机推荐

  1. 微信小程序的登录流程

    一.背景 传统的web开发实现登陆功能,一般的做法是输入账号密码.或者输入手机号及短信验证码进行登录 服务端校验用户信息通过之后,下发一个代表登录态的 token 给客户端,以便进行后续的交互,每当t ...

  2. ffmpeg剪视频

    ffmpeg裁剪合并视频   ffmpeg提供简单的命令参数: ffmpeg -ss START -t DURATION -i INPUT -vcodec copy -acodec copy OUTP ...

  3. 第二次Scrum Metting

    日期:2021年4月25日会议主要内容概述:前后端针对WebAPI进行协调与统一工作,商量接下来两日计划:敲定部分设计细节. 一.进度情况 组员 负责 两日内已完成的工作 后两日计划完成的工作 工作中 ...

  4. OO第四单元

    OO第四单元总结 第四单元架构设计 第一次作业 uml类图 这次作业我采取的基本思路就是根据指令来建造一个简易的类图,用于查询,其中umlclass中包含了umlAttraibute,umlOpera ...

  5. Qt字符编码小知识

    1.VS2010默认编码是GBK,Qt5的内置编码是utf-8,想要在VS2010及其以上版本,优雅的使用utf-8的字符编码需要 // Coding: UTF-8(BOM) #if defined( ...

  6. (转)linux下错误的捕获:errno和strerror的使用,以及perror和strerror的区别

    经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...

  7. hdu 1502 Regular Words(DP)

    题意: 一个单词X由{A,B,C}三种字母构成. A(X):单词X中A的个数.B(X),C(X)同理. 一个单词X如果是regular word必须满足A(X)=B(X)=C(X)且对于X的任意前缀有 ...

  8. hdu 2571 命运(水DP)

    题意: M*N的grid,每个格上有一个整数. 小明从左上角(1,1)打算走到右下角(M,N). 每次可以向下走一格,或向右走一格,或向右走到当前所在列的倍数的列的位置上.即:若当前位置是(i,j), ...

  9. hdu 2147 kiki's game(DP(SG)打表找规律)

    题意: n*m的棋盘,一枚硬币右上角,每人每次可将硬币移向三个方向之一(一格单位):左边,下边,左下边. 无法移动硬币的人负. 给出n和m,问,先手胜还是后手胜. 数据范围: n, m (0<n ...

  10. linux 安装docker(一)

    1.安装环境 此处在Centos7进行安装,可以使用以下命令查看CentOS版本 lsb_release -a 在 CentOS 7安装docker要求系统为64位.系统内核版本为 3.10 以上,可 ...