Skip to content

Commit 934e054

Browse files
committed
add a few more solutions
1 parent 9481609 commit 934e054

File tree

154 files changed

+11177982
-430073
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+11177982
-430073
lines changed

hdoj/1166.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
#define lson l , m , idx << 1
6+
#define rson m + 1 , r , idx << 1 | 1
7+
8+
const int MAXN = 100000;
9+
10+
int data[MAXN*8];
11+
12+
void pushUp(int idx){
13+
data[idx] = data[idx*2] + data[idx*2+1];
14+
}
15+
16+
17+
void build(int l, int r, int idx){
18+
if(l==r){
19+
scanf("%d", &data[idx]);
20+
return ;
21+
}
22+
int m = (l+r)>>1;
23+
build(lson);
24+
build(rson);
25+
pushUp(idx);
26+
}
27+
28+
void update(int p, int value, int l, int r, int idx){
29+
if(p==l && p==r){
30+
data[idx] += value;
31+
return ;
32+
}
33+
int m = (l+r)>>1;
34+
if(p<=m){
35+
update(p, value, lson);
36+
}else{
37+
update(p, value, rson);
38+
}
39+
pushUp(idx);
40+
}
41+
42+
int query(int L, int R, int l, int r, int idx){
43+
if(L<=l && R>=r){
44+
return data[idx];
45+
}
46+
int m = (l+r)>>1;
47+
int ret = 0;
48+
if(L<=m) ret+= query(L,R,lson);
49+
if(R>m) ret+= query(L,R,rson);
50+
return ret;
51+
}
52+
53+
54+
int main(){
55+
int T , n;
56+
scanf("%d",&T);
57+
for (int cas = 1 ; cas <= T ; cas ++) {
58+
printf("Case %d:\n",cas);
59+
scanf("%d",&n);
60+
build(1 , n , 1);
61+
char op[10];
62+
while (scanf("%s",op)) {
63+
if (op[0] == 'E') break;
64+
int a , b;
65+
scanf("%d%d",&a,&b);
66+
if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));
67+
else if (op[0] == 'S') update(a , -b , 1 , n , 1);
68+
else update(a , b , 1 , n , 1);
69+
}
70+
}
71+
return 0;
72+
73+
74+
75+
return 0;
76+
}
77+

hdoj/1166.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1
2+
10
3+
1 2 3 4 5 6 7 8 9 10
4+
Query 1 3
5+
Add 3 6
6+
Query 2 7
7+
Sub 10 2
8+
Add 6 3
9+
Query 3 10
10+
End

hdoj/1394.cpp

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <iostream>
2+
#include <list>
3+
4+
using namespace std;
5+
6+
const int MAXN = 6000;
7+
const int INF = 999999999;
8+
int N, tree[MAXN*8];
9+
10+
//,data[MAXN];
11+
12+
list<int> data;
13+
14+
15+
void pushUp(int idx){
16+
tree[idx] = tree[idx*2]+tree[idx*2+1];
17+
}
18+
19+
void clear(){
20+
memset(tree,0,sizeof(tree));
21+
}
22+
23+
void build(int l, int r, int idx){
24+
if(l==r){
25+
tree[idx] = 0;
26+
return;
27+
}
28+
int m = (l+r)>>1;
29+
build(l,m,idx*2);
30+
build(m+1,r,idx*2+1);
31+
pushUp(idx);
32+
}
33+
34+
int query(int L,int R, int l, int r, int idx){
35+
if(L<=l && R>=r ){
36+
return tree[idx];
37+
}
38+
int m = (l+r)>>1;
39+
int ret1=0, ret2=0;
40+
if(L<=m){
41+
ret1 = query(L, R, l, m, idx*2);
42+
}
43+
if(R>=m+1){
44+
ret2 = query(L, R, m+1, r, idx*2+1);
45+
}
46+
return ret1+ret2;
47+
}
48+
49+
void update(int L, int R, int l, int r, int idx){
50+
if(L<=l && R>=r){
51+
tree[idx] += 1;
52+
return;
53+
}
54+
int m = (l+r)>>1;
55+
if(L<=m){
56+
update(L, R, l, m, idx*2);
57+
}
58+
if(R>m){
59+
update(L, R, m+1, r, idx*2+1);
60+
}
61+
pushUp(idx);
62+
}
63+
64+
65+
void work(){
66+
67+
int cnt_N = N;
68+
int ans = INF;
69+
build(1,N, 1);
70+
int sum = 0;
71+
list<int>::iterator it;
72+
for(it = data.begin(); it!=data.end(); it++){
73+
int t = query(*it+2, N, 1, N, 1);
74+
sum += t;
75+
update(*it+1, *it+1, 1, N, 1);
76+
}
77+
ans = sum;
78+
cnt_N-=1;
79+
while(cnt_N--){
80+
int ttt = 3;
81+
int front = data.front();
82+
front+=1;
83+
data.pop_front();
84+
data.push_back(front);
85+
int smaller = front-1;
86+
int larger = N-front;
87+
sum -= smaller;
88+
sum += larger;
89+
ans = min(sum, ans);
90+
}
91+
cout<<ans<<endl;
92+
93+
}
94+
95+
int main(){
96+
while(scanf("%d",&N)!=EOF){
97+
data.clear();
98+
int tmp;
99+
for(int i=0; i<N; i++){
100+
scanf("%d",&tmp);
101+
data.push_back(tmp);
102+
}
103+
work();
104+
}
105+
return 0;
106+
}
107+
108+

hdoj/1698.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
const int MAXN = 100001;
6+
int tree[MAXN*8],N,T, lazy[MAXN*8];
7+
8+
void pushUp(int idx){
9+
tree[idx] = tree[idx*2] + tree[idx*2+1];
10+
}
11+
12+
void build(int l, int r, int idx){
13+
if(l==r){
14+
tree[idx] = 1;
15+
return;
16+
}
17+
int m = (l+r)>>1;
18+
build(l, m, idx*2);
19+
build(m+1, r, idx*2+1);
20+
pushUp(idx);
21+
}
22+
23+
void insert(int L, int R, int l, int r, int idx, int value){
24+
cout<<"now idx = "<<idx<<endl;
25+
if(L<=l && r<=R){
26+
cout<<"inserting "<<L<<" to "<<R<<" idx = "<<idx<<" value = "<<value<<endl;
27+
tree[idx] = (R-L+1)*value;
28+
return ;
29+
}
30+
int m = (l+r)>>1;
31+
if(L<=m){
32+
insert(L, m, l, m, idx*2, value);
33+
}
34+
if(R>m){
35+
insert(m+1, R, m+1, r, idx*2+1, value);
36+
}
37+
pushUp(idx);
38+
}
39+
40+
void work(){
41+
int ops;
42+
scanf("%d",&ops);
43+
memset(tree,0,sizeof(tree));
44+
memset(lazy,0,sizeof(lazy));
45+
build(1,N,1);
46+
cout<<"ops = "<<ops<<endl;
47+
for(int i=0; i<ops; i++){
48+
int a,b,c;
49+
scanf("%d %d %d",&a,&b,&c);
50+
insert(a,b,1,N,1,c);
51+
}
52+
cout<<tree[1]<<endl;
53+
}
54+
55+
56+
57+
int main(){
58+
scanf("%d",&T);
59+
for(int t=1; t<=T; t++){
60+
scanf("%d",&N);
61+
work();
62+
}
63+
64+
return 0;
65+
}
66+

hdoj/1754.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
int N,M;
5+
const int MAXN = 200000;
6+
int data[MAXN*8];
7+
const int INF = 999999999;
8+
9+
void pushUp(int idx){
10+
data[idx] = max( data[idx*2], data[idx*2+1]);
11+
}
12+
13+
14+
void build(int l, int r, int idx){
15+
if(l==r){
16+
scanf("%d",&data[idx]);
17+
return ;
18+
}
19+
int m = (l+r)>>1;
20+
build(l, m, idx*2);
21+
build(m+1, r, (idx*2)+1);
22+
pushUp(idx);
23+
}
24+
25+
26+
int query(int L, int R, int l, int r, int idx){
27+
if(L<=l && R>=r){
28+
return data[idx];
29+
}
30+
int m = (l+r)>>1;
31+
int ret1=-INF,ret2=-INF;
32+
if(L<=m)ret1 = query(L,R,l,m,idx*2);
33+
if(R>m)ret2 = query(L,R,m+1,r,idx*2+1);
34+
return max(ret1,ret2);
35+
}
36+
37+
void update(int L, int R, int l, int r, int idx, int value){
38+
if(L<=l && R>=r){
39+
data[idx] = value;
40+
return;
41+
}
42+
int m = (l+r)>>1;
43+
if(R<=m){
44+
update(L,R,l,m,idx*2,value);
45+
}else if(L>=m+1){
46+
update(L,R,m+1,r,idx*2+1,value);
47+
}
48+
pushUp(idx);
49+
}
50+
51+
int main(){
52+
while(scanf("%d%d",&N,&M)!=EOF){
53+
getchar();
54+
build(1,N,1);
55+
char op[2];
56+
int a,b;
57+
for(int i=0; i<M; i++){
58+
scanf("%s %d %d",op,&a,&b);
59+
if(op[0]=='Q'){
60+
cout<<query(a,b,1,N,1)<<endl;;
61+
}else{
62+
update(a,a,1,N,1,b);
63+
}
64+
}
65+
66+
67+
}
68+
69+
return 0;
70+
}
71+
72+
73+
74+

0 commit comments

Comments
 (0)