CodeforcesRound #136 (Div. 2) D. Little Elephant andArray
2013-09-22 01:53
639 查看
点击打开链接
题意我就直接ctrl+v过来了——给你N个数M个查询,问在每一个查询区间[l,r]范围内有多少个值为valu的数出现次数也为valu。
感觉上这道题好难的样子……
首先,我们可以遍历这些数,如果有一个数的出现次数和它本身相等了,那么就说明当前我们查询的这个区间里面是有一个符合条件的数的,此时当前区间答案加1,然后我们需要处理的是当某一个数出现的次数大于它本身的时候,这个时候就说明当前我们查询的区间不符合了,那当前区间答案应该减1。我们记录的是可能的起点区间,终点是我们当前扫描到的位置,同时记录一下每一个数出现的位置,这样以来,我们就可以得到起点区间了(所谓起点区间,就是以我们枚举到的当前位置为终点,起点在那个区间里面任意选择均可满足条件),当某一个数出现的次数大于它本身的时候,刚刚更新到的起点区间应该减1,然后我们的新区间应该+1,同时我们要照顾一下询问,如果询问的终点刚好是我们枚举到的位置的话,我们就应该询问一下询问的起点到此有多少个答案,实际上由于我们只考虑了起点区间,终点自动算做了我们当前枚举到的位置,所以我们只需要询问一下起点里面保存了多少个答案即可。
讲的不太明白,代码奉上~~
神思想啊……反正我不看题解是想不到的了,现在理解了。。
题意我就直接ctrl+v过来了——给你N个数M个查询,问在每一个查询区间[l,r]范围内有多少个值为valu的数出现次数也为valu。
感觉上这道题好难的样子……
首先,我们可以遍历这些数,如果有一个数的出现次数和它本身相等了,那么就说明当前我们查询的这个区间里面是有一个符合条件的数的,此时当前区间答案加1,然后我们需要处理的是当某一个数出现的次数大于它本身的时候,这个时候就说明当前我们查询的区间不符合了,那当前区间答案应该减1。我们记录的是可能的起点区间,终点是我们当前扫描到的位置,同时记录一下每一个数出现的位置,这样以来,我们就可以得到起点区间了(所谓起点区间,就是以我们枚举到的当前位置为终点,起点在那个区间里面任意选择均可满足条件),当某一个数出现的次数大于它本身的时候,刚刚更新到的起点区间应该减1,然后我们的新区间应该+1,同时我们要照顾一下询问,如果询问的终点刚好是我们枚举到的位置的话,我们就应该询问一下询问的起点到此有多少个答案,实际上由于我们只考虑了起点区间,终点自动算做了我们当前枚举到的位置,所以我们只需要询问一下起点里面保存了多少个答案即可。
讲的不太明白,代码奉上~~
神思想啊……反正我不看题解是想不到的了,现在理解了。。
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 const int maxn = (int)1e5 + 10; struct segtree{ int sum[maxn << 2]; void PushDown(int rt){ if (sum[rt]){ sum[rt << 1] += sum[rt]; sum[rt << 1 | 1] += sum[rt]; sum[rt] = 0; } } void build(int l, int r, int rt){ sum[rt] = 0; if (l == r) return; int m = (l + r) >> 1; build(lson); build(rson); } void update(int ll, int rr, int v, int l, int r, int rt){ if (ll <= l && rr >= r){ sum[rt] += v; return; } PushDown(rt); int m = (l + r) >> 1; if (ll <= m) update(ll, rr, v, lson); if (rr > m) update(ll, rr, v, rson); } int query(int pos, int l, int r, int rt){ if (l == r) return sum[rt]; PushDown(rt); int m = (l + r) >> 1; if (pos <= m) return query(pos, lson); else return query(pos, rson); } }; segtree solver; struct Query{ int l, r, id; Query(){} Query(int a, int b, int c) : l(a), r(b), id(c){} friend bool operator < (const Query &a, const Query &b){ return a.r < b.r; } }; vector<Query> que; vector<int> pos[maxn]; int a[maxn], cnt[maxn], ans[maxn]; pair<int, int> pre[maxn]; int main(){ #ifndef ONLINE_JUDGE freopen("in.txt", "rt", stdin); #endif int n, m; while(scanf("%d%d", &n, &m) == 2){ memset(cnt, 0, sizeof(cnt)); for (int i = 0; i <= n; i++) pos[i].clear(); que.clear(); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 0; i < m; i++){ int a, b; scanf("%d%d", &a, &b); que.push_back(Query(a, b, i)); } sort(que.begin(), que.end()); int ind = 0; solver.build(1, n, 1); for (int i = 1; i <= n; i++){ int val = a[i]; if (val <= n){ cnt[val] += 1; pos[val].push_back(i); if (cnt[val] == val){ pre[val] = make_pair(1, pos[val][0]); solver.update(pre[val].first, pre[val].second, 1, 1, n, 1); }else if (cnt[val] > val){ solver.update(pre[val].first, pre[val].second, -1, 1, n, 1); pre[val] = make_pair(pre[val].second + 1, pos[val][cnt[val] - val]); solver.update(pre[val].first, pre[val].second, 1, 1, n, 1); } } while (que[ind].r == i && ind < m){ ans[que[ind].id] = solver.query(que[ind].l, 1, n, 1); ind += 1; } } for (int i = 0; i < m; i++) printf("%d\n", ans[i]); } return 0; }
相关文章推荐
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #136 (Div. 2) A. Little Elephant and Function
- Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #129 (Div. 2) D. Little Elephant and Cards
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #129 (Div. 2)A. Little Elephant and Rozdil
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #157 (Div. 2) B. Little Elephant and Magic Square
- Codeforces Round #129 (Div. 1) A Little Elephant and Interval
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
- 【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest
- Codeforces Round #259 (Div. 2) C. Little Pony and Expected Maximum
- Codeforces Round #259 (Div. 2) A.Little Pony and Crystal Mine