CodeChef:Chef and Subarray Queries(线段树)
2017-11-22 19:27
357 查看
思路:求值在[L,R]之间的题目,往往转化成求[1,R]-[1,L-1],然后这题用线段树处理,每个节点计算当前区间的答案,主要思考合并操作的写法,代码思路还是比较简单的。
# include <iostream>
# include <cstdio>
# define lson l,m,id<<1
# define rson m+1, r, id<<1|1
using namespace std;
typedef long long LL;
const int maxn = 5e5+30;
struct node
{
int left, right, len;
LL ans;
};
node a[maxn<<4], b[maxn<<4];
node merge(node &ta, node &tb)
{
node res;
res.ans = ta.ans + tb.ans;
res.ans += 1LL*ta.right*tb.left;
res.len = ta.len + tb.len;
res.left = ta.left;
res.right = tb.right;
if(ta.left == ta.len) res.left += tb.left;
if(tb.right == tb.len) res.right += ta.right;
return res;
}
void build(node arr[], int l, int r, int id)
{
if(l == r)
{
arr[id].left = arr[id].right = arr[id].len = 1;
arr[id].ans = 1LL;
return;
}
int m = l+r>>1;
build(arr, lson);
build(arr, rson);
arr[id] = merge(arr[id<<1], arr[id<<1|1]);
}
void update(node arr[], int pos, int val, int l, int r, int id)
{
if(l == r)
{
arr[id].ans = 1LL*val;
arr[id].left = arr[id].right = val;
return;
}
int m = l+r>>1;
if(pos <= m) update(arr, pos, val, lson);
else update(arr, pos, val, rson);
arr[id] = merge(arr[id<<1], arr[id<<1|1]);
}
node query(node arr[], int L, int R, int l, int r, int id)
{
if(L <= l && R >= r)
return arr[id];
int m = l+r>>1;
node tl = {0,0,0,0}, tr = {0,0,0,0};
if(L <= m) tl = query(arr, L, R, lson);
if(R > m) tr = query(arr, L, R, rson);
return merge(tl, tr);
}
int main()
{
int n, q, l, r;
scanf("%d%d%d%d",&n,&q,&l,&r);
build(a, 1, n, 1);
build(b, 1, n, 1);
while(q--)
{
int op, x, y;
scanf("%d%d%d",&op,&x,&y);
if(op == 1)
{
if(y <= r) update(a, x, 1, 1, n, 1);
else update(a, x, 0, 1, n, 1);
if(y < l) update(b, x, 1, 1, n, 1);
else update(b, x, 0, 1, n, 1);
}
else
{
LL ans = 0;
ans = query(a, x, y, 1, n, 1).ans-query(b, x, y, 1, n, 1).ans;
printf("%lld\n",ans);
}
}
return 0;
}
相关文章推荐
- 【Codechef】【Chef and Graph Queries】Lct 可持久化线段树
- [CodeChef - GERALD07 ] Chef and Graph Queries
- 【codechef】Chef and the Cards(dp,推论)
- 【欧拉筛+主席树】CodeChef PRMQ Chef and Prime Queries
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
- 【codechef】Chef and Polygons(灵活题,坑题)
- 【CodeChef】 Chef and Stones
- CodeChef JUNE17 - Chef and Prime Queries
- 【codechef】Chef and Prime Divisors(判断A能否被B的所有素因子整除)
- 【CodeChef】Chef and Interview
- codechef Tree and Queries Solved
- [点分树] Codechef December Challenge 2017. Chef, Leonardo And Queries
- 【线段树区间单点更新加约束条件】Range Minimum Queries CodeChef - ANDMIN
- 【codechef】Chef and the Number Sequence(构成最长公共子序列为L的可能性)
- CC DEC.17 Chef And Easy Xor Queries 分块+懒标记
- 【codechef】 Chef and Piano Scales (简单题)
- 【codechef】Chef and A Large Permutation(技巧题)
- 【codeforces】678 F. Lena and Queries【线段树维护凸壳】
- 【codechef】Chef and Bracket-Pairs (分层dp)
- 【Codechef】Chef and Bike(二维多项式插值)