【洛谷】2345 奶牛集会 树状数组
来源:互联网 发布:淘宝上买黑莓手机 编辑:程序博客网 时间:2024/06/11 04:57
题目传送门
题目描述:……摸牛仔的屁股……。话说这不是LYF最喜欢的游戏吗?
考虑题目给出的公式,
然后考虑绝对值,如果
但是!这个结论是非常有用的!接着往下看就知道了。
对于一头奶牛
显然上述的公式中的
于是这题就被愉快的A掉啦。
附上AC代码:
#include <cstdio>#include <cctype>#include <algorithm>using namespace std;const int N=2e4+10;struct note{ int v,x; bool operator < (const note lyf) const {return v<lyf.v;}}a[N];int n,m,sum[N],num[N];long long ans;inline char nc(){ static char ch[100010],*p1=ch,*p2=ch; return p1==p2&&(p2=(p1=ch)+fread(ch,1,100010,stdin),p1==p2)?EOF:*p1++;}inline void read(int &a){ static char c=nc();int f=1; for (;!isdigit(c);c=nc()) if (c=='-') f=-1; for (a=0;isdigit(c);a=a*10+c-'0',c=nc()); return (void)(a*=f);}#define lowbit(x) ((x)&(-x))inline int query(int *t,int x){int sum=0; for (int i=x; i; i-=lowbit(i)) sum+=t[i]; return sum;}inline void add(int *t,int x,int w){for (int i=x; i<=m; i+=lowbit(i)) t[i]+=w; return;}int main(void){ read(n); for (int i=1; i<=n; ++i) read(a[i].v),read(a[i].x),m=max(m,a[i].x); sort(a+1,a+1+n); for (int i=1; i<=n; ++i){ int numl=query(num,a[i].x-1); int suml=query(sum,a[i].x-1); int numr=query(num,m)-query(num,a[i].x); int sumr=query(sum,m)-query(sum,a[i].x); ans+=1ll*a[i].v*(sumr-a[i].x*numr+a[i].x*numl-suml); add(num,a[i].x,1),add(sum,a[i].x,a[i].x); } return printf("%lld\n",ans),0;}
阅读全文
0 0
- 【洛谷】2345 奶牛集会 树状数组
- 奶牛集会
- 奶牛集会
- 【洛谷】2344 奶牛抗议 DP+树状数组
- BZOJ1827 奶牛大集会
- 【奶牛大集会】解题报告
- 树形问题--奶牛大集会
- 【带权中位数】奶牛大集会
- USACO 2004 OPEN Moofest 奶牛集会
- BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
- 1827: [Usaco2010 Mar]gather 奶牛大集会
- 【USACO】2004 Open MooFest 奶牛集会
- P2345 奶牛集会/P2657 低头一族
- BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
- 【USACO】2004 Open MooFest 奶牛集会
- 【动态规划】【树状数组】[USACO2011 FEB]奶牛抗议
- Usaco 奶牛抗议(树状数组+DP+离散化)
- [树Dfs][Usaco2010 Mar]gather奶牛大集会
- 【洛谷】1637 三元上升子序列 树状数组
- 转载:linux内核input子系统代码实现流程
- 百钱百鸡问题
- 对拍 入门+模板
- Python: PS 滤镜--水波特效
- 【洛谷】2345 奶牛集会 树状数组
- 斐波那契公约数
- Android stduio 开发百度地图API版本4.5(1)
- TV TimeShift和PVR的区别
- scss学习笔记
- bzoj1855 SCOI2010 股票交易【单调队列优化dp】
- Anagrams
- 【javascript常见面试题】常见前端面试题及答案
- Java练习