1001

给定数n,要我们求该数的二进制中有多少组1, 相邻的1称为1组, 直接位运算摸你即可

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */ int main()
{
int t;
LL n;
scanf("%d", &t);
while (t--)
{
scanf("%I64d", &n);
int ans = ;
bool flag = true;
while (n)
{
if ((n & ))
{
if (flag)
{
ans++;
flag = false;
}
}
else
flag = true; n >>= ;
}
printf("%d\n", ans);
}
return ;
}

1002

比赛的时候,一直认为求逆序对,要用树状数组做, 就YY出了一个树状数组的解法。

我把询问按照L从小到大排序,然后分为两种情况

对于第一种情况,我们只要将左边的数从树状数组中减去,然后加上右边的数即可。

对于第二种情况,我们将数状数组情况, 重新建树状数组

其实比赛完之后,仔细想想,如果存在很多第二种情况(应该最多只有1000种,因为N<=1000) , 那么时间复杂度是N*N*logN, 应该是勉强可以过, 或者直接TLE

不过自己YY出来这种想法, 还是挺好的。

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
struct Node
{
int l, r;
int id;
bool operator<(const Node &rhs)const
{
if (l == rhs.l)
return r < rhs.r;
return l < rhs.l;
}
}Q[+];
int a[ + ];
int b[ + ];
map<int, int> mark;
int ans[ + ];
int sum[ + ];
int lowbit(int x)
{
return x & (-x);
}
void modify(int pos, int val, int n)
{
while (pos <= n)
{
sum[pos] += val;
pos += lowbit(pos);
}
}
int getSum(int pos)
{
int ret = ;
while (pos > )
{
ret += sum[pos];
pos -= lowbit(pos);
}
return ret;
}
int main()
{
int n, q;
while (scanf("%d%d", &n, &q) != EOF)
{
memset(sum, , sizeof(sum));
for (int i = ; i < n; ++i)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
for (int i = ; i < q; ++i)
{
scanf("%d%d", &Q[i].l, &Q[i].r);
Q[i].l--;
Q[i].r--;
Q[i].id = i;
}
sort(a, a + n);
sort(Q, Q + q);
n = unique(a, a + n) - a;
//printf("%d\n", n);
for (int i = ; i < n; ++i)
{
mark[a[i]] = i + ;
//printf("%d %d\n", a[i], i + 1);
}
int preL = ;
int preR = ;
int t;
int tmp = ;
for (int i = ; i < q; ++i)
{
if (preR - > Q[i].r)//第二种情况, 清空树状数组
{
memset(sum, , sizeof(sum));
preR = Q[i].l;
preL = Q[i].l;
tmp = ;
}
for (int j = preL; j < Q[i].l; ++j)//减去左边的区间
{
tmp -= getSum(mark[b[j]]-);
modify(mark[b[j]], -, mark[a[n - ]]);
preL = Q[i].l; } for (int j = preR; j <= Q[i].r; ++j)
{ modify(mark[b[j]], , mark[a[n-]]);
tmp += getSum(mark[a[n-]])-getSum(mark[b[j]]);
}
preR = Q[i].r + ;
ans[Q[i].id] = tmp;
}
for (int i = ; i < q; ++i)
printf("%d\n", ans[i]);
}
return ;
}

正解应该是。

设ans1[i][j] 为区间i-->j,包含a[i]的逆序对数,  首先我们暴力算好ans1[i][j] ,时间复杂度为O(N*N)

设ans2[i][j] 为区间i-->j中的逆序对数,

那么ans2[i][j] = ans1[i][j] + ans[i+1][j] +...+ans[j][j]

优化一下就是, 如果ans2[i+1][j] 已经算好了,  那么ans2[i][j] = ans2[i+1][j] + ans1[i][j];

所以这个的时间复杂度也是O(N*N),而且 ans1 和ans2可以合成一个数组

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
const int N = + ;
int ans[N][N];
int a[N];
int main()
{
int n, q;
while (scanf("%d%d", &n, &q) != EOF)
{
for (int i = ; i <= n; ++i)
scanf("%d", &a[i]); //这里的ans[i][j] 表示区间i-->j, 包含元素a[i]的逆序对数
for (int i = ; i <= n; ++i)
for (int j = i; j <= n; ++j)
ans[i][j] += ans[i][j - ] + (a[i] > a[j] ? : ); //那么 ans[i][j] 要表示区间i-->j的逆序对数时, ans[i][j] += ans[i+1][j] + ans[i+2][j]+...+ans[j][j]
// 如果ans[i+1][j]已经算出来了, 那么ans[i][j] += ans[i+1][j] 即可, 因为贡献是后缀和性质的
for (int j = ; j <= n; ++j)
for (int i = j; i >= ; --i)
ans[i][j] += ans[i + ][j];
int l, r;
while (q--)
{
scanf("%d%d", &l, &r);
printf("%d\n", ans[l][r]);
}
}
return ;
}

1003

给我们一棵树,每个节点都有权值, 有两个操作

0 x y ,  将节点x的权值改为y

1 x y   询问x->y 的路径上, 是否有权值的次数出现奇数次(保证最多只有一个奇数次) 如果没有,输出-1, 如果有, 输出那个权值

任意两点的路径, 可以用lca求出, 然后要求权值是不是出现奇数次, 可以用异或,   a^a = 0  ,  a^a^a=a,

然而这并没有什么用,TLE, 如果树退化成链, 那么每次询问的时间复杂度是O(N),每次询问的复杂度要O(logN)才行

 #pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/*
RE
*/
const int N = + ;
struct Edge
{
int to, next;
}g[N]; int value[N], head[N], e, parent[N], depth[N];
void addEdge(int a, int b)
{
g[e].to = b;
g[e].next = head[a];
head[a] = e++;
}
void dfs(int u, int fa, int d)
{
depth[u] = d;
for (int i = head[u]; i != -; i = g[i].next)
{
int v = g[i].to;
if (v == fa) continue;
dfs(v, u,d + );
}
} int solve(int x, int y)
{
if (x == y)
return value[x];
int ret = ;
ret ^= value[x];
//ret ^= value[y];
if (depth[x] < depth[y])
swap(x, y);
int t = y;
int d = depth[x];
while(d > depth[y])
{
x = parent[x];
d--;
ret ^= value[x];
}
while (x!=y)
{
x = parent[x];
y = parent[y];
ret ^= value[x];
ret ^= value[y];
}
if (t != y)
ret ^= value[y];
return ret; }
int main()
{
int t, n, q, a, b, op;
scanf("%d", &t);
while (t--)
{
memset(parent, -, sizeof(parent));
memset(depth, , sizeof(depth));
memset(head, -, sizeof(head));
e = ;
scanf("%d%d", &n, &q);
for (int i = ; i < n; ++i)
{
scanf("%d%d", &a, &b);
addEdge(a, b);
addEdge(b, a);
parent[b] = a; }
int root = ;
while (parent[root] != -)
root = parent[root];
dfs(root,-,);
for (int i = ; i <= n; ++i)
{
scanf("%d", &value[i]);
value[i]++;
}
for (int i = ; i < q; ++i)
{
scanf("%d%d%d", &op, &a, &b);
if (op == )
value[a] = b;
else
{
int ret = solve(a, b);
printf("%d\n", ret ? ret- : -);
}
}
}
return ;
}

题解说这题要用树链剖分, 然后我并不懂

Bestcoder Round#45的更多相关文章

  1. 从lca到树链剖分 bestcoder round#45 1003

    bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ...

  2. BestCoder Round #16

    BestCoder Round #16 题目链接 这场挫掉了,3挂2,都是非常sb的错误 23333 QAQ A:每一个数字.左边个数乘上右边个数,就是能够组成的区间个数,然后乘的过程注意取模不然会爆 ...

  3. hdu5634 BestCoder Round #73 (div.1)

    Rikka with Phi  Accepts: 5  Submissions: 66  Time Limit: 16000/8000 MS (Java/Others)  Memory Limit: ...

  4. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  5. BestCoder Round #89 02单调队列优化dp

    1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01  HDU 5944   水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...

  6. BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元

    BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy  Init函数 然后统计就ok B. 博弈 题  不懂  推了半天的SG.....  结果这 ...

  7. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  8. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  9. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

随机推荐

  1. android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用

    android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用,只要把Imageview的src给去掉就成了,src捕获了bac ...

  2. LeetCode——Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  3. DMA过程分析

    1.1 当我们在应用程序中编写write系统调用,向磁盘中写入数据时,写入请求会先调用底层写函数,将请求先写入内存中的页快速缓存(page cache)中,写入成功则立马返回,真正的写入磁盘操作会延迟 ...

  4. Maven 建 Struts2 基本实现 CURD Controller

    开发环境 开发工具:Eclipse 数据库:MySQL server:Tomcat Struts2 请求原理流程图 构建一个 web maven project,在pom.xml引入struts2的j ...

  5. maven使用.02.一些概念

    在上一篇POST中,简要的介绍了一下maven的特点,优势,安装.并建立了一个简单地Hello world工程.这一篇POST中,将主要会介绍一下Maven的一些约定. pom.xml文件 Maven ...

  6. Api之Cors跨域以及其他跨域方式

    Web Api之Cors跨域以及其他跨域方式(三)   我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...

  7. Android实现位图剪切

    我们不能总是依赖于BitmapFactory 以下告诉大家怎么从Bitmaqp中截取某一部分创建新的Bitmap  系统会有一个默认png图片:icon.png 可是这个图片中最外层会有白色的 比較讨 ...

  8. Cocos2d-x 游戏存档

    游戏存档功能能够保存游戏中数据.让玩家游戏能够延续. 单机游戏更为重要.而CCUserDefault能够作轻量级数据库使用,用来存储数据,支持数据类型bool,int, float, double, ...

  9. Managing Data in Containers

    Managing Data in Containers So far we've been introduced to some basic Docker concepts, seen how to ...

  10. hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))

    链接: huangjing 题目:中文题目 思路: 1:这个题目假设去掉那个距离大于d的条件,那么必定是一个普通的LIS.可是加上那个条件后就变得复杂了.我用的线段树的解法. . .就是採用延迟更新的 ...