1. 程式人生 > >No Program No Life

No Program No Life

記錄一個菜逼的成長。。

網上好多zkw線段樹版本都是錯的。。坑啊。
主要是連zkw的ppt上都是不完整和有錯誤的。統計的力量

結點資訊

struct Node{
  int sum,mx,mn;
}T[maxn<<2];
int M,a[maxn];

建樹

void build(int n)
{
  for( M = 1; M <= n+1; M <<= 1);
  for( int i = M + 1; i <= M + n; i++ )T[i].sum = T[i].mx = T[i].mn = a[i-M];
  for
( int i = M - 1; i; i-- ){ T[i].sum = T[i<<1].sum + T[i<<1|1].sum; //結點記錄的是相對的值 T[i].mx = max(T[i<<1].mx,T[i<<1|1].mx); T[i<<1].mx -= T[i].mx;T[i<<1|1].mx -= T[i].mx; T[i].mn = min(T[i<<1].mn,T[i<<1|1].mn); T[i<<1].mn -= T
[i].mn;T[i<<1|1].mn -= T[i].mn; } }

和的單點更新

for( T[x+=M].sum += v,x >>= 1; x; x >>= 1){
  T[x].sum = T[x<<1].sum + T[x<<1|1].sum;
}

最小(大)值的區間更新

區間更新也可以時單點更新(左端點等於右端點時

int s,t;
// 只更新到當s,t是兄弟結點時
for( s = x+M-1,t = x+M+1; s^t^1; s>>=1,t>>=1 ){
  if
(~s&1)T[s^1].mx += v,T[s^1].mn += v; if( t&1)T[t^1].mx += v,T[t^1].mn += v; A = max(T[s].mx,T[s^1].mx);T[s].mx -= A;T[s^1].mx -= A;T[s>>1].mx += A; A = max(T[t].mx,T[t^1].mx);T[t].mx -= A;T[t^1].mx -= A;T[t>>1].mx += A; A = min(T[s].mn,T[s^1].mn);T[s].mn -= A;T[s^1].mn -= A;T[s>>1].mn += A; A = min(T[t].mn,T[t^1].mn);T[t].mn -= A;T[t^1].mn -= A;T[t>>1].mn += A; } // 更新到根 while(s>1){ A = max(T[s].mx,T[s^1].mx),T[s].mx -= A,T[s^1].mx -= A,T[s>>1].mx += A; A = min(T[s].mn,T[s^1].mn),T[s].mn -= A,T[s^1].mn -= A,T[s>>=1].mn += A; }

和的區間查詢

int querySum(int l,int r,int res = 0)//查詢閉區間[l,r]
{
  // 看成開區間(l-1,r+1)
  for( l = l + M - 1,r = r + M + 1; l^r^1; l >>= 1,r >>= 1 ){
    if(~l&1)res += T[l^1].sum;
    if( r&1)res += T[r^1].sum;
  }
  return res;
}

最小(大)值的區間查詢

int queryMax(int l,int r,int lmx = 0,int rmx = 0)// 查詢閉區間[l,r]
{
  // 這裡還是閉區間[l,r],跟和的查詢不同
  // 還要分是不是葉子結點
  l += M,r += M;
  int res ;
  if(l != r){
    for( ; l^r^1; l >>= 1,r >>= 1 ){
      lmx += T[l].mx;rmx += T[r].mx;
      if(~l&1)lmx = max(lmx,T[l^1].mx);
      if( r&1)rmx = max(rmx,T[r^1].mx); 
    }
  }
  res = max(lmx+T[l].mx,rmx+T[r].mx);
  while(l > 1)res += T[l>>=1].mx;
  return res;
}
int queryMin(int l,int r,int lmn = 0,int rmn = 0)
{
  l += M,r += M;
  int res = 0;
  if(l != r){
    for( ; l^r^1; l >>= 1,r >>= 1 ){
      lmn += T[l].mn;rmn += T[r].mn;
      if(~l&1)lmn = min(lmn,T[l^1].mn);
      if( r&1)rmn = min(rmn,T[r^1].mn); 
    }
  }
  res = min(lmn+T[l].mn,rmn+T[r].mn);
  while(l > 1)res += T[l>>=1].mn;
  return res;
}

完整模板

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
struct Node{
  int sum,mx,mn;
}T[maxn<<2];
int M,a[maxn];
void build(int n)
{
  for( M = 1; M <= n+1; M <<= 1);
  for( int i = M + 1; i <= M + n; i++ )T[i].sum = T[i].mx = T[i].mn = a[i-M];
  for( int i = M - 1; i; i-- ){
    T[i].sum = T[i<<1].sum + T[i<<1|1].sum;

    T[i].mx = max(T[i<<1].mx,T[i<<1|1].mx);
    T[i<<1].mx -= T[i].mx;T[i<<1|1].mx -= T[i].mx;

    T[i].mn = min(T[i<<1].mn,T[i<<1|1].mn);
    T[i<<1].mn -= T[i].mn;T[i<<1|1].mn -= T[i].mn;
  }
}
void update(int x,int v,int A = 0)
{
  int s,t;
  for( s = x+M-1,t = x+M+1; s^t^1; s>>=1,t>>=1 ){
    if(~s&1)T[s^1].mx += v,T[s^1].mn += v;
    if( t&1)T[t^1].mx += v,T[t^1].mn += v;
    A = max(T[s].mx,T[s^1].mx);T[s].mx -= A;T[s^1].mx -= A;T[s>>1].mx += A;
    A = max(T[t].mx,T[t^1].mx);T[t].mx -= A;T[t^1].mx -= A;T[t>>1].mx += A;

    A = min(T[s].mn,T[s^1].mn);T[s].mn -= A;T[s^1].mn -= A;T[s>>1].mn += A;
    A = min(T[t].mn,T[t^1].mn);T[t].mn -= A;T[t^1].mn -= A;T[t>>1].mn += A;
  }

  while(s>1){
    A = max(T[s].mx,T[s^1].mx),T[s].mx -= A,T[s^1].mx -= A,T[s>>1].mx += A;

    A = min(T[s].mn,T[s^1].mn),T[s].mn -= A,T[s^1].mn -= A,T[s>>=1].mn += A;
  }

  for( T[x+=M].sum += v,x >>= 1; x; x >>= 1){
    T[x].sum = T[x<<1].sum + T[x<<1|1].sum;
  }
}
int querySum(int l,int r,int res = 0)
{
  for( l = l + M - 1,r = r + M + 1; l^r^1; l >>= 1,r >>= 1 ){
    if(~l&1)res += T[l^1].sum;
    if( r&1)res += T[r^1].sum;
  }
  return res;
}
int queryMax(int l,int r,int lmx = 0,int rmx = 0)
{
  l += M,r += M;
  int res ;
  if(l != r){
    for( ; l^r^1; l >>= 1,r >>= 1 ){
      lmx += T[l].mx;rmx += T[r].mx;
      if(~l&1)lmx = max(lmx,T[l^1].mx);
      if( r&1)rmx = max(rmx,T[r^1].mx); 
    }
  }
  res = max(lmx+T[l].mx,rmx+T[r].mx);
  while(l > 1)res += T[l>>=1].mx;
  return res;
}
int queryMin(int l,int r,int lmn = 0,int rmn = 0)
{
  l += M,r += M;
  int res = 0;
  if(l != r){
    for( ; l^r^1; l >>= 1,r >>= 1 ){
      lmn += T[l].mn;rmn += T[r].mn;
      if(~l&1)lmn = min(lmn,T[l^1].mn);
      if( r&1)rmn = min(rmn,T[r^1].mn); 
    }
  }
  res = min(lmn+T[l].mn,rmn+T[r].mn);
  while(l > 1)res += T[l>>=1].mn;
  return res;
}
int main()
{

  return 0;
}

相關推薦

No Program No Life

記錄一個菜逼的成長。。 網上好多zkw線段樹版本都是錯的。。坑啊。 主要是連zkw的ppt上都是不完整和有錯誤的。統計的力量 結點資訊 struct Node{ int sum,mx,mn; }T[maxn<<2]; in

于丹丹 no zuo no die no life

        今天和同學聊天,她像我描述了她最近的情況  ,一個字形容那就是"累"     ---------身體累,心累,總之是各種累!!!                        

no zuo no die

col clu adp cout style phi std +++ code 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using na

那些年不上運維自動化 NO ZUO NO DIE 寫下的關機腳本

stat alias success 17.1 power amp p12 ucc ESS #!/bin/sh#author:jerry#create date 2015-12#last update: 2018-8-27decide() {[ $1 == 0 ] &

hdu 4630 No Pain No Game (線段樹+離線)

uil code pri 我們 i++ style src sqrt cst 題目大意:給你一個無序的1~n的排列a,每次詢問[l,r]之間任取兩個數得到的最大gcd是多少 先對所有詢問離線,然後把問題掛在區間的左端點上(右端點也行) 在預處理完質數,再處理一個next數組

NOIP2018模擬賽 HDU 4630 No Pain No Game 2018 10 9 T1

難度:NOIP+ 演算法:離線+樹狀陣列 簡述題意: 1.1 題目描述 給定一個長度為n 的排列a1; a2; a3; :::; an, 現在有Q 個詢問,每次詢 問區間[l,r] 內任意選取兩個不同的數字能夠得到的gcd 的最大值。 1.2 輸入 一行兩個整數n;Q 接下來一行是

hdu 4630 No Pain No Game

老師說你們做題太少了,這都是套路題。。考了居然都不會。。。 面壁ing。。。 我回來了。。。 好吧它確實是套路題。。。題目要求給一段1-n的序列。。然後每次詢問l-r區間中最大的gcd。。。詢問1e5,序列1e5.。 自然需要log級的。。根號也可以。畢竟1e5的根號

Ubuntu 16.04 No launcher, no panel, unity 的解決方案

第一種解決思路 第二種解決思路 Launch a tty terminal by pressing ctrl + alt + f1. Once in that terminal, run the following set of commands in the

No Mercy / No Malice

Last week I received several calls from media asking for comments on what has become the Mecca of innovation, Apple’s Special Events, where they announce t

Ansible and the AWS CLI: No module, no problem

Ansible and the AWS CLI: No module, no problemTips on integrating the AWS CLI when Ansible modules are letting you downIf you have picked up Ansible as a t

no pains,no gains

/* 題意: 寬為L的河,有n塊石頭,青蛙可以通過石頭跳到 河對岸去,最多跳m次,問青蛙每次最少跳多遠 思路: 假設河的兩岸都是石頭,一共跳m次,一共有m+1塊石頭被用到 那麼我們就可以轉化為在n個石頭中挑出m+1個石頭來解 */ #include <cstdio> #inclu

You can you up,No can no bb !

1、在Windows 系統下,很多軟體安裝都需要配置環境變數,比如 安裝 jdk ,如果不配置環境變數,在非軟體安裝的目錄下執行javac 命令,將會報告找不到檔案,類似的錯誤。 2、那麼什麼是環境變數?簡單說,就是指定一個目錄,執行軟體的時候,相關的程式將會

no pain no gain no Gavin

專案管理的軟體真的很多,基於SaaS的,基於Proprietary的,當然還有Open Source的,如下連結有完整的比較: http://en.wikipedia.org/wiki/Comparison_of_project_management_software ht

No errors,no gains!

1,什麼是Web應用程式   Web應用程式是可以通過Web進行訪問的應用程式,最大的好處是訪問非常容易。比如淘寶、新浪、網易等入口網站   軟體開發領域的三大方向:桌面應用程式(C/S架構

Linux下運行Java項目時,出現No X11 DISPLAY variable was set, but this program performed an operation which requires it.的問題解決

per 出現 cnblogs -m performed program this exp code 在~/.bashrc環境變量文件最下方加入: export DISPLAY=:0.0 然後,刷新環境變量以使其生效: source ~/.bashrc

錯誤 'Cannot run program "/home/uv/IDE/adt/sdk/platform-tools/adb": error=2, No such file or directory

sudo XP exce error run cto lin type pen 轉 Linux下Android SDK中adb找不到的解決方案 2013年04月22日 20:41:48 閱讀數:7621 在Linux平臺下配置Android SDK開發環境過程中

error: cannot spawn D:\Program Files (86)\Git\usr\bin\ssh.exe: No such file or directory

發現問題 升級git之後,提交程式碼報瞭如下的錯誤。 git.exe pull --progress -v --no-rebase “origin” master error: cannot spawn D:\Program Files (86)\Git\usr\bin\

完美解決 No IDEA annotations attached to the JDK 1.8 (C:\Program Files\Android\Android Studio\jre)

問題背景 今天上午開啟AS後突然發現所有xml介面無法預覽,開啟class檔案也發現從頭到尾全是錯誤,在class檔案介面上方提示No IDEA annotations attached to the JDK 1.8 (C:\Program Files\Android\Android

成功解決FileNotFoundError: [Errno 2] No such file or directory: 'F:\\Program Files\\Python\\Python36\\li

  解決問題 FileNotFoundError: [Errno 2] No such file or directory: 'F:\\Program Files\\Python\\Python36\\lib\\site-packages\\chatterbot_corpus\

解決Jenkins執行sudo命令時出現“sudo: no tty present and no askpass program specified”的錯誤

在Jenkins的使用過程中,如果在指令碼中使用到sudo命令,有可能出現如下所示的錯誤: sudo: no tty present and no askpass program specified 這是因為Jenkins伺服器在執行sudo命令時的