HDU 4031 树状数组 区间更新及点询问
2012-01-19 17:42
453 查看
/******************************************************************************* 去年成都赛区网络赛一道题,树状数组在区间更新中的应用。树状数组一般支持的是改点,查区间,但 是这道题要求的是改区间,查点,这就要变通一下,具体可以看代码,另外要考虑的一个问题是,如何统计? 这里的处理方法就是把总共的攻击次数-防御的次数,因为对单个点的询问可能有多次,所以为每个点都设 定一个游标来优化时间~ *******************************************************************************/ #include <iostream> #include <functional> #include <algorithm> #include <complex> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <sstream> #include <utility> #include <bitset> #include <cctype> #include <cstdio> #include <limits> #include <memory> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <list> #include <map> #include <set> using namespace std; #define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) ) #define CLR(x, k) memset((x), (k), sizeof(x)) #define CPY(t, s) memcpy((t), (s), sizeof(s)) #define SC(t, s) static_cast<t>(s) #define LEN(s) static_cast<int>( strlen((s)) ) #define SZ(s) static_cast<int>( (s).size() ) typedef double LF; typedef __int64 LL; //VC typedef unsigned __int64 ULL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<char> VC; typedef vector<double> VF; typedef vector<string> VS; template <typename T> T sqa(const T & x) {return x * x;} template <typename T> T ll(const T & x) {return x << 1;} template <typename T> T rr(const T & x) {return x << 1 | 1;} template <typename T> T gcd(T a, T b) { if (!a || !b) { return max(a, b); } T t; while (t = a % b) { a = b; b = t; } return b; }; const int INF_INT = 0x3f3f3f3f; const LL INF_LL = 0x7fffffffffffffffLL; //15f const double oo = 10e9; const double eps = 10e-7; const double PI = acos(-1.0); #define ONLINE_JUDGE const int MAXN = 20004; int test, n, q, t; int tree[MAXN]; int crs[MAXN], def[MAXN]; vector<PII> vp; void updateBIT(int x, int inc) { for (int i = x; i > 0; i -= LOWBIT(i)) { tree[i] += inc; } return ; } void updateSeg(int l, int r, int inc) { updateBIT(l - 1, -inc); updateBIT(r, inc); return ; } int queryBIT(int x) { int sum = 0; for (int i = x; i <= n; i += LOWBIT(i)) { sum += tree[i]; } return sum; } void ace() { int cas = 1; char op[10]; int l, r, p; for (scanf("%d", &test); test--; ++cas) { scanf("%d %d %d", &n, &q, &t); CLR(tree, 0); CLR(crs, 0); CLR(def, 0); vp.clear(); int ind = 0; printf("Case %d:\n", cas); while (q--) { scanf("%s", op); if ('A' == op[0]) { scanf("%d %d", &l, &r); if (l > r) { swap(l, r); } //l = max(l, 1); //r = min(r, n); updateSeg(l, r, 1); vp.push_back(PII(l, r)); ++ind; } else { scanf("%d", &p); if (1 == t) { puts("0"); continue ; } int token = 0; while (crs[p] < ind) { if (vp[ crs[p] ].first <= p && p <= vp[ crs[p] ].second) { if (0 == token % t) { crs[p] += t - 1; ++def[p]; token = t - 1; } ++token; } ++crs[p]; } printf("%d\n", queryBIT(p) - def[p]); } } } return ; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ace(); return 0; } /******************************************************************************* Test Data... 2 3 7 2 Attack 1 2 Query 2 Attack 2 3 Query 2 Attack 1 3 Query 1 Query 3 9 7 3 Attack 5 5 Attack 4 6 Attack 3 7 Attack 2 8 Attack 1 9 Query 5 Query 3 *******************************************************************************/
相关文章推荐
- [HDU 4031]Attack[树状数组区间更新单点查询]
- 树状数组的区间更新 HDU 1556 示例
- HDU - 1556 树状数组(区间修改+单点更新)
- [HDU 1754]I Hate It[树状数组查询+更新区间最大值]
- 树状数组模板区间更新 区间询问
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
- hdu 4533(树状数组区间更新+单点查询)
- poj3468树状数组之区间更新+区间询问
- HDU 1556 Color the ball 树状数组-区间更新-模板
- Hdu 1166 敌兵布阵 树状数组 或 线段树 单点更新,区间求和
- hdu 4970 树状数组区间更新 思维题
- HDU 1166 敌兵布阵 (树状数组--单点更新,区间求值)
- HDU 4267 A Simple Problem with Integers(树状数组区间更新)
- hdu 3584 Cube (三维树状数组,更新区间,查询单点)
- HDOJ 4031 - Attack 区间更新点查询的树状数组+暴力...
- POJ 2352 Stars + HDU 1556 Color the ball(树状数组单点更新及伪区间更新)
- HDU 1556 Color the ball (线段树|树状数组,区间更新)
- 树状数组:HDU1166敌兵布阵 【单点更新,区间查询】
- HDu 1556 Color the ball【线段树&&树状数组】区间更新,单点查询