Merge pull request #16 from Kiritow/master

Catch up with master
This commit is contained in:
KiritoTRw 2016-08-11 11:23:09 +08:00 committed by GitHub
commit ab3d830a64
12 changed files with 664 additions and 0 deletions

View File

@ -0,0 +1,12 @@
///约瑟夫问题,n个人,查m个数
int JosephusProblem_Solution4(int n, int m)
{
if(n < 1 || m < 1)
return -1;
vector<int> f(n+1,0);
for(unsigned i = 2; i <= n; i++)
f[i] = (f[i-1] + m) % i;
return f[n];
}

View File

@ -0,0 +1,57 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <functional>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 10010;
struct node
{
int lt,rt,v;
};
node tree[maxn<<2];
/** ========== Conditional =========== */
using ValueType = int;
inline ValueType algo_delegate(ValueType a,ValueType b)
{
return min(a,b);
}
/****************************************/
void build(int lt,int rt,int v)
{
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt)
{
scanf("%d",&tree[v].v);
return;
}
int mid = (lt + rt)>>1;
build(lt,mid,v<<1);
build(mid+1,rt,v<<1|1);
tree[v].v = algo_delegate(tree[v<<1].v,tree[v<<1|1].v);
}
void update(int p,int val,int v)
{
if(tree[v].lt == tree[v].rt)
{
tree[v].v = val;
return;
}
if(p <= tree[v<<1].rt) update(p,val,v<<1);
if(p >= tree[v<<1|1].lt) update(p,val,v<<1|1);
tree[v].v = algo_delegate(tree[v<<1].v,tree[v<<1|1].v);
}
int query(int lt,int rt,int v)
{
if(tree[v].lt >= lt && tree[v].rt <= rt)
return tree[v].v;
int a = INF,b = INF;
if(lt <= tree[v<<1].rt) a = query(lt,rt,v<<1);
if(rt >= tree[v<<1|1].lt) b = query(lt,rt,v<<1|1);
return algo_delegate(a,b);
}

View File

@ -1 +1,2 @@
#Tools
Some tools will be included here to help setup a develop platform on a new computer.

72
HDOJ/1698_sr_19930829.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=100002;
int col[maxn<<2];//用来标记每个节点为0则表示没有标记否则为标记以颜色的价值作为标记。
int sum[maxn<<2];//求和
void PushUp(int rt)//向上更新和
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int m)//对某一个区间进行改变,如果被标记了,在查询的时候就得把改变传给子节点,因为查询的并不一定是当前区间
{//m为区间长度
if(col[rt])//已经标记过,该区间被改变过
{
col[rt<<1]=col[rt<<1|1]=col[rt];//标记子节点
sum[rt<<1]=(m-(m>>1))*col[rt];//更新左儿子的和
sum[rt<<1|1]=(m>>1)*col[rt];//更新右儿子的和
col[rt]=0;
}
}
void build(int l,int r,int rt)
{
col[rt]=0;//初始化为所有节点均未标记
sum[rt]=1;//初始值为1
if(l==r)
return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]=c;
sum[rt]=c*(r-l+1);//更新代表某个区间的节点和,该节点不一定是叶子节点
return ;
}
PushDown(rt,r-l+1);//向下传递
int m=(l+r)>>1;
if(L<=m)
update(L,R,c,lson);//更新左儿子
if(R>m)
update(L,R,c,rson);//更新右儿子
PushUp(rt);//向上传递更新和
}
int main()
{
int k;cin>>k;
int n,c,add,a,b;
for(int i=1;i<=k;i++)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&c);
for(int j=1;j<=c;j++)
{
scanf("%d%d%d",&a,&b,&add);
update(a,b,add,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",i,sum[1]);
}
return 0;
}

55
POJ/2566_wiking__acm.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 5;
const int INF = 2000000000;
typedef pair<int, int> P;
typedef long long LL;
P p[maxn];
int Abs(int x){return x<0?-x:x;}
int n;
void query(int tar){
int s = 0,e = 1,Min = INF;
int ansl,ansr,ansx;
while(s <= n && e <= n){
int tem = p[e].first-p[s].first;
if(Abs(tem-tar) < Min){
Min = Abs(tem-tar);
ansx = tem;
ansl = p[s].second;
ansr = p[e].second;
}
if(tem > tar) s++;
else if(tem < tar) e++;
else break;
if(s == e) e++;
}
if(ansl > ansr) swap(ansl,ansr);
printf("%d %d %d\n",ansx,ansl+1,ansr);
}
int main(){
int m;
while(scanf("%d%d",&n,&m)){
if(n == 0 && m == 0) break;
p[0] = P(0,0);
int sum = 0;
for(int i = 1;i <= n;i++){
int tem;
scanf("%d",&tem);
sum += tem;
p[i] = P(sum,i);
}
sort(p,p+n+1);
while(m--){
int x;
scanf("%d",&x);
query(x);
}
}
return 0;
}

68
hihoCoder/1068.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1000005;
int n,ndev;
int a[MAXN];
const int MAXDEV = 25;
int minsum[MAXN][MAXDEV];
void RMQ() //预处理->O(nlogn)
{
for(int j = 1; j <= ndev; ++j)
for(int i = 1; i <= n; ++i)
if(i + (1 << j) - 1 <= n)
{
//maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);
minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
}
}
int RMQ(int i,int j)
{
int k=log2( j - i + 1);
//return max(maxsum[i ][ k], maxsum[ j - 2 ^ k + 1][ k]);
return min(minsum[i ][ k], minsum[ j - (1<<k) + 1][ k]);
}
void Init()
{
for(int i=1;i<=n;i++)
{
minsum[i][0]=a[i];
}
}
int main()
{
scanf("%d",&n);
ndev=0;
int p=n;
if(p&1) p++;
while(p>1)
{
p>>=1;
ndev++;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
Init();
RMQ();
int t;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
int L,R;
scanf("%d %d",&L,&R);
int ans=RMQ(L,R);
printf("%d\n",ans);
}
return 0;
}

View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
using namespace std;
const int N = 10e6+10;
const int M = 30;
int arrayData[N];
int rmqData[N][M];
int n,m;
int min(int x, int y){
return (x <y ? x : y);
}
void RMQ(){
int i, j;
for (i = 0; i < n; i++){
rmqData[i][0] = arrayData[i];
}
for (j = 1; j <=m; j++){
for (i = 0; i+(1 << j)-1 <n; i++){
rmqData[i][j] = min(rmqData[i][j - 1], rmqData[i + (1<<(j - 1))][j - 1]);
}
}
}
int main(){
scanf("%d", &n);
int i, j;
for (i = 0; i < n; i++){
scanf("%d", &arrayData[i]);
}
m = (log(n*1.0) / log(2.0));
RMQ();
int q;
scanf("%d", &q);
for (i = 0; i < q; i++){
int l, r;
scanf("%d%d", &l, &r);
l = l - 1;
r = r - 1;
int k=0;
while ((1 << (k + 1)) <= (r - l + 1)) k++;
printf("%d\n", min(rmqData[l][k], rmqData[r-(1<<k)+1][k]));
}
return 0;
}

View File

@ -0,0 +1,75 @@
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<string>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const int maxn = 1e6 + 5;
const int max_pos = 20 ;
struct Q{ int l,r; } quary[maxn];
int N,Q;
int weight[maxn];
int rmq[maxn][max_pos];
void init(){
memset(rmq,0x5f,sizeof(rmq));
}
void input(){
scanf("%d",&N);
for(int i=1;i<=N;i++) scanf("%d",&weight[i]);
scanf("%d",&Q);
for(int i=1;i<=Q;i++) scanf("%d%d",&quary[i].l,&quary[i].r);
}
/*
int get_mi(int x){
int ret=0;
for(int i=1;i<=x;i*=2) ret++;
return ret-1;
}
*/
void RMQ_ST(){
for(int i=1;i<=N;i++) rmq[i][0]=weight[i];
//int l=get_mi(N);
int l=(int)((log(N))/(log(2.0)));
for(int j=1;j<=l;j++){
for(int i=1;i+(1<<j)-1<=N;i++){
rmq[i][j]=min(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
}
}
}
void solve(){
RMQ_ST();
for(int i=1;i<=Q;i++){
int l=quary[i].l;
int r=quary[i].r;
//int mi=get_mi(v-u+1);
int mi=(int)((log(r-l+1))/(log(2.0)));
printf("%d\n",min(rmq[l][mi],rmq[r-(1<<mi)+1][mi]));
}
}
int main(){
init();
input();
solve();
return 0;
}

74
hihoCoder/1070.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <functional>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 10010;
struct node
{
int lt,rt,v;
};
node tree[maxn<<2];
/** ========== Conditional =========== */
using ValueType = int;
inline ValueType algo_delegate(ValueType a,ValueType b)
{
return min(a,b);
}
/****************************************/
void build(int lt,int rt,int v)
{
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt)
{
scanf("%d",&tree[v].v);
return;
}
int mid = (lt + rt)>>1;
build(lt,mid,v<<1);
build(mid+1,rt,v<<1|1);
tree[v].v = algo_delegate(tree[v<<1].v,tree[v<<1|1].v);
}
void update(int p,int val,int v)
{
if(tree[v].lt == tree[v].rt)
{
tree[v].v = val;
return;
}
if(p <= tree[v<<1].rt) update(p,val,v<<1);
if(p >= tree[v<<1|1].lt) update(p,val,v<<1|1);
tree[v].v = algo_delegate(tree[v<<1].v,tree[v<<1|1].v);
}
int query(int lt,int rt,int v)
{
if(tree[v].lt >= lt && tree[v].rt <= rt)
return tree[v].v;
int a = INF,b = INF;
if(lt <= tree[v<<1].rt) a = query(lt,rt,v<<1);
if(rt >= tree[v<<1|1].lt) b = query(lt,rt,v<<1|1);
return algo_delegate(a,b);
}
int main()
{
int n,m,op,u,v;
while(~scanf("%d",&n))
{
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%d %d %d",&op,&u,&v);
if(op) update(u,v,1);
else printf("%d\n",query(u,v,1));
}
}
return 0;
}

View File

@ -0,0 +1,63 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 10010;
struct node{
int lt,rt,minv;
};
node tree[maxn<<2];
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt){
scanf("%d",&tree[v].minv);
return;
}
int mid = (lt + rt)>>1;
build(lt,mid,v<<1);
build(mid+1,rt,v<<1|1);
tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv);
}
void update(int p,int val,int v){
if(tree[v].lt == tree[v].rt){
tree[v].minv = val;
return;
}
if(p <= tree[v<<1].rt) update(p,val,v<<1);
if(p >= tree[v<<1|1].lt) update(p,val,v<<1|1);
tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv);
}
int query(int lt,int rt,int v){
if(tree[v].lt >= lt && tree[v].rt <= rt)
return tree[v].minv;
int a = INF,b = INF;
if(lt <= tree[v<<1].rt) a = query(lt,rt,v<<1);
if(rt >= tree[v<<1|1].lt) b = query(lt,rt,v<<1|1);
return min(a,b);
}
int main() {
int n,m,op,u,v;
while(~scanf("%d",&n)){
build(1,n,1);
scanf("%d",&m);
while(m--){
scanf("%d %d %d",&op,&u,&v);
if(op) update(u,v,1);
else printf("%d\n",query(u,v,1));
}
}
return 0;
}

74
hihoCoder/1077.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <functional>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000010;
struct node
{
int lt,rt,v;
};
node tree[maxn<<2];
/** ========== Conditional =========== */
using ValueType = int;
inline ValueType algo_delegate(ValueType a,ValueType b)
{
return min(a,b);
}
/****************************************/
void build(int lt,int rt,int v)
{
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt)
{
scanf("%d",&tree[v].v);
return;
}
int mid = (lt + rt)>>1;
build(lt,mid,v<<1);
build(mid+1,rt,v<<1|1);
tree[v].v = algo_delegate(tree[v<<1].v,tree[v<<1|1].v);
}
void update(int p,int val,int v)
{
if(tree[v].lt == tree[v].rt)
{
tree[v].v = val;
return;
}
if(p <= tree[v<<1].rt) update(p,val,v<<1);
if(p >= tree[v<<1|1].lt) update(p,val,v<<1|1);
tree[v].v = algo_delegate(tree[v<<1].v,tree[v<<1|1].v);
}
int query(int lt,int rt,int v)
{
if(tree[v].lt >= lt && tree[v].rt <= rt)
return tree[v].v;
int a = INF,b = INF;
if(lt <= tree[v<<1].rt) a = query(lt,rt,v<<1);
if(rt >= tree[v<<1|1].lt) b = query(lt,rt,v<<1|1);
return algo_delegate(a,b);
}
int main()
{
int n,m,op,u,v;
while(~scanf("%d",&n))
{
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%d %d %d",&op,&u,&v);
if(op) update(u,v,1);
else printf("%d\n",query(u,v,1));
}
}
return 0;
}

View File

@ -0,0 +1,63 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000010;
struct node {
int lt,rt,minv;
};
node tree[maxn<<2];
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt) {
scanf("%d",&tree[v].minv);
return;
}
int mid = (lt + rt)>>1;
build(lt,mid,v<<1);
build(mid+1,rt,v<<1|1);
tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv);
}
void update(int p,int val,int v) {
if(tree[v].lt == tree[v].rt) {
tree[v].minv = val;
return;
}
if(p <= tree[v<<1].rt) update(p,val,v<<1);
if(p >= tree[v<<1|1].lt) update(p,val,v<<1|1);
tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv);
}
int query(int lt,int rt,int v) {
if(tree[v].lt >= lt && tree[v].rt <= rt)
return tree[v].minv;
int a = INF,b = INF;
if(lt <= tree[v<<1].rt) a = query(lt,rt,v<<1);
if(rt >= tree[v<<1|1].lt) b = query(lt,rt,v<<1|1);
return min(a,b);
}
int main() {
int n,q,u,v,op;
while(~scanf("%d",&n)) {
build(1,n,1);
scanf("%d",&q);
while(q--) {
scanf("%d %d %d",&op,&u,&v);
if(op) update(u,v,1);
else printf("%d\n",query(u,v,1));
}
}
return 0;
}