Codeforces 101B Buses 排序+树状数组
2014-08-31 17:40
295 查看
题目链接:点击打开链接
当转移[l,r] 区间时, 若[0, r-1] 这里的区间都已经转移完毕时是最优的,所以按右端点升序,同理右端点相同时左端点升序,然后树状数组维护一下前缀和。
当转移[l,r] 区间时, 若[0, r-1] 这里的区间都已经转移完毕时是最优的,所以按右端点升序,同理右端点相同时左端点升序,然后树状数组维护一下前缀和。
#include <vector> #include <iostream> #include <algorithm> #include <string.h> #include <stdio.h> using namespace std; #define N 100005 #define mod 1000000007 #define ll long long int n, m; ll c[N<<2]; inline int lowbit(int x){return x&(-x);} void updata(ll val, int pos){ while(pos <= n){ c[pos] = (c[pos] + val)%mod; pos += lowbit(pos); } } ll sum(int pos){ ll ans = 0; while(pos){ ans = (ans + c[pos])%mod; pos -= lowbit(pos); } return ans; } struct node{ int l, r; }a ; bool cmp1(node x, node y){if(x.r==y.r)return x.l<y.l; return x.r < y.r;} vector<int>G; void debug(){ for(int i = 0; i < m; i++)printf(" %d %d\n", a[i].l, a[i].r); printf("***"); for(int i = 0; i < G.size(); i++) printf("%d ", G[i]); puts(""); printf("** n = %d\n", n); } void input(){ n++; G.clear(); G.push_back(1); G.push_back(n); for(int i = 0; i < m; i++)scanf("%d %d", &a[i].l, &a[i].r),a[i].l++, a[i].r++, G.push_back(a[i].l), G.push_back(a[i].r); sort(G.begin(), G.end()); G.erase(unique(G.begin(), G.end()), G.end()); // debug(); for(int i = 0; i < m; i++) { a[i].l = lower_bound(G.begin(), G.end(), a[i].l) - G.begin()+1; a[i].r = lower_bound(G.begin(), G.end(), a[i].r) - G.begin()+1; } n = lower_bound(G.begin(), G.end(), n) - G.begin()+1; sort(a, a+m, cmp1); } int main(){ int i, j; while(~scanf("%d %d",&n,&m)){ // bool ok = n==42; input(); if(m==0 || G.size()==1){puts("0");continue;} // debug(); memset(c, 0, sizeof c); updata(1, 1); for(i = 0; i < m; i++){ ll ans = sum(a[i].r-1) - sum(a[i].l-1); if(ans < 0) ans = (ans%mod + mod)%mod; // printf(" %d %d %d\n", a[i].l, a[i].r, ans); updata(ans, a[i].r); // printf(" %d %d %d\n", a[i].l, a[i].r, sum(a[i].r) - sum(a[i].l-1)); } ll ans = sum(n) - sum(n-1); // printf("%d\n", ans); if(ans < 0) ans = (ans%mod + mod)%mod; cout<<ans<<endl; } return 0; } /* 4 4 0 2 1 3 1 4 2 4 4 2 0 2 1 3 ans: 4 4 0 */
相关文章推荐
- Codeforces 12D Ball 树状数组模拟3个元素的排序
- Codeforces 557E Ann and Half-Palindrome (字典树+字符串排序)
- Codeforces 387E George and Cards(二分+树状数组)
- PKU 2481 COWS 排序 + 树状数组
- 算法训练 Beaver's Calculator/codeforces 207A1 排序+贪心
- Codeforces 484B Maximum Value(排序+二分)
- CodeForces 612D The Union of k-Segments(排序+区间计数、扫描线)
- Codeforces 496E. Distributing Parts 排序+贪心
- Codeforces - 609C.Load Balancing 排序、模拟
- Codeforces 650D. Zip-line (动态LIS) (可持久化线段树 或 离线+树状数组)
- CodeForces - 668D Little Artem and Time Machine(线段树||树状数组)
- Codeforces 853A Round#433 Div2C & Div1A Planning:优先队列或堆或排序
- CodeForces 545D Queue (排序模拟)
- codeforces-230A-Dragons(贪心+排序)
- Codeforces 3 B. Lorry 暴力 排序 枚举 贪心
- Codeforces 593B Anton and Lines 【思维】【枚举】【排序】
- hdu 5032 树状数组+极角排序
- CodeForces - 668D Little Artem and Time Machine(线段树||树状数组)
- Codeforces 301D (顺序统计+树状数组)
- 【HDU5808 BestCoder Round 86E】【时间排序 树状数组计数 bitsetDP 或分治】Price List Strike Back 距离范围、区间范围商店购物 使得价值和恰为m