标签:des style http io ar color os sp java
2 5 1 2 2 2 2 4 3 4 5 1000 5 1 1 2 2 3 3 4 4 5 5
3 1
Statistic | Submit | Discuss | Note
比赛时一开始用线段树WA了,然后树状数组过的,赛后听某牛说可以O(n),然后仔细想了下,比如现在要在[l,r]上覆盖一次,那么我们可以在s[l]上+1,s[r + 1] 上 - 1,记录覆盖次数,然后for一遍,当前的s[i] 加上 s[i - 1]就是i这个点被覆盖的次数,因为有s[r + 1] --,所以此方案是合理的,数据太大可以离散化一下
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 100010; int xis[N << 1]; int po[N << 1]; struct node { int l, r; }lines[N]; int main() { int t, n, cnt, res, ans; scanf("%d", &t); while (t--) { scanf("%d", &n); map <int, int> newx; memset (xis, 0, sizeof(xis)); newx.clear(); cnt = 0; res = 0; for (int i = 0; i < n; ++i) { scanf("%d%d", &lines[i].l, &lines[i].r); po[cnt++] = lines[i].l; po[cnt++] = lines[i].r; } sort (po, po + cnt); for (int i = 0; i < cnt; ++i) { if (i == 0) { newx[po[i]] = ++res; } else if(po[i] == po[i - 1]) { newx[po[i]] = newx[po[i - 1]]; } else { newx[po[i]] = ++res; } } for (int i = 0; i < n; ++i) { int l = newx[lines[i].l]; int r = newx[lines[i].r]; xis[l]++; xis[r + 1]--; } ans = 0; for (int i = 1; i <= res; ++i) { xis[i] += xis[i - 1]; if (ans < xis[i]) { ans = xis[i]; } } printf("%d\n", ans); } return 0; }
标签:des style http io ar color os sp java
原文地址:http://blog.csdn.net/guard_mine/article/details/41620575