HDU - 5372 Segment Game 树状数组
我们考虑总的线段减去违法的线段, 因为是按长度从小到大加入数轴的, 所以违法线段就是Rj > Ri的个数加上 Lj < Li 的个数,
用树状数组维护就完事了。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 4e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; int addtot; int X[N], Xtot; int ans[N]; int id[N]; int L[N], R[N]; int op[N], b[N]; struct Bit { int a[N]; void init() { for(int i = 1; i <= Xtot; i++) { a[i] = 0; } } inline void modify(int x, int v) { for(int i = x; i <= Xtot; i += i & -i) { a[i] += v; } } inline int sum(int x) { int ans = 0; for(int i = x; i; i -= i & -i) { ans += a[i]; } return ans; } inline int query(int L, int R) { if(L > R) return 0; return sum(R) - sum(L - 1); } } bit[2]; void init() { Xtot = addtot = 0; } int main() { int cas = 0; while(scanf("%d", &n) != EOF) { init(); int now = 0; for(int i = 1; i <= n; i++) { scanf("%d%d", &op[i], &b[i]); if(!op[i]) { addtot++; ans[addtot] = now; id[i] = addtot; L[addtot] = b[i]; R[addtot] = b[i] + addtot; X[++Xtot] = b[i]; X[++Xtot] = b[i] + addtot; now++; } else { now--; } } sort(X + 1, X + 1 + Xtot); Xtot = unique(X + 1, X + 1 + Xtot) - X - 1; for(int i = 1; i <= addtot; i++) { L[i] = lower_bound(X + 1, X + 1 + Xtot, L[i]) - X; R[i] = lower_bound(X + 1, X + 1 + Xtot, R[i]) - X; } bit[0].init(); bit[1].init(); for(int i = 1; i <= n; i++) { if(op[i] == 0) { ans[id[i]] -= bit[0].query(1, L[id[i]] - 1); ans[id[i]] -= bit[1].query(R[id[i]] + 1, Xtot); bit[0].modify(L[id[i]], 1); bit[1].modify(R[id[i]], 1); } else { bit[0].modify(L[b[i]], -1); bit[1].modify(R[b[i]], -1); } } printf("Case #%d:\n", ++cas); for(int i = 1; i <= addtot; i++) { printf("%d\n", ans[i]); } } return 0; } /* */
分类:
树状数组
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话