区间动态统计的好题。

 /*  */
#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. (转)ie浏览器判断

    常用的 JavaScript 检测浏览器为 IE 是哪个版本的代码,包括是否是最人极端厌恶的 ie6 识别与检测. var isIE=!!window.ActiveXObject; var isIE6 ...

  2. Java-hibernate的Hello World

     hibernate 是对jdbc进行轻量级封装的  orm 框架,充当项目的持久层. 要使用 hibernate首先就需要继续配置, 引包:下载hibernate然后加入jar包 同时引入mysql ...

  3. Mono For Android的项目发布

    Mono for Android的发布步骤: 1.选择项目名称鼠标右键,选择Options选项. 2.弹出工程选项框,选择Android Build,勾选 Supported ABIS里面的所有项. ...

  4. sql 删除表数据truncate delete drop的区别

    已下内容为转载内容:学习之用 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL语句(数据定义语言),执行后会自动提交. ...

  5. iOS崩溃报告获取一

    在AppDelegate.m文件中实现函数 void UncaughtExceptionHandler(NSException *exception) { /** * 获取异常崩溃信息 */ NSAr ...

  6. C++专题 - 修练8年C++面向对象程序设计之体会 林锐

    六年前,我刚热恋“面向对象”(Object-Oriented)时,一口气记住了近十个定义.六年后,我从几十万行程序中滚爬出来准备写点心得体会时, 却无法解释什么是“面向对象”,就象说不清楚什么是数学那 ...

  7. Memcached认知[分布式]

    Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. Memcached的服务器客户端通信使用简单的基于文本行的协议. Memcached基于一个存储键/值对的 ...

  8. Hadoop学习第一天

    1.hadoop量大,数目多. 存储:分布式,集群的概念,管理(主节点.从节点),HDFS. 分析:分布式.并行.离线计算框架,管理(主节点.从节点),MapReduce. 来源:GFS->HD ...

  9. GoJS研究,简单图表制作。

    话不多说,先上图 我在这个中加入了缩略图.鼠标放大缩小等功能. <!doctype html> <html> <head> <title>Flowcha ...

  10. 主流的phpcms分析

    小型网站适合wordpress,onethink,joomla(囧啦)    wordpress(免费开源) 优点:1.样式丰富,模板重多 2. 安全性 3. 对搜索引擎友好,收录快.        ...