区间动态统计的好题。

 /*  */
#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. 多维数组遍历PHP

    原文出处 <?php /* * ------------------------------------------------- * Author : nowamagic * Url : ww ...

  2. 研究WCF并发及处理能力的控制

    WCF 跟并发 性能相关的几个配置:1.系统控制的客户端网络连接并发(如果服务端也需要并发请求的话这个参数也是需要的):          <configuration>          ...

  3. while loading persisted sessions 异常解决方法

    一直用tomcat一段时间都正常无事,最近一次启动tomcat就发生以下异常: 严重: IOException while loading persisted sessions: java.io.EO ...

  4. [转]jQuery源码分析系列

    文章转自:jQuery源码分析系列-Aaron 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://github.com/JsAaro ...

  5. 数据库(学习整理)----7--Oracle多表查询,三种join连接

    聚合函数:(都会忽略null数据) 常用的有5种:将字段中所有的数据聚合在一条中 .sum(字段名) :求总和 .avg(字段名) :求平均值 .max(字段名) :求最大值 .min(字段名) :求 ...

  6. Binary Tree Inorder Traversal 解题思路 ×

    问题: 非递归中序遍历二叉树 思路: 1.大循环,判断节点是否为空,栈是否为空 2.不为空:点进栈,向左走 3.为空:为空,出栈,读取值,向右走

  7. mysql备份和恢复

    摘自:http://safe.it168.com/a2009/1108/805/000000805490.shtml 要备份数据库" phpbb_db_backup " #mysq ...

  8. FreeRTOS随记

    任务函数原型: void ATaskFunction(void * pvParameters); 任务不允许从实现函数中返回.如果一个任务不再需要,可以用vTaskDelete()删除; 一个任务函数 ...

  9. JS中的call、apply、bind方法

    JS中的call.apply.bind方法 一.call()和apply()方法 1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]] ...

  10. python判断一个数字是整数还是浮点数

    在网上闲逛,发现了一个python的用法