设为首页 加入收藏

TOP

Codeforces 101B Buses 排序+树状数组
2015-07-20 17:46:52 来源: 作者: 【 】 浏览:18
Tags:Codeforces 101B Buses 排序

题目链接:点击打开链接

当转移[l,r] 区间时, 若[0, r-1] 这里的区间都已经转移完毕时是最优的,所以按右端点升序,同理右端点相同时左端点升序,然后树状数组维护一下前缀和。

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        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[N]; bool cmp1(node x, node y){if(x.r==y.r)return x.l
       
        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<
        
         

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇NYOJ-最高位数字 下一篇Leetcode 细节实现题 Spiral Matr..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)