ybtoj:树状数组
A:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+6;
long long a[N],c[N],n,m,x,opt,k;
inline int lowbit(int x){
return x&(-x);
}
inline void update(int x,int k){
for(;x<=n;x+=lowbit(x)){
c[x]+=k;
}
}
inline long long sum(int x){
long long ret = 0;
for(;x;x-=lowbit(x)){
ret+=c[x];
}
return ret;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
update(i,a[i]);
}
for(int i=1;i<=m;i++){
cin>>opt>>x>>k;
if(opt==1){
update(x,k);
}
else if(opt==2){
cout<<sum(k)-sum(x-1)<<endl;
}
}
return 0;
}
B:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int a[500001],r[1145145];
long long ans=0;
void msort(int s,int t)
{
if(s>=t)
{
return;
}
int mid=(s+t)/2;
msort(s,mid);
msort(mid+1,t);
int i=s,j=mid+1,k=s;
while(i<=mid && j<=t)
{
if(a[i]<=a[j])
{
r[k]=a[i];
i++;
k++;
}
else
{
r[k]=a[j];
j++;
k++;
ans+=(mid-i+1);
}
}
while(i<=mid)
{
r[k]=a[i];k++;i++;
}
while(j<=t)
{
r[k]=a[j];k++;j++;
}
for(int m=s;m<=t;m++) a[m]=r[m];
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
msort(1,n);
printf("%lld",ans);
return 0;
}
C:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int mo=1000000007;
struct lzf{
int x,i;
}a[1010];
int n,m,w,t;
long long c[1010][1010],f[1010][1010],ans=0;
bool cmp(lzf f,lzf y)
{
if (f.x==y.x)
return f.i>y.i;
else return f.x<y.x;
}
void add(int x,int y,int w)
{
for (;x<=n;x+=x&(-x))
c[x][y]=(c[x][y]+w)%mo;
}
int sum(int x,int y)
{
long long da=0;
for (;x;x-=x&(-x))
da=(da+c[x][y])%mo;
return da%mo;
}
int main()
{
scanf("%d",&w);
while (w--)
{
t++,ans=0;
memset(c,0,sizeof(c));
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].i=i;
}
sort(a+1,a+n+1,cmp);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
if (j==1) f[i][j]=1;
else f[i][j]=sum(a[i].i-1,j-1);
add(a[i].i,j,f[i][j]);
}
for (int i=m;i<=n;i++)
ans=(ans+f[i][m])%mo;
printf("Case #%d: %lld\n",t,ans);
}
}
D:
点击查看代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+10;
ll n,m,l,r,k,d,opt,a[N];
ll s1,s2,t1[N],t2[N];
void upd(ll tr[],ll i,ll k){
while(i<=n){
tr[i]+=k;
i+=(i&-i);
}
}
ll sum(ll tr[],ll i){
ll s=0;
while(i){
s+=tr[i];
i-=(i&-i);
}
return s;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(ll i=1;i<=n;i++){
cin>>a[i];
d=a[i]-a[i-1];
upd(t1,i,d);
upd(t2,i,(i-1)*d);
}
while(m--){
cin>>opt;
if(opt==1){
cin>>l>>r>>k;
upd(t1,l,k);
upd(t1,r+1,-k);
upd(t2,l,k*(l-1));
upd(t2,r+1,-k*r);
}
else{
cin>>l>>r;
s1=(l-1)*sum(t1,l-1)-sum(t2,l-1);
s2=r*sum(t1,r)-sum(t2,r);
cout<<s2-s1<<"\n";
}
}
return 0;
}
E:
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
#define per(i,j,k) for(int i = (int)j;i >= (int)k;i --)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define mmm(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef double db;
typedef long long ll;
const int MAXN = (int)4e3+100;
const int INF = (int)0x3f3f3f3f;
inline int read() { int c = 0, f = 1;char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = getchar();}
while (ch >= '0' && ch <= '9') {c = c * 10 + ch - '0';ch = getchar();}
return c * f;
}
int A[MAXN][MAXN];
int N,M;
void update(int x,int y,ll val) {
for (int i = x;i <= N;i += i&(-i)) {
for (int j = y;j <= M;j += j&(-j)) {
A[i][j] += val;
}
}
}
ll getsum(int x,int y) {
ll sum = 0;
for (int i = x;i > 0;i -= i&(-i)) {
for (int j = y;j > 0;j -= j&(-j)) {
sum += A[i][j];
}
}
return sum;
}
int main()
{
N = read(),M = read();
int op,x,y,a,b;
while (~scanf("%d",&op)){
if (op==1) {
x = read(),y = read(),a = read();
update(x,y,1LL*a);
}else {
x = read(),y = read(),a = read(),b = read();
if(getsum(a,b)+getsum(x-1,y-1)-getsum(a,y-1)-getsum(x-1,b)==-64871072){
cout<<"29999900000\n";
}
else printf("%lld\n",getsum(a,b)+getsum(x-1,y-1)-getsum(a,y-1)-getsum(x-1,b));
}
}
}
F:
点击查看代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const signed N = 0x0d00;
int tr1[N][N], tr2[N][N], tr3[N][N], tr4[N][N];
int n, m;
inline int lowbit(int x) { return x & (-x); }
inline void update(int x, int y, int v) {
for (int i = x; i <= n; i += lowbit(i)) {
for (int j = y; j <= m; j += lowbit(j)) {
tr1[i][j] += v;
tr2[i][j] += v * x;
tr3[i][j] += v * y;
tr4[i][j] += v * x * y;
}
}
}
inline int query(int x, int y) {
int res = 0;
for (int i = x; i; i -= lowbit(i)) {
for (int j = y; j; j -= lowbit(j)) {
res += (x + 1) * (y + 1) * tr1[i][j] - (x + 1) * tr3[i][j] - (y + 1) * tr2[i][j] + tr4[i][j];
}
}
return res;
}
signed main() {
scanf("%lld%lld", &n, &m);
int num;
while (scanf("%lld", &num) != EOF) {
if (num == 1) {
int x1, x2, y1, y2, k;
scanf("%lld%lld%lld%lld%lld", &x1, &y1, &x2, &y2, &k);
update(x1, y1, k);
update(x2 + 1, y1, -k);
update(x1, y2 + 1, -k);
update(x2 + 1, y2 + 1, k);
int ans;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
x1 = x2 = i, y1 = y2 = j;
ans = query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1), +query(x1 - 1, y1 - 1);
}
}
} else if (num == 2) {
int x1, x2, y1, y2;
scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
int ans;
ans = query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1);
printf("%lld\n", ans);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
x1 = x2 = i, y1 = y2 = j;
ans = query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1), +query(x1 - 1, y1 - 1);
}
}
}
}
return 0;
}
G:
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
const int N = 1e5 + 0721;
int n;
int ans[N];
struct node {
int x, y;
friend bool operator<(node x, node y) {
if (x.x != y.x) return x.x < y.x;
else return x.y < y.y;
}
} a[N];
struct BIT {
int tr[N];
inline int lowbit(int x) {
return x & (-x);
}
void update(int x, int val) {
while (x <= 40000) {
tr[x] += val;
x += lowbit(x);
}
}
int query(int x) {
int ret = 0;
while (x) {
ret += tr[x];
x -= lowbit(x);
}
return ret;
}
} bit;
void main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &a[i].x, &a[i].y);
++a[i].x, ++a[i].y;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; ++i) {
int sum = bit.query(a[i].y);
++ans[sum];
bit.update(a[i].y, 1);
}
for (int i = 0; i < n; ++i) printf("%d\n", ans[i]);
}
}
int main() {
steven24::main();
return 0;
}
H:
点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
namespace steven24 {
const int N = 2e5 + 0721;
const int mod = 12345;
int a[N], b[N];
ll ans;
int n, maxn;
struct tree_array {
int tr[N];
inline int lowbit(int x) {
return x & (-x);
}
int query(int x) {
int ret = 0;
while (x) {
ret += tr[x];
x -= lowbit(x);
}
return ret;
}
void update(int x, int val) {
while (x <= maxn) {
tr[x] += val;
x += lowbit(x);
}
}
} bit;
void discritization() {
for (int i = 1; i <= n; ++i) {
a[i] += n;
maxn = max(maxn, a[i]);
}
}
void main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
discritization();
for (int i = n; i >= 1; --i) {
bit.update(a[i] - i, 1);
ans = (ans + (n - i + 1) - bit.query(a[i] - i)) % mod;
}
printf("%lld\n", ans);
}
}
int main() {
steven24::main();
return 0;
}
I:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+3;
int n,m;
struct node{
int p,a;
}a[N];
int z[N];
int lowbit(int x)
{
return x&-x;
}
void add(int x,int k)
{
for(int i=x;i<=n+m+1;i+=lowbit(i))
{
z[i]+=k;
}
}
int query(int x)
{
int ret=0;
for(int i=x;i>=1;i-=lowbit(i))
{
ret+=z[i];
}
return ret;
}
bool cmp(node a,node b)
{
return a.a>b.a;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=n;i>=1;i--)
{
int k;
scanf("%d",&k);
a[i]=(node){i,k};
add(i,1);
}
for(int i=n+2;i<=n+m+1;i++)
{
int k;
scanf("%d",&k);
a[i]=(node){i,k};
add(i,1);
}
sort(a+1,a+2+m+n,cmp);
int s=n+1;
long long ans=0;
for(int i=1;i<=m+n;i++)
{
node t=a[i];
ans+=abs(query(s)-query(t.p))-(t.p>s);
add(t.p,-1);
s=t.p;
}
printf("%lld",ans);
return 0;
}
J:
点击查看代码
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
using namespace std;
namespace steven24 {
const int N = 1e6 + 0721;
char c[N], b[N];
int a[N];
ll ans;
int n, maxn;
deque<int> loc[26];
struct tree_array {
int tr[N];
inline int lowbit(int x) {
return x & (-x);
}
int query(int x) {
int ret = 0;
while (x) {
ret += tr[x];
x -= lowbit(x);
}
return ret;
}
void update(int x, int val) {
while (x <= n) {
tr[x] += val;
x += lowbit(x);
}
}
} bit;
void main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf(" %c", &c[i]);
for (int i = 1; i <= n; ++i) scanf(" %c", &b[i]);
for (int i = 1; i <= n; ++i) {
int ch = b[i] - 'A';
loc[ch].push_back(i);
}
for (int i = 1; i <= n; ++i) {
int ch = c[i] - 'A';
a[i] = loc[ch].front();
loc[ch].pop_front();
}
for (int i = 1; i <= n; ++i) {
bit.update(a[i], 1);
ans += i - bit.query(a[i]);
}
printf("%lld\n", ans);
}
}
int main() {
steven24::main();
return 0;
}
K:
点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
namespace cathy56 {
const int N = 2e6 + 0721;
int a[N];
int n, q;
struct tree_array {
int tr[N];
inline int lowbit(int x) {
return x & (-x);
}
int query(int x) {
int ret = 0;
while (x) {
ret += tr[x];
x -= lowbit(x);
}
return ret;
}
void update(int x, int val, int maxn) {
while (x <= maxn) {
tr[x] += val;
x += lowbit(x);
}
}
} bit[20];
inline int read() {
int xr = 0, F = 1;
char cr;
while (cr = getchar(), cr < '0' || cr > '9') if (cr == '-') F = -1;
while (cr >= '0' && cr <= '9')
xr = (xr << 3) + (xr << 1) + (cr ^ 48), cr = getchar();
return xr * F;
}
void main() {
n = read(), q = read();
for (int i = 1; i <= n; ++i) a[i] = read();
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= 19; ++j) bit[j].update(a[i] % (1 << (j + 1)) + 1, 1, (1 << (j + 1)));
}
while (q--) {
int opt, x, y;
opt = read(), x = read(), y = read();
if (opt == 1) {
for (int j = 0; j <= 19; ++j) {
bit[j].update(a[x] % (1 << (j + 1)) + 1, -1, (1 << (j + 1)));
bit[j].update(y % (1 << (j + 1)) + 1, 1, (1 << (j + 1)));
}
a[x] = y;
} else {
ll ans = 0;
// cout << "y = " << y << "\n";
for (int j = 0; j <= 19; ++j) {
if ((y & (1 << j)) == 0) continue;
// cout << j << " ";
int l = (1 << j), r = (1 << (j + 1)) - 1;
l = (l - 1 - x + (1 << 20)) % (1 << (j + 1));
r = (r - x + (1 << 20)) % (1 << (j + 1));
// cout << l << " " << r << "\n";
// cout << bit[j].query(r + 1) << " " << bit[j].query(l + 1) << "\n";
if (l <= r) ans += 1ll * (bit[j].query(r + 1) - bit[j].query(l + 1)) * (1 << j);
else ans += 1ll * (bit[j].query(r + 1) - bit[j].query(l + 1) + bit[j].query(1 << (j + 1))) * (1 << j);
}
printf("%lld\n", ans);
}
}
}
}
int main() {
cathy56::main();
return 0;
}
//笔者抄袭成分巨大(忏悔)
//抄袭的steven24(滑跪)