hdu 4027 Can you answer these queries?
2016-02-25 16:08
447 查看
线段树区间更新问题,lazy标记当前数能否开方
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <list> #include <queue> #include <map> using namespace std; #define L(i) i<<1 #define R(i) i<<1|1 #define INF 0xFFFFFFFF #define pi acos(-1.0) #define eps 1e-4 #define maxn 100010 #define MOD 1000000007 struct node { int l,r; long long sum,lazy; }tree[maxn*4]; void pushup(int pos) { tree[pos].sum = tree[pos<<1].sum + tree[pos<<1|1].sum; tree[pos].lazy = tree[pos<<1].lazy && tree[pos<<1|1].lazy; } //void pushdown(int pos) //{ // if(!tree[pos].lazy) // return; // tree[pos<<1].sum += (tree[pos<<1].r-tree[pos<<1].l+1)*tree[pos].lazy; // tree[pos<<1|1].sum += (tree[pos<<1|1].r-tree[pos<<1|1].l+1)*tree[pos].lazy; // tree[pos<<1].lazy += tree[pos].lazy; // tree[pos<<1|1].lazy += tree[pos].lazy; // tree[pos].lazy = 0; //} void build(int l,int r,int pos) { tree[pos].l = l; tree[pos].r = r; tree[pos].sum = 0; tree[pos].lazy = 0; if(l == r) { scanf("%I64d",&tree[pos].sum); return; } int mid = (l+r)/2; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); pushup(pos); } //建树 //查询操作 void update(int l,int r,int pos) { // if(tree[pos].l == l && tree[pos].r == r) // { // tree[pos].sum += (r-l+1)*add; // tree[pos].lazy += add; // return ; // } if(tree[pos].l == tree[pos].r) { tree[pos].sum = sqrt(tree[pos].sum); if(tree[pos].sum <= 1) tree[pos].lazy = 1; return; } //pushdown(pos); int mid = (tree[pos].l + tree[pos].r) >> 1; if(r <= mid && !tree[pos<<1].lazy) update(l,r,pos<<1); else if(l > mid && !tree[pos<<1|1].lazy) update(l,r,pos<<1|1); else if(l <= mid && r > mid) { if(!tree[pos<<1].lazy) update(l,mid,pos<<1); if(!tree[pos<<1|1].lazy) update(mid+1,r,pos<<1|1); } pushup(pos); } //自上而下更新节点 long long query(int l,int r,int pos) { if(l == tree[pos].l && r == tree[pos].r) return tree[pos].sum; //pushdown(pos); int mid = (tree[pos].l+tree[pos].r)>>1; if(r <= mid) return query(l,r,pos<<1); else if(l > mid) return query(l,r,pos<<1|1); else return query(l,mid,pos<<1) + query(mid+1,r,pos<<1|1); } int main() { int t,n,m,C = 1; //scanf("%d",&t); while(scanf("%d",&n) != EOF) { int a,b,c; build(1,n,1); scanf("%d",&m); printf("Case #%d:\n",C++); for(int i = 1; i <= m; i++) { getchar(); scanf("%d%d%d",&a,&b,&c); if(b > c) swap(b,c); if(a) printf("%I64d\n",query(b,c,1)); else update(b,c,1); } printf("\n"); } return 0; }
相关文章推荐
- iOS initWithFrame:frame] 与 [UIButton buttonWithType] 对比
- CodeForces 626 A. Robot Sequence(水~)
- Android uiautomator环境搭建
- angular 服务(factory service constant value provider)学习笔记
- Divide and Conquer
- 1101 Quick Sort
- UIImageView用法
- 浅谈 easyui tabs 的href和content属性
- java request获取各种数据
- incredibuild 命令行 error D8037
- 用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
- Ultra-QuickSort(树状数组求逆序对数)
- IOS之UITabBarController
- 基于pt-query-digest的慢查询日志持续追踪 推荐
- 从源代码安装Guidance
- ./configure: error: the GeoIP module requires the GeoIP library.
- 《iOS Human Interface Guidelines》——The Status Bar
- UIResponder
- Formal System-相继式演算(Sequenz)
- iOS开发系列--UITableView全面解析