codeforces 830B 树状数组
来源:互联网 发布:淘宝如何设置快递模板 编辑:程序博客网 时间:2024/06/07 07:33
#include <bits/stdc++.h>using namespace std;typedef long long ll;int read(){int ret=0;char ch=getchar();while(ch<'0'||ch>'9') ch=getchar();for(;ch>='0'&&ch<='9';ch=getchar()) ret=ret*10+ch-'0';return ret;}vector<int> q[100005];int sum[100005];#define lowbit(x) x&(-x)void add(int x,int p){while(x<=100000){sum[x]+=p;x+=lowbit(x);}}int get(int x){int ans=0;while(x){ans+=sum[x];x-=lowbit(x);}return ans;}int main(){ int n=read(); for(int i=1;i<=n;i++) q[read()].push_back(i),add(i,1); int cur=1; ll ans=1; for(int i=1;i<=100000;i++) { int pos=upper_bound(q[i].begin(),q[i].end(),cur-1)-q[i].begin(); for(int j=pos;j<q[i].size();j++) { ans+=get(q[i][j])-get(cur); cur=q[i][j]; add(cur,-1); } for(int j=0;j<pos;j++) { if(q[i][j]<cur)ans+=get(q[i][j])+(get(100000)-get(cur)); else ans+=get(q[i][j])-get(cur); cur=q[i][j]; add(cur,-1); } } cout<<ans<<endl;}
阅读全文
2 0
- codeforces 830B 树状数组
- Codeforces-830B:Cards Sorting(树状数组+线段树)
- Codeforces 101B Buses 排序+树状数组
- codeforces 368B B. Sereja and Suffixes(树状数组)
- Codeforces Round #248 (Div. 2) B题 【数据结构:树状数组】
- Codeforces Beta Round #10 B. Cinema Cashier (树状数组)
- Codeforces 220B - Little Elephant and Array 离线树状数组
- Little Elephant and Array - CodeForces 220 B 树状数组
- CodeForces 658B Bear and Displayed Friends(树状数组)
- Codeforces Round #367 (Div. 2) B. Interesting drink 树状数组
- CodeForces 706B Interesting drink(树状数组)
- Codeforces #367(Div.2)B Interesting drink【树状数组】
- Problem B +树状数组
- Codeforces Round #225 (Div. 2)(B思维题,E:dfs+树状数组)
- Codeforces 528B Clique Problem dp+线段树(or 树状数组)
- Codeforces 600B Queries about less or equal elements 【离散化去重二分查找 + 树状数组】
- Codeforces Round #381 (Div. 1) B Alyona and a tree 树状数组
- Codeforces 276E(树状数组)
- Cloudera Manager 5 和 CDH5.7.0 本地(离线)安装
- rsync(文件传输工具)
- 继承方式
- Linux虚拟机ip为127.0.0.1的处理
- 微信小程序--部分内容记录(持续更新...)
- codeforces 830B 树状数组
- 比特币改进协议BIP11(翻译)
- Markdown编辑器的使用
- AIDL中的in、out、inout是什么?
- Android Git与sourcetree使用方法(二)项目导入远端仓库
- pat 1009. Product of Polynomials (25)
- angular js 对于数组的操作
- 迄今为止最优的Eclipse运行性能调优 ,含eclipse.ini
- 常见Git推送的报错