HDU(3874)树状数组+离线
2016-01-29 16:18
302 查看
题目大意:和 HDU3333 一样。。。=_= 数据范围有些变化。
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <string> using namespace std; const int maxn = 55555; struct node { int l, r; int id; bool operator < (const node &a)const { return r < a.r; } }p[maxn << 2]; int n, q; long long sum[maxn << 2]; long long ans[maxn << 2]; long long a[maxn], b[maxn]; long long tail[maxn]; int lowbit(int x) { return x&(-x); } void modify(int pos, long long val) { for(int i=pos; i<=n; i+=lowbit(i)) { sum[i] += val; } } long long query(int pos) { long long res = 0; for(int i=pos; i>0; i-=lowbit(i)) { res += sum[i]; } return res; } int main() { int T; scanf("%d", &T); while(T--) { memset(sum, 0, sizeof sum); memset(tail, 0, sizeof tail); scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%lld", &a[i]); b[i] = a[i]; } scanf("%d", &q); for(int i=1; i<=q; i++) { scanf("%d%d", &p[i].l, &p[i].r); p[i].id = i; } sort(b+1, b+1+n); sort(p+1, p+1+q); int tot = 1; for(int i=1; i<=n; i++) { int pos = lower_bound(b+1, b+1+n, a[i]) - b; if(!tail[pos]) { modify(i, a[i]); tail[pos] = i; } else { modify(tail[pos], a[i] * (-1)); modify(i, a[i]); tail[pos] = i; } while(p[tot].r <= i && tot <= q) { ans[p[tot].id] = query(i) - query(p[tot].l - 1); tot++; } } for(int i=1; i<=q; i++) { printf("%lld\n", ans[i]); } } return 0; }
相关文章推荐
- Weblogic 节点启动
- AIX Version 6 裸设备 小机上增加数据文件
- Swift 之Protocol在cocoa中的使用范例搜集(一)
- HBase二级索引与Join
- js大小写转换
- Shell case esac语句
- 超实用!移动端界面中的版式设计原理(上)
- linux命令-cat
- Shell if else语句
- CentOS device eth0 does not seem to be present...
- s3 分段下载核心原理-获取指定范围的文件内容
- 走近OSSIM传感器(Sensor)插件
- 【选择图片Jar】“MVC“来实现逻辑处理与View视图的分离(五)
- Git 使用心得
- 收不到组播的异常情况
- matlab中fix, floor, ceil, round 函数的使用方法
- 你需要知道的12个Git高级命令
- 分布式缓存memcached介绍,win7环境安装,常用命令set,get,delete,stats, java访问
- Tornado从入门到入门
- WebView