1. 程式人生 > >[Luogu] 貪婪大陸

[Luogu] 貪婪大陸

namespace org mat col name 單點 main ble math

https://www.luogu.org/problemnew/show/P2184

區間修改時只需修改區間端點的numl或numr值
區間查詢x-y只需用1-y的numr - 1-(x - 1)的numl值
線段樹 單點修改 + 區間查詢

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;
const int N = 1e5 + 10;

#define yxy getchar()
#define
lson jd << 1 #define rson jd << 1 | 1 #define RR freopen("gg.in", "r", stdin) int n, Ty, Ans, Ans1, Ans2; int Numl[N << 2], Numr[N << 2]; inline int read() { int x = 0; char c = yxy; while(c < 0 || c > 9) c = yxy; while(c >= 0 && c <=
9) x = x * 10 + c - 0, c = yxy; return x; } void Poi_G(int l, int r, int jd, int x, int how) { if(l == r) { if(!how) Numl[jd] ++; else Numr[jd] ++; return ; } int mid = (l + r) >> 1; if(x <= mid) Poi_G(l, mid, lson, x, how); else
Poi_G(mid + 1, r, rson, x, how); Numl[jd] = Numl[lson] + Numl[rson]; Numr[jd] = Numr[lson] + Numr[rson]; } void Sec_A(int l, int r, int jd, int x, int y, int how) { if(x <= l && r <= y) { if(!how) Ans += Numl[jd]; else Ans += Numr[jd]; return ; } int mid = (l + r) >> 1; if(x <= mid) Sec_A(l, mid, lson, x, y, how); if(y > mid) Sec_A(mid + 1, r, rson, x, y, how); } int main() { n = read(); Ty = read(); while(Ty --) { int opt = read(), x = read(), y = read(); if(opt == 1) { Poi_G(1, n, 1, x, 0); Poi_G(1, n, 1, y, 1); } else { Ans = 0; Sec_A(1, n, 1, 1, y, 0); Ans1 = Ans; Ans = 0; Sec_A(1, n, 1, 1, x - 1, 1); Ans2 = Ans; cout << Ans1 - Ans2 << "\n"; } } return 0; }

[Luogu] 貪婪大陸