Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 55154 | Accepted: 15999 |
Description
Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
题目大意:
贴n张图片,后一张会把前面一张给覆盖中,问,最后能看到的能有几张
思路:
由于坐标过大,那么如果开数组肯定胡超过这个区间。那么我们就要思考着离散化。离散化的步骤是首先用一个数组保存这些数据,然后去重,然后用lowerbound来寻找他的坐标并保存就可以了。
然后对于题目的这种情况,我们考虑到的是先update,然后用tree数组来保存这个坐标值,但是注意,每次如果找到了这个tree[o]要记得往下传值。
然后还有一个注意的一点就是为了防止一个点重复的ans++,所以就要用一个atlas数组来保存已经访问过的点的信息就可以了。
然后query的任务就是访问所有的段。
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; const int maxn = 10000 + 5; int n; int pos[maxn][2]; int tree[maxn << 2]; int a[maxn * 4]; int atlas[maxn * 4]; ll ans; int al; void init(){ memset(pos, 0, sizeof(pos)); memset(a, 0, sizeof(a)); memset(tree, 0, sizeof(tree)); memset(atlas, 0, sizeof(atlas)); scanf("%d", &n); al = 0; for (int i = 1; i <= n; i++){ scanf("%d%d", &pos[i][0], &pos[i][1]); a[++al] = pos[i][0], a[++al] = pos[i][1]; } /* for (int i = 1; i <= al; i++){ printf("%d%c", a[i], i == al ? '\n' : ' '); } */ } void pushdown(int o){ if (tree[o] == 0) return ; int l = o << 1; int r = o << 1 | 1; tree[l] = tree[r] = tree[o]; tree[o] = 0; } void update(int o, int ul, int ur, int l, int r, int pos){ if (ul <= l && ur >= r){ tree[o] = pos; return ; } pushdown(o); int mid = l + (r - l) / 2; if (ul <= mid){ update(o << 1, ul, ur, l, mid, pos); } if (ur > mid){ update(o << 1 | 1, ul, ur, mid + 1, r, pos); } } void query(int o, int l, int r){ if (tree[o] != 0){ if (atlas[tree[o]] == 0) { ans++; atlas[tree[o]] = 1; } tree[o] = 0; return ; } int mid = l + (r - l) / 2; query(o << 1, l, mid); query(o << 1 | 1, mid + 1, r); } void solve(){ sort(a + 1, a + 1 + al); int cnt = 1; for (int i = 2; i <= al; i++){ if (a[i] == a[i - 1]) continue; a[++cnt] = a[i]; } /* for (int i = 1; i <= cnt; i++){ printf("%d%c", a[i], i == cnt ? '\n' : ' ' ); } */ ans = 0; for (int i = 1; i <= n; i++){ int ul = lower_bound(a + 1, a + 1 + cnt, pos[i][0]) - a; int ur = lower_bound(a + 1, a + 1 + cnt, pos[i][1]) - a; pos[i][0] = ul, pos[i][1] = ur; update(1, ul, ur, 1, cnt, i); } query(1, 1, cnt); printf("%I64d\n", ans); } int main(){ int t; scanf("%d", &t); while (t--){ init(); solve(); } return 0; } </cstring></algorithm></cstdio>