树状数组,与Turing Tree类似. xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l] 其中xr[r] ^ xr[l] 相当于l + 1到r出现奇数次的数的异或,sum(r) ^ sum(l)表示l + 1到r每个数只考虑一次的异或,则两者异或为出现偶数次的数的异或. #include<cstdio> #include<iostream> #include<cstdlib>…
题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k](1<=k<j),由于要求前缀和,可以用树状数组优化 #include<bits/stdc++.h> #define lowbit(x) x&(-x) typedef long long ll; const int N=1e5+3; ll dp[12][N]; using n…
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长度为j的上升子序列个数,但是dp数组是在树状数组的update函数中进行更新. update(i, val, j)函数表示在i的位置加上val,更新dp[i][j]. sum(i, j)就是求出末尾数字小于等于i 且长度为j的子序列有多少个. //#pragma comment(linker, "/…
树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<vector> #i…
Problem: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val. Example: Given nums = [1, 3, 5] sumRange(0, 2) -> 9 up…
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Petya has an array aa consisting of nn integers. He has learned partial sums recently, and now he can calculate the sum o…
ZYB's Premutation    Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一个排列PP,但他只记得PP中每个前缀区间的逆序对数,现在他要求你还原这个排列. (i,j)(i < j)(i,j)(i<j)被称为一对逆序对当且仅当A_i>A_jA​i​​>A​j​​ 输入描述 第一行一个整数TT表示数据组数. 接下来每组数据: 第一行一个正整数NN,描述排列的长度. 第二行NN个正整数A_iA​i​​,描述前…
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in other words, a string s* of length n, consisting of characters "(" and ")". Sereja needs to answer m queries, each of them is describe…
https://nanti.jisuanke.com/t/41422 题目大意: 给出n和b,求1到n,各数在b进制下各位数之和的总和. 直接暴力模拟,TLE.. 没想到是要打表...还是太菜了. #include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <math.h> #include <algorithm> #inc…
题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$-1$的数量为$cnt$,可以等效成求长度为$cnt$的排列的逆序对期望.设$dp[i]$为长度为$i$的全排列的逆序对期望,有$dp[i]=dp[i-1]+$$\frac{i-1}{2}$,可以理解成在原$dp[i-1]$的基础上,数值$i$对每个长度为$i-1$的排列产生$\sum_{t=1}^{…
题中有两种操作,第一种把某个位置的字母修改,第二种操作查询与[L, R]内与给出字符串循环起来以后对应位置的字母相同的个数.给出的字符串最大长度是10. 用一个四维树状数组表示 cnt[ATCG的编号][可能的给出字符串的长度][原字符中这个位置在循环中的位置][从开始到这个位置] = 出现的次数. 然后对于一开始的字符串,暴力更新每一种情况. 对于1操作,对于每一种可能的循环节长度,把这个位置的贡献清除,然后换成要填入的字母 对于2操作,(l+i)表示在原字符串的位置,(l+i-1)%len表…
题目传送 做法 对于每个人,inc为x,pref为y:对于每道菜,p和s为x,b为y 于是根据题意有\[p[i]<=x<=s[i]\]\[p[i]+b[i]<=x+y\]\[p[i]-b[i]<=x-y\] 把所有出现的点都离散化一下,然后开始扫x轴 对于x+y和x-y这两个函数,分别开一个树状数组去维护合法点的个数 #include <cstdio> #include <cctype> #include <iostream> #include…
转自:https://blog.csdn.net/my_sunshine26/article/details/79831362 题目大意: i从1开始 基本思路: 完全没思路,所以上来就二分,果不其然卡死在了第7个样例,然后认为是贪心,苦思冥想未果,然后就看了答案. 说是主席树模板题,但是可以用巧方法过了,大部分用的都是树状数组好像,然后想了想也没想出来用树状数组能好到那里去, 然后参考了下答案思路,嗯,这绝对不是我能想到的,难受,就很难受. 正确思路: 1.先记录满足i<=a[j]的最大下标i…
枚举,每次增加点,删除点 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<, INF =  ; ;     ;i-=lowbit(i)){         ret+=C[i];     }     , r = n + ;     ;         ;         }].num; } ; i < n; i++){         …
题目 两个操作: 1. Add x y value: Add value to the element Axy. (Subscripts starts from 0 2. Sum x1 y1 x2 y2: Return the sum of every element Axy for x1 ≤ x ≤ x2, y1 ≤ y ≤ y2. 注意取模,因为value可能为负值 ans%=Mod;        if(ans<0) ans+=Mod; #include <iostream> #i…
You are given an array $a_1,a_2,…,a_n$. All $a_i$ are pairwise distinct. Let's define function $f(l,r)$ as follows: let's define array $b_1,b_2,…,b_{r-l+1}$, where $b_i=a_{l-1+i}$; sort array $b$ in increasing order; result of the function $f(l,r)$ i…
题意:给你一颗树,有q次询问,每次询问给你若干个点,这些点可以最多分出m组,每组要满足两个条件:1:每组至少一个点,2:组内的点不能是组内其它点的祖先,问这样的分组能有多少个? 思路:https://blog.csdn.net/BUAA_Alchemist/article/details/86765501 代码: #include <bits/stdc++.h> #define LL long long #define lowbit(x) (x & (-x)) using namesp…
大意: 给定n*m矩阵, k条链, 链上每个点有权值, 每次操作可以关闭或打开一条链或询问一个子矩阵内未关闭的权值和. 关键询问操作比较少, 可以枚举每条链, 暴力算出该条链对每个询问的贡献. 最后再按询问顺序切换链的状态, 只记录打开的链的贡献即可. #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #inclu…
例题来源: 题目: 1468: Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered from 1 to N for simplicity. We know exactly the position of every one (noted pos[i],pos[i] is positive integer and pos[i]<=10^8). The local authority want…
Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered from 1 to N for simplicity. We know exactly the position of every one (noted pos[i],pos[i] is positive integer and pos[i]<=10^8). The local authority wants to build a pos…
题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的…
当初听郭炜老师讲时不是很懂,几个月内每次复习树状数组必看的题 树的dfs序映射在树状数组上进行单点修改,区间查询. /* 树状数组: lowbit[i] = i&-i C[i] = a[i-lowbit[i]+1]+...+a[i] 求和: 设sum[k] = a[1]+a[2]+...+a[k] 则a[i]+a[i+1]+...+a[j] = sum[j]-sum[i-1] 在树状数组上:sum[k] = C[n1]+C[n2]+...+C[k] n1 = n2-lowbit[n2]... &g…
题目链接 http://poj.org/problem?id=2299 题意 给出一个序列 求出 这个序列要排成有序序列 至少要经过多少次交换 思路 求逆序对的过程 但是因为数据范围比较大 到 999999999 但是 给的 n 的数量又比较少 所以 离散化一下就可以了 比如 给出的 9 1 0 5 4 原始ID 0 1 2 3 4 排序后 0 1 4 5 9 原始ID 2 1 4 3 0 然后就可以发现 求 9 1 0 5 4 的 所有逆序对个数 实际和 求 2 1 4 3 0 的逆序对个数…
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i]-sum[j]\ge0) \] \(O(n^2)\)过不了,考虑优化 移项得: \[ f[i]=\sum f[j]\;(j< i,sum[i]\ge sum[j]) \] 这时候我们发现相当于求在\(i\)前面并且前缀和小于\(sum[i]\)的所有和,这就可以用一个树状数组优化了,在树状数组维护下标为…
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test 3.5 secondsmemory limit per test 256 megabytes 问题描述 Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to prese…
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和Xor[i],表示1~i的xor和.因为num^num=0,所以Xor[r] ^ Xor[l - 1]求的是l~r之间出现奇数次的数字xor和. 那怎么求偶数次的呢,那我们可以先求l到r之间不重复出现数字的xor(比如1 1 2 求的是1 ^ 2),然后再xor以上求出的Xor[r] ^ Xor[l…
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的性质,在一个区间中,我们如果把所有数字都异或的话,可以发现最后偶数次的数字异或后都变成了0,只剩下了奇数次的数字异或. 举个例子,{1,2,3,2,3,5} 异或和是1^2^3^2^3^5=1^5 因为最后要计算偶数次数字的异或和,那么最后我们只需要再异或上该区间内所有不同数字即可. 那么我们可以先…
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her wit…
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数异或起来就是答案,那么出现偶数次的数就可以 先求出区间 l,r 内有多少不同的数,将这些数异或起来,再异或上区间内出现奇数次的数的异或和就是答案.(出现偶数次的数异或后为0,奇数次的数异或后是本身   然后离线处理询问,对询问按右端点 sort,因为树状数组保存的是数出现的最后位置.离线处理询问后便…
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有出现偶数次的数异或的值. 思路:容易想到,把区间内的所有的数都异或得到的是出现奇数次的数的值,然后再异或该区间内的所有出现过的数(每个数只统计一次),得到的ans了. 第一个问题:得到询问区间的所有数的异或值,由 a[1~r] ^ a[0~(l-1)] = a[l~r] 可以用数组all_xor[i…