4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
来源:互联网 发布:手机淘宝旧版本6.5.0 编辑:程序博客网 时间:2024/06/07 06:33
4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 25 Solved: 20
[Submit][Status][Discuss]
Description
给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数
Input
Output
Sample Input
4
3
2
4
4
1
3
2
1
3
2
4
4
1
3
2
1
Sample Output
3
刚开始写了个滑动窗口 写到一半发现GG
重新想一想 其实就是从前向后扫 每次询问区间内权值和
#include<cmath>#include<ctime>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<bitset>#include<queue>#include<set>#include<map>using namespace std;typedef long long ll;inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void print(ll x){if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}const int N=100100;int n,w[N],s[N],bit[N];inline void modify(int x){for(;x<=n;x+=(x&-x))bit[x]++;}inline int query(int x){int res=0;for(;x;x-=(x&-x))res+=bit[x];return res;}bool book[N];int main(){n=read()<<1;register int i,x;for(i=1;i<=n;++i){x=read();if(!book[x])book[x]=1,w[x]=i;else s[w[x]]=i-w[x];}ll ans=0;modify(1+s[1]);for(i=2;i<=n;++i)if(s[i]){ans+=query(i+s[i])-query(i);modify(i+s[i]);}print(ans);puts("");return 0;}/*43 2 4 4 1 3 2 13*/
阅读全文
0 0
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套Treap
- [BZOJ]4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III CDQ分治+树状数组
- BZOJ 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套splay【未AC】
- 滑动解锁插件slideunlock.js不依赖jquery
- 使用POI操作excel(读取和创建)快速开发指南
- 【Unity技巧】Unity中的优化技术
- Django middleware 中间件
- Linux系统安装之CentOS 6.8 安装步骤
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- 移动端Jenkins持续集成攻略(2)
- sizeof和strlen用法区别(转载)谢谢大牛
- KVM虚拟化平台部署
- 阿里面试官:什么样的人能在阿里晋升?相信你能在文章找到答案
- MySQL与Oracle UPDATE多条记录不同值,同时UPDATE多个字段
- SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)
- maven scala idea 步骤
- 逆波兰表达式