题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
敌兵布阵Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 97473 Accepted Submission(s): 41240
Problem Description
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。 接下来每行有一条命令,命令有4种形式: (1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30) (2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30); (3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数; (4)End 表示结束,这条命令在每组数据最后出现; 每组数据最多有40000条命令
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
Sample Output
Author
Windbreaker
Recommend
Eddy
|
解析:裸的线段树单点更新,求区间和,可以用树状数组和线段树写
代码1:
//#include
#include
#include
#include
#include
typedef long long LL;
using namespace std;
const int N = 200009;
int dp[N];
int n;
void add(int k, int c)
{
while(k <= n)
{
dp[k] += c;
k += k&(-k);
}
}
int sum(int k)
{
int ans = 0;
while(k)
{
ans += dp[k];
k -= k&(-k);
}
return ans;
}
int main()
{
int T, cnt = 0;
scanf("%d", &T);
char s[11];
while(T--)
{
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
int num;
scanf("%d", &num);
add(i, num);
}
int x, y;
printf("Case %d:\n", ++cnt);
while(true)
{
scanf(" %s", s);
if(s[0] == 'E') break;
scanf("%d%d", &x, &y);
if(s[0] == 'Q') printf("%d\n", sum(y)-sum(x-1));
else if(s[0] == 'S') add(x, -y);
else add(x, y);
}
}
return 0;
}
代码2:
//#include
#include
#include
#include
typedef long long LL;
using namespace std;
const int N = 200009;
struct node
{
int x;
}t[N<<2];
void Creat(int i, int l, int r)
{
if(l == r)
{
scanf("%d", &t[i].x);
return ;
}
int mid = (l + r)>>1;
Creat(i*2, l, mid);
Creat(i*2+1, mid+1, r);
t[i].x = t[i*2].x + t[i*2+1].x;
}
void updata(int i, int l, int r, int x, int y)
{
if(l <= x && x <= r)
{
t[i].x += y;
if(l == r) return ;
int mid = (l + r)>>1;
updata(i*2, l, mid, x, y);
updata(i*2+1, mid+1, r, x, y);
}
}
int Find(int i, int l, int r, int x, int y)
{
if(l == x && r == y) return t[i].x;
int mid = (l + r)>>1;
if(mid >= y) return Find(i*2, l, mid, x, y);
else if(mid < x) return Find(i*2+1, mid+1, r, x, y);
return Find(i*2, l, mid, x, mid) + Find(i*2+1, mid+1, r, mid+1, y);
}
int main()
{
int T, cnt = 0, n;
scanf("%d", &T);
char s[11];
while(T--)
{
scanf("%d", &n);
Creat(1, 1, n);
int x, y;
printf("Case %d:\n", ++cnt);
while(true)
{
scanf(" %s", s);
if(s[0] == 'E') break;
scanf("%d%d", &x, &y);
if(s[0] == 'Q') printf("%d\n", Find(1, 1, n, x, y));
else if(s[0] == 'S') updata(1, 1, n, x, -y);
else updata(1, 1, n, x, y);
}
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
空AI大赛”(报名中...)
|
I Hate ItTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 81816 Accepted Submission(s): 31472
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0 第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。 接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。 当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。 当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
Sample Output
Author
linle
Source
2007省赛集训队练习赛(6)_linle专场
Recommend
lcy
|
解析:线段树单点更新,求最值,可以用树状数组(比较麻烦)、线段树
代码1:
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int N = 500009;
int dp[N], n, a[N];
void add(int k)
{
while(k <= n)
{
dp[k] = a[k];
int lx = k&(-k);
for(int i = 1; i < lx; i <<= 1) dp[k] = max(dp[k], dp[k - i]);
k += k&(-k);
}
}
int sum(int x, int y)
{
int ans = 0;
while(y >= x)
{
ans = max(ans, a[y]);
y--;
for(; y - (y&(-y)) >= x; y -= y&(-y)) ans = max(ans, dp[y]);
}
return ans;
}
int main()
{
int m;
while(~scanf("%d%d", &n, &m))
{
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
add(i);
}
char s[109];
int x, y;
//scanf("%d", &m);
while(m--)
{
scanf(" %s", s);
scanf("%d%d", &x, &y);
if(s[0] == 'Q') printf("%d\n", sum(x, y));
else a[x] = y, add(x);
}
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
3 5 5 2 4 3 3 3
1 2 1 3 -1
解析:线段树去维护一个高度,没遇到一个新的广告牌,就去找最左边的
代码:
#include
using namespace std;
typedef long long LL;
const int N = 200009;
struct node
{
int x;
}t[N<<2];
int w;
void init(int i, int l, int r)
{
t[i].x = w;
if(l != r)
{
int mid = (l + r)>>1;
init(i*2, l, mid);
init(i*2+1, mid+1, r);
}
}
int Creat(int i, int l, int r, int x)
{
if(l == r && t[i].x >= x)
{
t[i].x -= x;
return l;
}
int mid = (l + r)>>1, ans = -1;
if(t[i*2].x >= x) ans = Creat(i*2, l, mid, x);
else if(t[i*2+1].x >= x) ans = Creat(i*2+1, mid+1, r, x);
t[i].x = max(t[i*2].x, t[i*2+1].x);
return ans;
}
int main()
{
int num, n, h;
while(~scanf("%d%d%d", &h, &w, &n))
{
h = min(h, n);
init(1, 1, h);
for(int i = 1; i <= n; i++)
{
scanf("%d", &num);
printf("%d\n", Creat(1, 1, h, num));
}
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
1 0 2 4
解析:用线段树求连续区间,维护一个lx,rx,mx,分别是左连续区间,右连续区间,最大连续区间,还有运用到栈暂存破坏的村庄
代码:
#include
using namespace std;
typedef long long LL;
const int N = 50009;
struct node
{
int mx, lx, rx;
} t[N<<2];
stack s;
void Creat(int i, int l, int r)
{
t[i].mx = t[i].lx = t[i].rx = r - l + 1;
if(l != r)
{
int mid = (l + r)>>1;
Creat(i*2, l, mid);
Creat(i*2+1, mid+1, r);
}
}
void updata(int i, int l, int r, int x, int f)
{
if(l == r)
{
if(f) t[i].lx = t[i].rx = t[i].mx = 1;
else t[i].lx = t[i].rx = t[i].mx = 0;
return ;
}
int mid = (l + r)>>1;
if(x <= mid) updata(i*2, l, mid, x, f);
else updata(i*2+1, mid+1, r, x, f);
t[i].lx = t[i*2].lx;
t[i].rx = t[i*2+1].rx;
t[i].mx = max(max(t[i*2].mx, t[i*2+1].mx), t[i*2].rx + t[i*2+1].lx);
if(t[i*2].lx == mid - l + 1) t[i].lx += t[i*2+1].lx;
if(t[i*2+1].rx == r - mid) t[i].rx += t[i*2].rx;
}
int Find(int i, int l, int r, int x)
{
if(l == r || t[i].mx == 0 || t[i].mx == r - l + 1) return t[i].mx;
int mid = (l + r)>>1;
if(x <= mid)
{
if(x >= mid - t[i*2].rx + 1) return Find(i*2, l, mid, x) + Find(i*2+1, mid+1, r, mid+1);
else return Find(i*2, l, mid, x);
}
else
{
if(x <= mid+t[i*2+1].lx) return Find(i*2, l, mid, mid) + Find(i*2+1, mid+1, r, x);
else return Find(i*2+1, mid+1, r, x);
}
}
int main()
{
int n, m, x;
char ch;
while(~scanf("%d%d", &n, &m))
{
Creat(1, 1, n);
while(!s.empty()) s.pop();
while(m--)
{
scanf(" %c", &ch);
if(ch == 'R')
{
if(!s.empty())
{
int num = s.top();
s.pop();
updata(1, 1, n, num, 1);
}
continue;
}
scanf("%d", &x);
if(ch == 'D')
{
s.push(x);
updata(1, 1, n, x, 0);
}
else printf("%d\n", Find(1, 1, n, x));
}
}
return 0;
}
题目链接:http://lightoj.com/volume_showproblem.php?problem=1080
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 64 MB |
Given a binary number, we are about to do some operations on the number. Two types of operations can be here.
'I i j' which means invert the bit from i to j (inclusive)
'Q i' answer whether the ith bit is 0 or 1
The MSB (most significant bit) is the first bit (i.e. i=1). The binary number can contain leading zeroes.
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing a binary integer having length n (1 ≤ n ≤ 105). The next line will contain an integer q (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form 'I i j' where i, j are integers and 1 ≤ i ≤ j ≤ n. Or the query will be in the form 'Q i' where i is an integer and 1 ≤ i ≤ n.
For each case, print the case number in a single line. Then for each query 'Q i' you have to print 1 or 0 depending on the ith bit.
Sample Input |
Output for Sample Input |
2 0011001100 6 I 1 10 I 2 7 Q 2 Q 1 Q 7 Q 5 1011110111 6 I 1 10 I 2 7 Q 2 Q 1 Q 7 Q 5 |
Case 1: 0 1 1 0 Case 2: 0 0 0 1 |
Dataset is huge, use faster i/o methods.
解析:区间翻转,1变成0,0变成1,我们应该只关心它翻转的次数是奇数还是偶数,所以用树状数组维护区间
代码:
#include
using namespace std;
typedef long long LL;
const int N = 500009;
int dp[N];
char s[N];
int n;
void add(int k, int x)
{
while(k <= n)
{
dp[k] += x;
k += k&(-k);
}
}
int sum(int k)
{
int ans = 0;
while(k)
{
ans += dp[k];
k -= k&(-k);
}
return ans;
}
int main()
{
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
memset(dp, 0, sizeof(dp));
scanf(" %s", s);
int m, l, r;
char ch;
scanf("%d", &m);
n = strlen(s);
printf("Case %d:\n", ++cnt);
while(m--)
{
scanf(" %c", &ch);
if(ch == 'I')
{
scanf("%d%d", &l, &r);
add(l, 1);
add(r+1, -1);
}
else
{
scanf("%d", &r);
if(sum(r)&1) printf("%d\n", !(s[r - 1] - '0'));
else printf("%c\n", s[r-1]);
}
}
}
return 0;
}
题目链接:http://lightoj.com/volume_showproblem.php?problem=1085
PDF (English) | Statistics | Forum |
Time Limit: 3 second(s) | Memory Limit: 64 MB |
An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold
1. i1 < i2 < i3 < ... < ik and
2. Ai1 < Ai2 < Ai3 < ... < Aik
Now you are given a sequence, you have to find the number of all possible increasing subsequences.
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.
For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.
Sample Input |
Output for Sample Input |
3 3 1 1 2 5 1 2 1000 1000 1001 3 1 10 11 |
Case 1: 5 Case 2: 23 Case 3: 7 |
1. For the first case, the increasing subsequences are (1), (1, 2), (1), (1, 2), 2.
2. Dataset is huge, use faster I/O methods.
解析:求前面有多少比它小的数的所有组合,用树状数组去维护前面有多少个比它小的数的种类,最后加一下就好了
代码:
#include
using namespace std;
typedef long long LL;
const int N = 500009;
const int mod = 1000000007;
int dp[N], a[N], b[N];
int n;
void add(int k, int x)
{
while(k <= n)
{
dp[k] += x;
dp[k] %= mod;
k += k&(-k);
}
}
int sum(int k)
{
int ans = 0;
while(k)
{
ans += dp[k];
ans %= mod;
k -= k&(-k);
}
return ans;
}
bool cmp(int i, int j)
{
if(a[i] != a[j]) return a[i] < a[j];
return i > j;
}
int main()
{
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
b[i] = i;
}
sort(b+1, b+n+1, cmp);
for(int i = 1; i <= n; i++) add(b[i], sum(b[i])+1);
printf("Case %d: %d\n", ++cnt, sum(n));
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541
5 1 1 5 1 7 1 3 3 5 5
1 2 1 1 0
解析:仔细想想y其实并没有什么用,因为它输入时已经有序了,所以咱用树状数组去维护前面有多少个比它小的,最后输出即可
代码:
#include
using namespace std;
const int N = 32006;
int n;
int dp[N];
int num[N];
void add(int k, int c)
{
while(k <= N)
{
dp[k] += c;
k += k&(-k);
}
}
int sum(int k)
{
int ans = 0;
while(k)
{
ans += dp[k];
k -= k&(-k);
}
return ans;
}
int main()
{
while(~scanf("%d", &n))
{
int x, y;
memset(dp, 0, sizeof(dp));
memset(num, 0, sizeof(num));
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &x, &y);
num[sum(x+1)]++;
add(x+1, 1);
}
for(int i = 0; i < n; i++) printf("%d\n", num[i]);
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394
10 1 3 6 9 0 8 5 7 4 2
16
解析:求逆序数,如果单纯的求逆序数,咱可以用归并排序,但这个题是一个环,求最小的逆序数,求出原序列的逆序数之后,咱可以用递推,求其他的逆序数,设原序列逆序数为s,把第一个数a移到最后,多出来比a大的数(n-a)个逆序数,少了比a小的数(n-a-1)个逆序数,递推式: s += n - a[i] - (a[i] - 1); //+ >a[i] -
代码: 题目链接:http://codeforces.com/problemset/problem/272/C 解析:这是一个线段树区间更新的题,由于题意只放在最左侧,咱可以用一个变量去维护高度,这样这个题就变成一个水题了 代码: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 解析:线段树区间更新,直接板子 代码: 题目链接:http://poj.org/problem?id=3468 #include
#include
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34563 Accepted Submission(s): 16879
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
#include
A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. Input The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000. Output You need to answer all Q commands in order. One answer in a line. Sample Input 10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4 Sample Output 4 55 9 15 Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
|
[Submit] [Go Back] [Status] [Discuss]
解析:线段树区间更新,求区间和
代码:
//#include
#include
#include
#include
typedef long long LL;
using namespace std;
const int N = 100009;
struct node
{
LL x, mark;
}t[N<<2];
void Creat(int i, int l, int r)
{
t[i].mark = 0ll;
if(l == r)
{
scanf("%lld", &t[i].x);
return ;
}
int mid = (l + r)>>1;
Creat(i*2, l, mid);
Creat(i*2+1, mid+1, r);
t[i].x = t[i*2].x + t[i*2+1].x;
}
void pushdown(int i, int l, int r)
{
if(t[i].mark)
{
int mid = (l + r)>>1;
t[i*2].mark += t[i].mark;
t[i*2+1].mark += t[i].mark;
t[i*2].x += t[i].mark*(mid-l+1);
t[i*2+1].x += t[i].mark*(r-mid);
t[i].mark = 0;
}
}
void updata(int i, int l, int r, int x, int y, LL z)
{
if(r < x || y < l) return ;
if(x <= l && r <= y)
{
t[i].mark += z;
t[i].x += z*(r-l+1);
return ;
}
pushdown(i, l, r);
int mid = (l + r)>>1;
updata(i*2, l, mid, x, y, z);
updata(i*2+1, mid+1, r, x, y, z);
t[i].x = t[i*2].x + t[i*2+1].x;
}
LL Find(int i, int l, int r, int x, int y)
{
if(r < x || y < l) return 0;
if(x <= l && r <= y) return t[i].x;
pushdown(i, l, r);
int mid = (l + r)>>1;
return Find(i*2, l, mid, x, y) + Find(i*2+1, mid+1, r, x, y);
}
int main()
{
int n, q;
scanf("%d%d", &n, &q);
Creat(1, 1, n);
char ch;
int x, y;
LL z;
while(q--)
{
scanf(" %c", &ch);
if(ch == 'Q')
{
scanf("%d%d", &x, &y);
printf("%lld\n", Find(1, 1, n, x, y));
}
else
{
scanf("%d%d%lld", &x, &y, &z);
updata(1, 1, n, x, y, z);
}
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4553
1 5 6 DS 3 NS 2 NS 4 STUDY!! 1 5 DS 4 NS 2
Case 1: 1,let's fly 4,don't put my gezi wait for me I am the hope of chinese chengxuyuan!! 1,let's fly 1,don't put my gezi
解析:线段树区间更新,比较复杂需要考虑优先级和lx,rx,mx
代码:
#include
using namespace std;
typedef long long LL;
const int N = 100010;
struct node
{
int d, n, s; // mark
int dlx, drx, dmx;
int nlx, nrx, nmx;
}t[N<<2];
void ns(int i, int l, int r)
{
t[i].n = 1; t[i].d = 0;
t[i].dlx = t[i].drx = t[i].dmx = 0;
t[i].nlx = t[i].nrx = t[i].nmx = 0;
}
void ds(int i, int l, int r)
{
t[i].d = 1;
t[i].dlx = t[i].drx = t[i].dmx = 0;
}
void stu(int i, int l, int r)
{
t[i].s = 1; t[i].d = t[i].n = 0;
t[i].dlx = t[i].drx = t[i].dmx = r - l + 1;
t[i].nlx = t[i].nrx = t[i].nmx = r - l + 1;
}
void pushup(int i, int l, int r)
{
int mid = (l+r)>>1;
t[i].dlx = t[i*2].dlx; t[i].drx = t[i*2+1].drx;
t[i].dmx = max(max(t[i*2].dmx, t[i*2+1].dmx), t[i*2].drx + t[i*2+1].dlx);
if(t[i*2].dlx == mid - l + 1) t[i].dlx += t[i*2+1].dlx;
if(t[i*2+1].drx == r - mid) t[i].drx += t[i*2].drx;
t[i].nlx = t[i*2].nlx; t[i].nrx = t[i*2+1].nrx;
t[i].nmx = max(max(t[i*2].nmx, t[i*2+1].nmx), t[i*2].nrx + t[i*2+1].nlx);
if(t[i*2].nlx == mid - l + 1) t[i].nlx += t[i*2+1].nlx;
if(t[i*2+1].nrx == r - mid) t[i].nrx += t[i*2].nrx;
}
void pushdown(int i, int l, int r)
{
int mid = (l + r)>>1;
if(t[i].s)
{
stu(i*2, l, mid);
stu(i*2+1, mid+1, r);
t[i].s = 0;
}
if(t[i].n)
{
ns(i*2, l, mid);
ns(i*2+1, mid+1, r);
t[i].n = 0;
}
if(t[i].d)
{
ds(i*2, l, mid);
ds(i*2+1, mid+1, r);
t[i].d = 0;
}
}
void Creat(int i, int l, int r)
{
stu(i, l, r);
if(l == r) return ;
int mid = (l+r)>>1;
Creat(i<<1,l, mid);
Creat(i<<1|1, mid+1, r);
pushup(i, l, r);
}
void study(int i, int l, int r, int x, int y)
{
if(x == l && r == y)
{
stu(i, l, r);
return ;
}
pushdown(i, l, r);
int mid = (l+r)>>1;
if(mid >= y) study(i*2, l, mid, x, y);
else if(mid < x) study(i*2+1, mid+1, r, x, y);
else
{
study(i*2, l, mid, x, mid);
study(i*2+1, mid+1, r, mid+1, y);
}
pushup(i, l, r);
}
void updata(int i, int l, int r, int x, int y, int f)
{
if(x == l && r == y)
{
if(f) ds(i, l, r);
else ns(i, l, r);
return ;
}
pushdown(i, l, r);
int mid = (l+r)>>1;
if(mid >= y) updata(i*2, l, mid, x, y, f);
else if(mid < x) updata(i*2+1, mid+1, r, x, y, f);
else
{
updata(i*2, l, mid, x, mid, f);
updata(i*2+1, mid+1, r, mid+1, y, f);
}
pushup(i, l, r);
}
int Find(int i, int l, int r, int x, int f)
{
if(l == r) return l;
int mid = (l + r)>>1;
pushdown(i, l, r);
if(f)
{
if(t[i*2].dmx >= x) return Find(i*2, l, mid, x, f);
else if(t[i*2].drx + t[i*2+1].dlx >= x) return mid - t[i*2].drx + 1;
else return Find(i*2+1, mid+1, r, x, f);
}
else
{
if(t[i*2].nmx >= x) return Find(i*2, l, mid, x, f);
else if(t[i*2].nrx + t[i*2+1].nlx >= x) return mid - t[i*2].nrx + 1;
else return Find(i*2+1, mid+1, r, x, f);
}
}
int main()
{
int T, n, m, cnt = 0;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
Creat(1, 1, n);
char s[11];
int x, y;
printf("Case %d:\n", ++cnt);
while(m--)
{
scanf(" %s", s);
if(s[0] == 'S')
{
scanf("%d%d", &x, &y);
study(1, 1, n, x, y);
printf("I am the hope of chinese chengxuyuan!!\n");
continue;
}
scanf("%d", &x);
if(s[0] == 'D')
{
if(t[1].dmx < x) printf("fly with yourself\n");
else
{
y = Find(1, 1, n, x, 1);
updata(1, 1, n, y, y+x-1, 1);
printf("%d,let's fly\n", y);
}
}
else
{
if(t[1].dmx < x)
{
if(t[1].nmx < x) printf("wait for me\n");
else
{
y = Find(1, 1, n, x, 0);
updata(1, 1, n, y, y+x-1, 0);
printf("%d,don't put my gezi\n", y);
}
}
else
{
y = Find(1, 1, n, x, 1);
updata(1, 1, n, y, y+x-1, 0);
printf("%d,don't put my gezi\n", y);
}
}
}
}
return 0;
}
//2
//5 5
//DS 1
//NS 5
//DS 1
//NS 3
//NS 6
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4267
4 1 1 1 1 14 2 1 2 2 2 3 2 4 1 2 3 1 2 2 1 2 2 2 3 2 4 1 1 4 2 1 2 1 2 2 2 3 2 4
1 1 1 1 1 3 3 1 2 3 4 1
解析:区间更新,树状数组写,dp[x][k][mod]是第x位置,%k,余数为mod
代码:
#include
using namespace std;
int dp[50009][11][11];
int aa[50009], n;
void add(int x, int k, int mod, int c)
{
while(x)
{
dp[x][k][mod] += c;
x -= x&(-x);
}
}
int sum(int x,int a)
{
int ans = 0;
while(x <= n)
{
for(int i = 1; i <= 10; i++) ans += dp[x][i][a%i];
x += x&(-x);
}
return ans;
}
int main()
{
while(~scanf("%d", &n))
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++) scanf("%d", &aa[i]);
int m, op, a, b, c, k;
scanf("%d", &m);
while(m--)
{
scanf("%d", &op);
if(op == 1)
{
scanf("%d%d%d%d", &a, &b, &k, &c);
add(b, k, a%k, c);
add(a-1, k, a%k, -c);
}
else
{
scanf("%d", &a);
printf("%d\n", sum(a, a) + aa[a]);
}
}
}
return 0;
}
题目链接:http://lightoj.com/volume_showproblem.php?problem=1120
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 64 MB |
Given some axis parallel rectangles, you have to find the union of their area. For example, see the shaded regions in the picture. Each rectangle will be denoted by four integers. They are x1, y1, x2, y2 where (x1, y1) denotes the lower left corner and (x2, y2) denotes the upper right corner.
For the picture above, there are three rectangles. For the yellow rectangle the co-ordinates are (0, 2) and (3, 6). For the blue rectangle the co-ordinates are (1, 3) and (6, 7). For the green rectangle the co-ordinates are (2, 1) and (5, 4). So, the union area is (the shaded region) 31 square units.
Input starts with an integer T (≤ 13), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 30000). Each of the next n lines will contain four integers x1, y1, x2, y2 (0 ≤ x1, y1, x2, y2 ≤ 109, x1 < x2, y1 < y2) denoting a rectangle.
For each case, print the case number and the union area.
Sample Input |
Output for Sample Input |
2 3 0 2 3 6 1 3 6 7 2 1 5 4 2 0 0 4 4 1 1 2 5 |
Case 1: 31 Case 2: 17 |
Dataset is huge, use faster I/O methods.
Developed and Maintained by
JANE ALAM JAN |
解析:裸的扫描线,用线段树去维护当前区间,这里其实有图的,还没画,待更新,见下图(矩形的下边为1,上边为-1,用于标记,然后一层一层的计算面积,图为样例1)
扫描线基础题:http://codeforces.com/contest/612/problem/D
题解:http://blog.csdn.net/qq_34287501/article/details/77248371
代码:
#include
using namespace std;
typedef long long LL;
struct node
{
int x1, x2, y, f;
}mp[60009];
int x[60009];
int f[60009*4], sum[60009*4];
void pushup(int i, int l, int r)
{
if(f[i])
{
sum[i] = x[r+1] - x[l];
}
else if(l == r)
{
sum[i] = 0;
}
else sum[i] = sum[i<<1] + sum[i<<1|1];
}
void updata(int i, int l, int r, int x, int y, int z)
{
if(y < l || r < x) return ;
if(x <= l && r <= y)
{
f[i] += z;
pushup(i, l, r);
return ;
}
int mid = (l+r)>>1;
updata(i<<1, l, mid, x, y, z);
updata(i<<1|1, mid+1, r, x, y, z);
pushup(i, l, r);
}
bool cmp(node a, node b)
{
if(a.y != b.y) return a.y < b.y;
return a.f > b.f;
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("o.txt", "w", stdout);
int t, n, Case = 0;
scanf("%d", &t);
while(t--)
{
memset(sum, 0, sizeof(sum));
memset(f, 0, sizeof(f));
scanf("%d", &n);
int len = 0, x1, y1, x2, y2;
for(int i = 1; i <= n; i++)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
mp[len] = (node){x1, x2, y1, 1};
x[len++] = x1;
mp[len] = (node){x1, x2, y2, -1};
x[len++] = x2;
}
sort(x, x+len);
sort(mp, mp+len, cmp);
int cnt = unique(x, x+len) - x;
int l, r;
LL ans = 0ll;
for(int i = 0; i < len; i++)
{
l = lower_bound(x, x+cnt, mp[i].x1) - x;
r = lower_bound(x, x+cnt, mp[i].x2) - x - 1;
updata(1, 0, cnt - 1, l, r, mp[i].f);
ans += (LL)sum[1] * (mp[i+1].y - mp[i].y);
}
printf("Case %d: %lld\n", ++Case, ans);
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
7.63 0.00
解析:要求覆盖大于等于2次的面积,原来求的是所有的面积,维护的是覆盖1次以上的sum1[1],现在需要多加一个维护覆盖2次以上的sum[1],这样就可以写了,把pushup函数改下更新下sum[i]数组就好了
代码:
#include
using namespace std;
typedef long long LL;
const int N = 2009;
struct node
{
double x1, x2, y;
int f;
}mp[N];
double x[N], sum[N<<2], sum1[N<<2];
int f[N<<2];
void pushup(int i, int l, int r)
{
if(f[i]) sum1[i] = x[r+1] - x[l];
else if(l == r) sum1[i] = 0;
else sum1[i] = sum1[i<<1] + sum1[i<<1|1];
if(f[i] > 1) sum[i] = x[r+1] - x[l];
else if(l == r) sum[i] = 0;
else if(f[i] == 1) sum[i] = sum1[i<<1] + sum1[i<<1|1];
else sum[i] = sum[i<<1] + sum[i<<1|1];
}
void updata(int i, int l, int r, int x, int y, int z)
{
if(y < l || r < x) return ;
if(x == l && r == y)
{
f[i] += z;
pushup(i, l, r);
return ;
}
int mid = (l+r)>>1;
if(mid >= y) updata(i<<1, l, mid, x, y, z);
else if(mid < x) updata(i<<1|1, mid+1, r, x, y, z);
else
{
updata(i<<1, l, mid, x, mid, z);
updata(i<<1|1, mid+1, r, mid+1, y, z);
}
pushup(i, l, r);
}
bool cmp(node a, node b)
{
if(a.y != b.y) return a.y < b.y;
return a.f > b.f;
}
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
memset(sum, 0, sizeof(sum));
memset(sum1, 0, sizeof(sum1));
memset(f, 0, sizeof(f));
scanf("%d", &n);
int len = 0;
double x1, y1, x2, y2;
for(int i = 1; i <= n; i++)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
mp[len] = (node){x1, x2, y1, 1};
x[len++] = x1;
mp[len] = (node){x1, x2, y2, -1};
x[len++] = x2;
}
sort(x, x+len);
sort(mp, mp+len, cmp);
int cnt = unique(x, x+len) - x;
int l, r;
double ans = 0.0;
for(int i = 0; i < len; i++)
{
l = lower_bound(x, x+cnt, mp[i].x1) - x;
r = lower_bound(x, x+cnt, mp[i].x2) - x - 1;
updata(1, 0, cnt - 1, l, r, mp[i].f);
ans += sum[1] * (mp[i+1].y - mp[i].y);
}
printf("%.2f\n", ans);
}
return 0;
}
附加:河南省多校联萌(一)D题
题目链接:http://acm.uestc.edu.cn/#/problem/show/1597
解析:线段树区间更新,更新区间加法和乘法,需要2个延迟标记
代码:
#include
#include
#include
typedef long long LL;
using namespace std;
const int N = 111111;
LL p;
struct node
{
LL x, mark1, mark2;
}t[N<<2];
void Creat(int i, int l, int r)
{
t[i].mark1 = 1ll;
t[i].mark2 = 0ll;
if(l == r)
{
scanf("%lld", &t[i].x);
return ;
}
int mid = (l + r)>>1;
Creat(i*2, l, mid);
Creat(i*2+1, mid+1, r);
t[i].x = (t[i*2].x + t[i*2+1].x) % p;
}
void pushdown(int i, int l, int r)
{
int mid = (l + r)>>1;
t[i*2].mark1 = (t[i*2].mark1 * t[i].mark1) % p;
t[i*2+1].mark1 = (t[i*2+1].mark1 * t[i].mark1) % p;
t[i*2].mark2 = (t[i*2].mark2 * t[i].mark1 + t[i].mark2) % p;
t[i*2+1].mark2 = (t[i*2+1].mark2 * t[i].mark1 + t[i].mark2) % p;
t[i*2].x = (t[i*2].x * t[i].mark1 + t[i].mark2*(mid-l+1ll)) % p;
t[i*2+1].x = (t[i*2+1].x * t[i].mark1 + t[i].mark2*((LL)r-mid)) % p;
t[i].mark1 = 1ll;
t[i].mark2 = 0ll;
}
void updata(int i, int l, int r, int x, int y, LL z, int f)
{
if(r < x || y < l) return ;
if(x <= l && r <= y)
{
if(f)
{
t[i].mark2 = t[i].mark2 * z % p;
t[i].mark1 = t[i].mark1 * z % p;
t[i].x = (t[i].x * z) % p;
}
else
{
t[i].mark2 = (t[i].mark2 + z) % p;
t[i].x = (t[i].x + z*(r-l+1)) % p;
}
return ;
}
pushdown(i, l, r);
int mid = (l + r)>>1;
updata(i*2, l, mid, x, y, z, f);
updata(i*2+1, mid+1, r, x, y, z, f);
t[i].x = (t[i*2].x + t[i*2+1].x) % p;
}
LL Find(int i, int l, int r, int x, int y)
{
if(r < x || y < l) return 0ll;
if(x <= l && r <= y) return t[i].x;
pushdown(i, l, r);
int mid = (l + r)>>1;
return (Find(i*2, l, mid, x, y) + Find(i*2+1, mid+1, r, x, y)) % p;
}
int main()
{
int n, m, op, x, y;
LL z;
scanf("%d%lld", &n, &p);
Creat(1, 1, n);
scanf("%d", &m);
while(m--)
{
scanf("%d", &op);
if(op == 3)
{
scanf("%d%d", &x, &y);
printf("%lld\n", Find(1, 1, n, x, y));
continue;
}
scanf("%d%d%lld", &x, &y, &z);
if(op == 1) updata(1, 1, n, x, y, z, 1);
else updata(1, 1, n, x, y, z, 0);
}
return 0;
}