锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
A sequence of memory units is called a memory block.
The memory control system we consider now has four kinds of operations:
1. Reset Reset all memory units free.
2. New x Allocate a memory block consisted of x continuous free memory units with the least start number
3. Free x Release the memory block which includes unit x
4. Get x Return the start number of the xth memory block(Note that we count the memory blocks allocated from left to right)
Where 1<=x<=N.You are request to find out the output for M operations.
Each test case starts with two integer N,M(1<=N,M<=50000) ,indicating that there are N units of memory and M operations.
Follow by M lines,each line contains one operation as describe above.
For each “New” operation, if it’s possible to allocate a memory block,
output “New at A”,where Ais the least start number,otherwise output “Reject New”.
For each “Free” operation, if it’s possible to find a memory block occupy unit x,
output “Free from A to B”,where A and B refer to the start and end number of the memory block,otherwise output “Reject Free”.
For each “Get” operation, if it’s possible to find the xth memory blocks,
output “Get at A”,where A is its start number,otherwise output “Reject Get”.
Output one blank line after each test case.
New 2
New 5
New 2
New 2
Free 3
Get 1
Get 2
Get 3
Free 3
Reset
Reject New
New at 3
New at 5
Free from 3 to 4
Get at 1
Get at 5
Reject Get
Reject Free
Reset Now
#include <stdlib.h>
#include <vector>
#include <iostream>
#define maxn 51000
using namespace std;
inline int L(int th)
{
return (th << 1);
}
inline int R(int th)
{
return ((th << 1) | 1);
}
struct Tree
{
int l, r;
int cover, rely;
int lm, rm, maxl;
int getLen()
{
return r - l;
}
int mid()
{
return ((l + r) >> 1);
}
}stree[maxn * 4];
void build(int l, int r, int th)
{
//printf("%d %d\n", l, r);
stree[th].l = l, stree[th].r = r;
stree[th].cover = stree[th].rely = 0;
stree[th].lm = stree[th].rm = stree[th].maxl = stree[th].getLen();
if (l + 1 == r)
{
return;
}
int m = stree[th].mid();
if (r <= m)
{
build(l, r, L(th));
}
else if (l >= m)
{
build(l, r, R(th));
}
else
{
build(l, m, L(th)), build(m, r, R(th));
}
}
void update(int th, int kind)//rely鎿嶄綔鏇存柊
{
stree[th].rely = 1;
stree[th].cover = kind;
if (stree[th].cover == 0)
{
stree[th].lm = stree[th].rm = stree[th].maxl = stree[th].getLen();
}
else
{
stree[th].lm = stree[th].rm = stree[th].maxl = 0;
}
}
void insert(int l, int r, int th, int kind)
{
//printf("insert %d, %d\n", l, r);
if (stree[th].l == l && stree[th].r == r)
{
update(th, kind);
return;
}
if (stree[th].rely)
{
stree[th].rely = 0;
update(L(th), stree[th].cover);
update(R(th), stree[th].cover);
}
stree[th].cover = -1;//璁劇疆鏃犳晥鍖哄煙錛屽湪榪欓噷浣撶幇涓嶄笂鐢ㄥ錛屼笉鏇存柊涔熻,鍑轟簬涔犳儻璺熼昏緫闇瑕?nbsp;
int m = stree[th].mid();
if (r <= m)
{
insert(l, r, L(th), kind);
}
else if (l >= m)
{
insert(l, r, R(th), kind);
}
else
{
insert(l, m, L(th), kind);
insert(m, r, R(th), kind);
}
stree[th].lm = stree[L(th)].lm, stree[th].rm = stree[R(th)].rm;
if (stree[L(th)].lm == stree[L(th)].getLen())
{
stree[th].lm += stree[R(th)].lm;
}
if (stree[R(th)].rm == stree[R(th)].getLen())
{
stree[th].rm += stree[L(th)].rm;
}
stree[th].maxl = max(stree[L(th)].maxl, stree[R(th)].maxl);
stree[th].maxl = max(stree[th].maxl, stree[L(th)].rm + stree[R(th)].lm);
}
int query(int th, int len)
{
if (stree[th].l + 1 == stree[th].r)
{
if (stree[th].maxl >= len)
{
return stree[th].l;
}
return 0;
}
if (stree[th].rely)
{
stree[th].rely = 0;
update(L(th), stree[th].cover);
update(R(th), stree[th].cover);
}
if (stree[th].maxl < len)//璇ョ嚎孌墊渶澶х┖鐧芥maxl灝忎簬len
{
return 0;
}
if (stree[L(th)].maxl >= len)//宸﹀瀛愭渶澶х┖鐧芥婊¤凍len
{
return query(L(th), len);
}
else if(stree[L(th)].rm + stree[R(th)].lm >= len)//鐢變袱瀛╁瓙鐨勪腑闂村悎騫舵弧瓚?nbsp;
{
return stree[L(th)].r - stree[L(th)].rm;
}
else//鍙沖瀛愭弧瓚?nbsp;
{
return query(R(th), len);
}
}
struct Seg
{
int left, right;
};
vector<Seg>list;
int bsc(int key)
{
//鏌ユ壘key鎵鍦ㄧ殑浣嶇疆鐨勫悗涓浣嶏紝
//濡傛灉涓嶅瓨鍦ㄥ寘鍚玨ey鐨勭嚎孌墊椂錛?br> //1.key姣旂涓涓嚎孌靛皬錛岃繑鍥炵殑鏄?,0-1灝辯瓑浜?1
//2.key姣旇搗鐐規渶澶х嚎孌佃繕澶э紝鍒欒繑鍥炶繖涓嚎孌電殑鍚庝竴浣嶏紝key鍙兘琚繖涓嚎孌靛寘鍚紝鍙兘涓嶈榪欎釜綰挎鍖呭惈錛屽彇鍐充簬榪欎釜綰挎鐨勫彸绔偣
int l = 0, r = list.size() - 1, m;
while (l <= r)
{
m = (l + r) >> 1;
if (list[m].left > key)
{
r = m - 1;
}
else if (list[m].left <= key)//淇濊瘉l鍦ㄦ弧瓚崇嚎孌電殑鍚庝竴浣?nbsp;
{
l = m + 1;
}
}
return l;
}
void show()
{
int i;
for (i = 0; i < list.size(); i++)
{
printf("%d:%d %d\n", i, list[i].left, list[i].right);
}printf("----\n");
}
int main()
{
int len, x, k, n, m, id;
Seg tmp;
while (scanf("%d%d", &n, &m) - EOF)
{
build(1, n + 1, 1);
char op[10];
list.clear();
while (m--)
{
scanf("%s", op);
if (*op == 'N')
{
scanf("%d", &len);
k = query(1, len);
if (k)
{
printf("New at %d\n", k);
insert(k, k + len, 1, 1);
tmp.left = k, tmp.right = k + len;
if (list.size() == 0)
{
list.push_back(tmp);
}
else
{
id = bsc(k);
list.insert(list.begin() + id, tmp);
}
//show();
}
else
{
printf("Reject New\n");
}
}
else if (*op == 'F')
{
scanf("%d", &x);
k = bsc(x) - 1;
if (k == -1 || list[k].right <= x)
{
printf("Reject Free\n");
}
else
{
printf("Free from %d to %d\n", list[k].left, list[k].right - 1);
insert(list[k].left, list[k].right, 1, 0);
list.erase(list.begin() + k, list.begin() + k + 1);
}
}
else if (*op == 'G')
{
scanf("%d", &x);
if (x <= list.size())
{
printf("Get at %d\n", list[x-1].left);
}
else
{
printf("Reject Get\n");
}
}
else if (*op == 'R')
{
insert(1, n + 1, 1, 0);
list.clear();
printf("Reset Now\n");
}
}
printf("\n");
}
return 0;
}
/*
3 100
N 1
N 1
N 1
F 1
N 1
F 2
N 1
F 3
N 2
*/
]]>Problem Description
Input
(1) Add i j,i鍜宩涓烘鏁存暟,琛ㄧず絎琲涓惀鍦板鍔爅涓漢錛坖涓嶈秴榪?0錛?br>(2)Sub i j ,i鍜宩涓烘鏁存暟,琛ㄧず絎琲涓惀鍦板噺灝慾涓漢錛坖涓嶈秴榪?0錛?
(3)Query i j ,i鍜宩涓烘鏁存暟,i<=j錛岃〃紺鴻闂i鍒扮j涓惀鍦扮殑鎬諱漢鏁?
(4)End 琛ㄧず緇撴潫錛岃繖鏉″懡浠ゅ湪姣忕粍鏁版嵁鏈鍚庡嚭鐜?
姣忕粍鏁版嵁鏈澶氭湁40000鏉″懡浠?br> Output
瀵逛簬姣忎釜Query璇㈤棶錛岃緭鍑轟竴涓暣鏁板茍鍥炶濺,琛ㄧず璇㈤棶鐨勬涓殑鎬諱漢鏁?榪欎釜鏁版渶澶氫笉瓚呰繃1000000銆?br> Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End Sample Output
Case 1:
6
33
59
鐩存帴璐達細
#include <stdlib.h>
#include <iostream>
#define maxn 50010
using namespace std;
struct Tree
{
int l, r, sum;
}tree[maxn * 5];
int p[maxn];
inline int L(int x)
{
return (x << 1);
}
inline int R(int x)
{
return (x << 1) | 1;
}
void build(int l, int r, int th)
{
tree[th].l = l, tree[th].r = r, tree[th].sum = 0;
if (l == r)
{
tree[th].sum = p[l];
return;
}
int m = (l + r) >> 1;
build(l, m, L(th)), build(m + 1, r, R(th));
tree[th].sum = tree[L(th)].sum + tree[R(th)].sum;
}
void insert(int l, int r, int th, int num)
{
if (tree[th].l == l && tree[th].r == r)
{
tree[th].sum += num;
//if (tree[th].sum < 0) tree[th].sum = 0;鏈夌偣澶氭兂浜?wa浜嗕竴嬈?nbsp;
return;
}
int m = (tree[th].l + tree[th].r) >> 1;
if (r <= m)
{
insert(l, r, L(th), num);
}
else if (l > m)
{
insert(l, r, R(th), num);
}
else
{
insert(l, m, L(th), num), insert(m + 1, r, R(th), num);
}
tree[th].sum = tree[L(th)].sum + tree[R(th)].sum;
}
int query(int l, int r, int th)
{
if (tree[th].l == l && tree[th].r == r)
{
return tree[th].sum;
}
int m = (tree[th].l + tree[th].r) >> 1;
if (r <= m)
{
return query(l, r, L(th));
}
else if (l > m)
{
return query(l, r, R(th));
}
else
{
return query(l, m, L(th)) + query(m + 1, r, R(th));
}
}
int main()
{
int t, n, i, a, b;
char op[10];
scanf("%d", &t);
int ca = 0;
while (t--)
{
ca++;
printf("Case %d:\n", ca);
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%d", &p[i]);
}
build(1, n, 1);
while (1)
{
scanf("%s", op);
if (op[0] == 'E') break;
scanf("%d%d", &a, &b);
if (op[0] == 'Q')
{
int ans = query(a, b, 1);
printf("%d\n", ans);
}
else if (op[0] == 'A')
{
insert(a, a, 1, b);
}
else
{
insert(a, a, 1, -b);
}
}
}
return 0;
}
/*
10
1 2 3 4 5 6 7 8 9 10
Q 1 1
Q 1 10
3
1 2 3
S 1 100
Q 1 1
*/
]]>Problem Description
We
define two operations, 1: “Not” operation that we change the A[i, j,
k]=!A[i, j, k]. that means we change A[i, j, k] from 0->1,or
1->0. (x1<=i<=x2,y1<=j<=y2,z1<=k<=z2).
0: “Query” operation we want to get the value of A[i, j, k].
Input
First line contains N and M, M lines follow indicating the operation below.
Each operation contains an X, the type of operation. 1: “Not” operation and 0: “Query” operation.
If X is 1, following x1, y1, z1, x2, y2, z2.
If X is 0, following x, y, z.
Output
Sample Input
2 5
1 1 1 1 1 1 1
0 1 1 1
1 1 1 1 2 2 2
0 1 1 1
0 2 2 2Sample Output
1
0
1
鍒嗘瀽:
闈炴爲鐘舵暟緇勮帿灞烇紝鍏朵粬鏁版嵁緇撴瀯閮藉お甯呬簡錛岀敤涓嶇潃銆?br> 鍏堣璇翠竴緇翠笂鐨勬爲鐘舵暟緇勶紝浠涔堥棶棰橀兘寰楁垚鏈綆鍗曠殑寮濮嬶紝鐞嗚В鍚庢墠鑳芥帹騫垮埌澶氱淮鎴栧鏉傜殑銆?br> 鎶婂鏌愪釜鏂逛綋鐨勪慨鏀規搷浣滅湅鍋氭槸瀵規煇涓柟浣撶殑鎿嶄綔鏁?1錛岄偅涓涓綅緗搷浣滄暟鐨勫鍋舵у喅瀹氫簡瀹冪殑鍊箋?br>鏍戠姸鏁扮粍1浣嶇疆鏄1鐨勶紝2浣嶇疆鏄1鍒?鐨?3鏄3鐨勶紝4鏄1鍒?鐨?.....鎵浠ュ涓涓尯闂存搷浣滐紝榪欎釜鍖洪棿棣栧厛鎸夋爲鐘舵暟緇勭殑鍒嗗尯瑙勫垯錛屼細琚垎鎴愯嫢騫蹭釜涓嶇浉浜ょ殑鍖洪棿錛屽湪鏍戠姸鏁扮粍閲屽搴旂殑灝辨槸鑻ュ共涓嶇浉浜ょ殑灝忔爲銆?br> 姣斿瀵?鍒?鎿嶄綔錛屼細鍒嗘垚鍒嗗埆瀵筟1,2],[3,3]涓ゆ鐨勬搷浣滐紱瀵?鍒?鐨勬搷浣滐紝鍙互鍒嗘垚[2,2],[3,3],[4,4]錛屼絾鏄榪欑鎯呭喌錛屾寜榪欑鏂瑰紡鍘諱慨鏀規爲鐘舵暟緇勬槸楹葷儲鐨勶紝鍙互杞寲鎴愬垎鍒[1,4]鎿嶄綔錛屽啀瀵筟1,1]鎿嶄綔錛岃繖鏍穂1,1]琚慨鏀逛簡涓ゆ錛屾墍浠ョ浉褰撲簬娌′慨鏀硅繃(閰嶅悎榪欓鐨勫鍋舵?錛屼篃鍙鐞嗘垚瀵筟1,4]鐨勬搷浣滄暟+1錛屽啀瀵筟1,1]鐨勬搷浣滄暟-1銆?br> 鐭ラ亾姣忔鐨勪俊鎭繚瀛樻搷浣滄鏁幫紝鍙互閫氳繃鍙犲姞灝辯煡閬撴瘡涓綅緗殑鎿嶄綔鏁般?br>姣斿瀵?鍒?鎿嶄綔浜嗕竴嬈★紝c[2] = 1(瀵瑰簲[1,2]鐨勫尯闂?錛宑[3] = 1(瀵瑰簲[3,3]鍖洪棿)錛涘啀瀵?鎿嶄綔錛屽垯c[1] = 1錛堝搴擺1,1]鍖洪棿錛夈傚垯1鐨勬搷浣滄暟鏄痗[1] + c[2]銆?br> 鍒版榪樹笉鐞嗚В鐨勶紝寤鴻鏈濂界湅鐪?a href="http://www.shnenglu.com/Ylemzy/articles/98322.html">鏍戠姸鏁扮粍鐨勮緇嗙粨鏋?/a>錛岀劧鍚庣敾涓鐢誨氨搴旇娓呮澶氫簡錛岃〃杈句笂鐨勪笉渚胯鍘熻皡錛岃繖涓畻鏄瘮杈冩按鐨勶紝涓嶈繃瀵規爲鐘舵暟緇勭殑鐞嗚В鏄湁鐐圭敤澶勭殑錛岃繕鏈夊氨鏄湪鏍戠姸鏁扮粍涓婂鏂瑰潡鐨勫垏鍓詫紝渚嬪瀵瑰2鍒?鐨勬搷浣滈渶瑕佸鐞嗘垚[1,4]鎿嶄綔錛屽啀瀵筟1,1]鎿嶄綔 錛屽皢榪欎釜鎿嶄綔鎺ㄥ箍鍒?緇淬?br> 涓緇寸殑鏄尯闂寸鍖洪棿錛屼簩緇存槸鏂瑰潡綆$悊鏂瑰潡錛屼笁緇存槸鏂逛綋綆$悊鏂逛綋錛屾湰璐ㄨ窡涓緇翠竴鏍楓?br>
#include <stdlib.h>
#include <iostream>
#define maxn 110
using namespace std;
int c[maxn][maxn][maxn];
int n;
void set()
{
int i, j, k;
for (i = 0; i <= n; i++)
{
for (j = 0; j <= n; j++)
{
for (k = 0; k <= n; k++)
{
c[i][j][k] = 0;
}
}
}
}
inline int lowbit(int x)
{
return x & (-x);
}
void update(int x, int y, int z)//鍒嗗潡鏇存柊鎴栨搷浣?nbsp;
{
int i, j, k;
for (i = x; i > 0; i -= lowbit(i))
{
for (j = y; j > 0; j -= lowbit(j))
{
for (k = z; k > 0; k -= lowbit(k))
{
c[i][j][k]++;
}
}
}
}
int sum(int x, int y, int z)//鍚戜笂鍙犲姞姣忎釜鐖惰妭鐐圭殑鎿嶄綔嬈℃暟
{
int i, j, k, var = 0;
for (i = x; i <= n; i += lowbit(i))
{
for (j = y; j <= n; j += lowbit(j))
{
for (k = z; k <= n; k += lowbit(k))
{
var += c[i][j][k];
}
}
}
return var;
}
int main()
{
int m, op, x1, y1, z1, x2, y2, z2, ans;
while (scanf("%d%d", &n, &m) - EOF)
{
set();
while (m--)
{
scanf("%d%d%d%d", &op, &x1, &y1, &z1);
if (op == 1)
{
scanf("%d%d%d", &x2, &y2, &z2);
update(x2, y2, z2);
update(x2, y1 - 1, z2);
update(x1 - 1, y2, z2);
update(x1 - 1, y1 - 1, z2);
//--------
update(x2, y2, z1 - 1);
update(x2, y1 - 1, z1 - 1);
update(x1 - 1, y2, z1 - 1);
update(x1 - 1, y1 - 1, z1 - 1);
}
else
{
ans = sum(x1, y1, z1);
printf("%d\n", ans % 2);
}
}
}
return 0;
}
/*
2 100
1 2 2 2 2 2 2
0 1 1 1
1
2 10
1 2 1 2 2
Q 2 2
1 2 1 2 1
Q 1 1
1 1 1 2 1
1 1 2 1 2
1 1 1 2 2
Q 1 1
1 1 1 2 1
Q 2 1
*/
]]>
]]>