区间动态统计的好题。

 /*  */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct inter_t {
int l, r, id, ans;
inter_t() {}
inter_t(int _l, int _r, int _id, int _ans=):
l(_l), r(_r), id(_id), ans(_ans) {}
friend bool operator< (const inter_t& a, const inter_t& b) {
if (a.r == b.r)
return a.l > b.l;
else
return a.r < b.r;
}
} inter_t; const int maxn = 1e5+;
vector<int> vc[maxn];
int sz[maxn];
vector<inter_t> Q;
map<int,int> tb;
int d[maxn];
int a[maxn], l = ;
int v[maxn]; bool comp(const inter_t& a, const inter_t& b) {
return a.id < b.id;
} int main() {
int i, j, k;
int n, m; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d %d", &n, &m);
for (i=; i<=n; ++i)
scanf("%d", &a[i]); int li, ri;
for (i=; i<m; ++i) {
scanf("%d %d", &li, &ri);
Q.push_back(inter_t(li, ri, i));
}
sort(Q.begin(), Q.end());
Q.push_back(inter_t(,n+,m));
memset(d, , sizeof(d));
memset(sz, , sizeof(sz)); int r = , mr, x, id, p, cnt;
i = ;
while (i < m) {
mr = Q[i].r;
while (r <= mr) {
x = a[r];
id = tb[x];
if (id == ) {
tb[x] = id = l;
l++;
v[l] = x;
}
vc[id].push_back(r);
++sz[id];
if (sz[id] >= x) {
p = vc[id][sz[id]-x];
++d[p];
}
if (sz[id] >= x+) {
p = vc[id][sz[id]-x-];
d[p] -= ;
}
if (sz[id] > x+) {
p = vc[id][sz[id]-x-];
++d[p];
}
++r;
}
cnt = ;
k = mr+;
while (Q[i].r == mr) {
for (j=Q[i].l; j<k; ++j)
cnt += d[j];
Q[i].ans = cnt;
k = Q[i].l;
++i;
}
} sort(Q.begin(), Q.end(), comp);
for (i=; i<m; ++i)
printf("%d\n", Q[i].ans); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】220B Little Elephant and Array的更多相关文章

  1. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  2. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  3. 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

    [题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  4. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  5. php基础知识【函数】(1)数组array

    一.排序 1.sort -- 从最低到最高排序,删除原有的键名,赋予新的键名[字母比数字高] 2.rsort -- 逆向排序(最高到最低),删除原有的键名,赋予新的键名[字母比数字高] 3.asort ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  8. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. 【leetcode】Search in Rotated Sorted Array (hard)

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. 【原】window上安装elasticserach

    [window上安装elasticserach] 系统环境:2008R2 x64测试安装用的服务器IP:192.168.12.52elasticsearch版本:2.3.4JDK版本:jdk 1.8. ...

  2. Installing the .NET Framework 4.5, 4.5.1

    This article provides links for installing the .NET Framework 4.5 and 4.5.1 on your computer.  If yo ...

  3. MYSQL批处理

    待更新 版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. memcached 安装

    安装 memcached 需要 三部1,下载 memcached 放到php目录将php_memcached.dll 放到php的ext 目录 2,打开管理员命令,将memcached 拖拉到命令中, ...

  5. 自定义组合控件,适配器原理-Day31

    自定义组合控件,适配器原理-Day31 mobile2.1 主页定义 手机上锁功能 1.弹出设置密码框. 手机下载进度 自定定义控件 控件的属性其实就是控件类一个属性设置属性调用类的set方法方法, ...

  6. http和HTTPS的区别及SSL介绍

    简单来说: 在URL前加https://前缀表明是用SSL加密的. 你的电脑与服务器之间收发的信息传输将更加安全. Web服务器启用SSL需要获得一个服务器证书并将该证书与要使用SSL的服务器绑定.  ...

  7. Linux 信号量互斥编程

    所谓信号量,其实就是一个数字.内核给这个数字赋予一定的含义,让它等于不同的值时所表示的意义不同.这样就可以用它来标示某种资源是否正被使用.信号的分类其实挺多的,主要还是二值和计数器.这里讨论二值 现在 ...

  8. C#中Socket用法,多个聊天和单一聊天。

    自己琢磨Socket刚刚几天,所以整理出来和大家共享一下.废话少说直接进入正题. 在C#中提供了两种网络服务,一种是Socket类,另一种是TcpListener(服务器),TcpClient(客户端 ...

  9. details标签和summary标签

    代码: <details open="open"> <summary>幸福度调查结果</summary> <p>对人们幸福度的调查& ...

  10. MOOC即Massive Open Online Course的缩写

    A man can succeed at almost anything for which he was unlimited enthusiasm. 只要有无限的热情,一个人几乎可以在任何事情上取得 ...