【CF】220B Little Elephant and Array

区间动态统计的好题。

  1 /*  */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 using namespace std;
 20 //#pragma comment(linker,"/STACK:102400000,1024000")
 21
 22 #define rep(i, a, n)     for (int i=a;i<n;++i)
 23 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 24 #define pb                 push_back
 25 #define mp                 make_pair
 26 #define all(x)             (x).begin(),(x).end()
 27 #define SZ(x)             ((int)(x).size())
 28 #define lson            l, mid, rt<<1
 29 #define rson            mid+1, r, rt<<1|1
 30
 31 typedef struct inter_t {
 32     int l, r, id, ans;
 33     inter_t() {}
 34     inter_t(int _l, int _r, int _id, int _ans=0):
 35         l(_l), r(_r), id(_id), ans(_ans) {}
 36     friend bool operator< (const inter_t& a, const inter_t& b) {
 37         if (a.r == b.r)
 38             return a.l > b.l;
 39         else
 40             return a.r < b.r;
 41     }
 42 } inter_t;
 43
 44 const int maxn = 1e5+5;
 45 vector<int> vc[maxn];
 46 int sz[maxn];
 47 vector<inter_t> Q;
 48 map<int,int> tb;
 49 int d[maxn];
 50 int a[maxn], l = 1;
 51 int v[maxn];
 52
 53 bool comp(const inter_t& a, const inter_t& b) {
 54     return a.id < b.id;
 55 }
 56
 57 int main() {
 58     int i, j, k;
 59     int n, m;
 60
 61     #ifndef ONLINE_JUDGE
 62         freopen("data.in", "r", stdin);
 63         freopen("data.out", "w", stdout);
 64     #endif
 65
 66     scanf("%d %d", &n, &m);
 67     for (i=1; i<=n; ++i)
 68         scanf("%d", &a[i]);
 69
 70     int li, ri;
 71     for (i=0; i<m; ++i) {
 72         scanf("%d %d", &li, &ri);
 73         Q.push_back(inter_t(li, ri, i));
 74     }
 75     sort(Q.begin(), Q.end());
 76     Q.push_back(inter_t(0,n+1,m));
 77     memset(d, 0, sizeof(d));
 78     memset(sz, 0, sizeof(sz));
 79
 80     int r = 1, mr, x, id, p, cnt;
 81     i = 0;
 82     while (i < m) {
 83         mr = Q[i].r;
 84         while (r <= mr) {
 85             x = a[r];
 86             id = tb[x];
 87             if (id == 0) {
 88                 tb[x] = id = l;
 89                 l++;
 90                 v[l] = x;
 91             }
 92             vc[id].push_back(r);
 93             ++sz[id];
 94             if (sz[id] >= x) {
 95                 p = vc[id][sz[id]-x];
 96                 ++d[p];
 97             }
 98             if (sz[id] >= x+1) {
 99                 p = vc[id][sz[id]-x-1];
100                 d[p] -= 2;
101             }
102             if (sz[id] > x+1) {
103                 p = vc[id][sz[id]-x-2];
104                 ++d[p];
105             }
106             ++r;
107         }
108         cnt = 0;
109         k = mr+1;
110         while (Q[i].r == mr) {
111             for (j=Q[i].l; j<k; ++j)
112                 cnt += d[j];
113             Q[i].ans = cnt;
114             k = Q[i].l;
115             ++i;
116         }
117     }
118
119     sort(Q.begin(), Q.end(), comp);
120     for (i=0; i<m; ++i)
121         printf("%d\n", Q[i].ans);
122
123     #ifndef ONLINE_JUDGE
124         printf("time = %d.\n", (int)clock());
125     #endif
126
127     return 0;
128 }
时间: 2024-10-14 11:19:19

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

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee

【26】Remove Duplicates from Sorted Array

[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【Leetcode】Median of Two Sorted Array II

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 非常经典的一个问题,如果直接查找中位数,即偶数约定向下取整,奇数就是严格的中位数,这个题目直接可以比较每个数组的mid来计算得到. 但是这个问题的需求更改为如果为偶数,则中

【CF】38E Let&#39;s Go Rolling! (dp)

前言 这题还是有点意思的. 题意: 给你 \(n\) (\(n<=3000\)) 个弹珠,它们位于数轴上.给你弹珠的坐标 \(x_i\) 在弹珠 \(i\) 上面花费 \(C_i\) 的钱 可以使弹珠在原地不动 (\(-10^9<=x_i,C_i<=10^9\)),游戏开始时,所有的弹珠向左滚动,直到碰到定在原地不动的弹珠,其花费是其滚动的距离.总花费=开始前的花费+弹珠滚动的花费,问最小的花费是多少 题解 首先划分出阶段,,我们可以先将弹珠排序,前 \(i\) 个弹珠,最后一个固定的弹

【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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array II 解题报告

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [解析] 相比Search in Rotated Sorted Array,在

【CF】codeforces round 369(div2)

*明早起来再贴代码 A [题意] 给定n*5的方格 将横向的相邻两个变成+输出   [题解] ...   B [题意] 一个n*n的正整数矩阵,有且仅有一个数为0 ,在这个位置填上一个数,使得每一列的和 每一行的和 两条对角线各自的和都相等 输出这个数   [题解]sb题.暴力一下.注意细节,否则你就像这样 (不是本人   C [题意] 一排点,用1~n表示,熊孩子要给这些点上色.最初每个点的颜色为ci.一共有m种颜色,如果ci=0表示这个点最初无色. 熊孩子们需要给最初为无色的点涂上色,往第i