HDU 4027

线段树变种
在2logn段上面做文章,swap(x, y)太阴了...


/*
* Author: rush
* Filename: segment_sum2.cpp
* Timestamp: 2012-02-04 19:12:20 CST
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <sstream>
#define OUT(x) cerr << #x << ": " << (x) << endl
#define SZ(x) ((int)x.size())
#define FOR(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;

// Segment Tree for Sum
// Accepted: hdu1166, hdu1754, hdu1394, hdu2795
// Accepted: hdu1698, pku3468, pku2528
template<class Data>
struct SegmentTreeSum {
#define LSON t * 2 + 1, a, (a + b) / 2
#define RSON t * 2 + 2, (a + b) / 2 + 1, b
// 0) NONE: 0x3f3f3f3f, (int)(~0U >> 1), 1LL << 62, (LL)(~0ULL >> 1), 1.0e300
#define NONE -(LL)(~0ULL >> 1)
// 1) OP: x = y, x = (x != NONE ? x + y : y)
inline void OP(Data &x, Data y) { x = y; }

vector<Data> lazy;
vector<Data> node;
vector<int> one;
void init(int N) {
lazy.assign(N * 4 + 5, NONE);
node.assign(N * 4 + 5, 0);
one.assign(N * 4 + 5, false);
}
inline void up(int t) {
node[t] = node[t * 2 + 1] + node[t * 2 + 2];
one[t] = one[t * 2 + 1] && one[t * 2 + 2];
}
void down(int t, int a, int b) {
if (lazy[t] != NONE) {
OP(lazy[t * 2 + 1], lazy[t]);
OP(lazy[t * 2 + 2], lazy[t]);
int k = (a + b) / 2;
OP(node[t * 2 + 1], lazy[t] * (k - a + 1));
OP(node[t * 2 + 2], lazy[t] * (b - (k + 1) + 1));
lazy[t] = NONE;
}
}
void update(int x, int y, Data delta, int t, int a, int b) {
if (x <= a && b <= y) { // inside
OP(lazy[t], delta);
OP(node[t], delta * (b - a + 1));
if (node[t] <= 1LL) one[t] = true;
} else if (a <= y && x <= b) { // overlap
//down(t, a, b);
update(x, y, delta, LSON);
update(x, y, delta, RSON);
up(t);
}
}
bool doMaintain;
void maintain(int t, int a, int b) {
if (a == b) {
const double EPS = 1.0e-9;
node[t] = (LL)sqrt(node[t] + EPS);
if (node[t] <= 1LL) one[t] = true;
} else {
//down(t, a, b);
if (!one[t * 2 + 1]) maintain(LSON);
if (!one[t * 2 + 2]) maintain(RSON);
up(t);
}
}
Data queryValue(int x, int y, int t, int a, int b) {
if (x <= a && b <= y) { // inside
if (doMaintain) maintain(t, a, b);
return node[t];
} else if (a <= y && x <= b) { // overlap
//down(t, a, b);
Data ans = 0;
ans += queryValue(x, y, LSON);
ans += queryValue(x, y, RSON);
up(t);
return ans;
}
return 0;
}
};

int main() {
SegmentTreeSum<LL> st;
int N, M, id = 1;
while (scanf("%d", &N) != EOF) {
st.init(N);
for (int i = 1; i <= N; ++i) {
LL t;
scanf("%I64d", &t);
st.update(i, i, t, 0, 1, N);
}
printf("Case #%d:\n", id++);
scanf("%d", &M);
while (M--) {
int T, X, Y;
scanf("%d%d%d", &T, &X, &Y);
if (X > Y) swap(X, Y);
if (T == 0) {
st.doMaintain = true;
st.queryValue(X, Y, 0, 1, N);
} else {
st.doMaintain = false;
LL ans = st.queryValue(X, Y, 0, 1, N);
printf("%I64d\n", ans);
}
}
printf("\n");
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值