HDU - 1698 Just a Hook

来源:互联网 发布:java教务管理系统源码 编辑:程序博客网 时间:2024/04/27 23:22

1.题面

http://acm.hdu.edu.cn/showproblem.php?pid=1698

2.题意

给你一个数组,将从[a,b]的一段元素的值设置为c,最后希望你输出元素和。

3.思路

线段树的区间修改,一开始忘记了下传标记,后来下传标记了还是错,最后经过我的仔细查找,发现错在我没写完整输出的那个句子。

4.代码

/*****************************************************************    > File Name: Cpp_Acm.cpp    > Author: Uncle_Sugar    > Mail: uncle_sugar@qq.com    > Created Time: 2016年06月30日 星期四 22时56分24秒*****************************************************************/# include <cstdio># include <cstring># include <cctype># include <cmath># include <cstdlib># include <climits># include <iostream># include <iomanip># include <set># include <map># include <vector># include <stack># include <queue># include <algorithm>using namespace std;const int debug = 1;const int size  = 500000 + 10; const int INF = INT_MAX>>1;typedef long long ll;struct node{int flag;int l,r;} tree[size];void Build(int no,int l,int r){tree[no].l = l;tree[no].r = r;tree[no].flag = 1;if (l==r) return;int mid = (l+r)>>1;Build(no*2,l,mid);Build(no*2+1,mid+1,r);}void Color(int no,int l,int r,int type){//# cout << no << " " << tree[no].l << " " << tree[no].r << " " << type << endl;if (l<=tree[no].l&&r>=tree[no].r){tree[no].flag = type; }else {if (tree[no].flag!=-1)tree[2*no].flag = tree[2*no+1].flag = tree[no].flag;int mid = (tree[no].l+tree[no].r)>>1;if (l<=mid) Color(no*2,l,r,type);if (r>mid)Color(no*2+1,l,r,type);if (tree[no*2].flag==tree[no*2+1].flag)tree[no].flag = tree[no*2].flag;elsetree[no].flag = -1;}}int Sum(int no = 1){if (tree[no].flag!=-1){//# cout << tree[no].l << " " << tree[no].r << " " << tree[no].flag*(tree[no].r - tree[no].l + 1) << endl;return tree[no].flag*(tree[no].r - tree[no].l + 1);}return Sum(no*2) + Sum(no*2+1);}int main(){std::ios::sync_with_stdio(false);cin.tie(0);int i,j;int T;cin >> T;int ncase = 1;while (T--){int n,q;cin >> n >> q;Build(1,1,n);while (q--){int a,b,c;cin >> a >> b >> c;Color(1,a,b,c);}//# cout << "@@@@@@@@@@@@@@@" << endl;cout << "Case " << ncase++ << ": The total value of the hook is " << Sum() << "." << endl;}return 0;}


0 0
原创粉丝点击